comparison dmd/codegen/Util.d @ 108:6da99741178e

e2ir.c changes, mainly accounts for static arrays being value types now
author Trass3r
date Tue, 31 Aug 2010 21:41:01 +0200
parents be2ab491772e
children 3482c73a991b
comparison
equal deleted inserted replaced
107:d8f19d85fadb 108:6da99741178e
3 import dmd.Loc; 3 import dmd.Loc;
4 import dmd.Id; 4 import dmd.Id;
5 import dmd.IRState; 5 import dmd.IRState;
6 import dmd.Type; 6 import dmd.Type;
7 import dmd.Array; 7 import dmd.Array;
8 import dmd.Declaration;
8 import dmd.Dsymbol; 9 import dmd.Dsymbol;
9 import dmd.FuncDeclaration; 10 import dmd.FuncDeclaration;
10 import dmd.Identifier; 11 import dmd.Identifier;
11 import dmd.RET; 12 import dmd.RET;
12 import dmd.TY; 13 import dmd.TY;
58 import core.stdc.string; 59 import core.stdc.string;
59 import core.stdc.stdlib; 60 import core.stdc.stdlib;
60 61
61 import core.memory; 62 import core.memory;
62 63
64
65 /* If variable var of type typ is a reference
66 */
67 version(SARRAYVALUE)
68 {
69 bool ISREF(Declaration var, Type tb) {return var.isOut() || var.isRef();}
70 }
71 else
72 bool ISREF(Declaration var, Type tb) {return (var.isParameter() && tb.ty == TY.Tsarray) || var.isOut() || var.isRef();}
73
74
75
63 /************************************ 76 /************************************
64 * Call a function. 77 * Call a function.
65 */ 78 */
66
67 elem* callfunc(Loc loc, 79 elem* callfunc(Loc loc,
68 IRState* irs, 80 IRState* irs,
69 int directcall, // 1: don't do virtual call 81 int directcall, // 1: don't do virtual call
70 Type tret, // return type 82 Type tret, // return type
71 elem *ec, // evaluates to function address 83 elem *ec, // evaluates to function address
124 if (arguments) 136 if (arguments)
125 { 137 {
126 // j=1 if _arguments[] is first argument 138 // j=1 if _arguments[] is first argument
127 int j = (tf.linkage == LINK.LINKd && tf.varargs == 1); 139 int j = (tf.linkage == LINK.LINKd && tf.varargs == 1);
128 140
129 for (i = 0; i < arguments.dim ; i++) 141 foreach (size_t i, Expression arg; arguments)
130 { 142 {
131 Expression arg;
132 elem* ea; 143 elem* ea;
133 144
134 arg = cast(Expression)arguments.data[i]; 145 //writef("\targ[%d]: %s\n", i, arg.toChars());
135 //printf("\targ[%d]: %s\n", i, arg.toChars());
136 146
137 size_t nparams = Argument.dim(tf.parameters); 147 size_t nparams = Argument.dim(tf.parameters);
138 if (i - j < nparams && i >= j) 148 if (i - j < nparams && i >= j)
139 { 149 {
140 Argument p = Argument.getNth(tf.parameters, i - j); 150 Argument p = Argument.getNth(tf.parameters, i - j);
148 goto L1; 158 goto L1;
149 } 159 }
150 } 160 }
151 ea = arg.toElem(irs); 161 ea = arg.toElem(irs);
152 L1: 162 L1:
153 if (tybasic(ea.Ety) == TYM.TYstruct) 163 if (tybasic(ea.Ety) == TYM.TYstruct || tybasic(ea.Ety) == TYarray)
154 { 164 {
155 ea = el_una(OPER.OPstrpar, TYM.TYstruct, ea); 165 ea = el_una(OPER.OPstrpar, TYM.TYstruct, ea);
156 ea.Enumbytes = ea.E1.Enumbytes; 166 ea.Enumbytes = ea.E1.Enumbytes;
157 assert(ea.Enumbytes); 167 //assert(ea.Enumbytes);
158 } 168 }
159 if (reverse) 169 if (reverse)
160 ep = el_param(ep,ea); 170 ep = el_param(ep,ea);
161 else 171 else
162 ep = el_param(ea,ep); 172 ep = el_param(ea,ep);
168 if (!ehidden) 178 if (!ehidden)
169 { 179 {
170 // Don't have one, so create one 180 // Don't have one, so create one
171 type* tt; 181 type* tt;
172 182
173 if (tf.next.toBasetype().ty == TY.Tstruct) 183 Type tret2 = tf.next; // in dmd tret is shadowed here, so -> tret2
174 tt = tf.next.toCtype(); 184 if (tret2.toBasetype().ty == Tstruct ||
185 tret2.toBasetype().ty == Tsarray)
186 tt = tret2.toCtype();
175 else 187 else
176 tt = type_fake(tf.next.totym()); 188 tt = type_fake(tret2.totym());
177 189
178 Symbol* stmp = symbol_genauto(tt); 190 Symbol* stmp = symbol_genauto(tt);
179 ehidden = el_ptr(stmp); 191 ehidden = el_ptr(stmp);
180 } 192 }
181 if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && tf.linkage != LINK.LINKd) { 193 if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && tf.linkage != LINK.LINKd) {
282 } 294 }
283 else 295 else
284 e = el_una(op,tyret,ep); 296 e = el_una(op,tyret,ep);
285 } 297 }
286 else if (ep) 298 else if (ep)
287 e = el_bin(tf.ispure ? OPER.OPcallns : OPER.OPcall, tyret, ec, ep); 299 e = el_bin((tf.ispure && tf.isnothrow) ? OPER.OPcallns : OPER.OPcall, tyret, ec, ep);
288 else 300 else
289 e = el_una(tf.ispure ? OPER.OPucallns : OPER.OPucall, tyret, ec); 301 e = el_una((tf.ispure && tf.isnothrow) ? OPER.OPucallns : OPER.OPucall, tyret, ec);
290 302
291 if (retmethod == RET.RETstack) 303 if (retmethod == RET.RETstack)
292 { 304 {
293 e.Ety = TYM.TYnptr; 305 e.Ety = TYM.TYnptr;
294 e = el_una(OPER.OPind, tyret, e); 306 e = el_una(OPER.OPind, tyret, e);
790 * eptr where to write the data to 802 * eptr where to write the data to
791 * evalue value to write 803 * evalue value to write
792 * edim number of times to write evalue to eptr[] 804 * edim number of times to write evalue to eptr[]
793 * tb type of evalue 805 * tb type of evalue
794 */ 806 */
795
796 elem* setArray(elem* eptr, elem* edim, Type tb, elem* evalue, IRState* irs, int op) 807 elem* setArray(elem* eptr, elem* edim, Type tb, elem* evalue, IRState* irs, int op)
797 { 808 {
798 int r; 809 int r;
799 elem* e; 810 elem* e;
800 int sz = cast(int)tb.size(); 811 int sz = cast(int)tb.size();
850 { 861 {
851 r = RTLSYM.RTLSYM_MEMSET8; 862 r = RTLSYM.RTLSYM_MEMSET8;
852 edim = el_bin(OPER.OPmul, TYM.TYuint, edim, el_long(TYM.TYuint, sz)); 863 edim = el_bin(OPER.OPmul, TYM.TYuint, edim, el_long(TYM.TYuint, sz));
853 } 864 }
854 865
855 if (tybasic(evalue.Ety) == TYM.TYstruct) 866 if (tybasic(evalue.Ety) == TYM.TYstruct || tybasic(evalue.Ety) == TYarray)
856 { 867 {
857 evalue = el_una(OPER.OPstrpar, TYM.TYstruct, evalue); 868 evalue = el_una(OPER.OPstrpar, TYM.TYstruct, evalue);
858 evalue.Enumbytes = evalue.E1.Enumbytes; 869 evalue.Enumbytes = evalue.E1.Enumbytes;
859 assert(evalue.Enumbytes); 870 assert(evalue.Enumbytes);
860 } 871 }
985 { 996 {
986 case TY.Tarray: 997 case TY.Tarray:
987 break; 998 break;
988 999
989 case TY.Tsarray: 1000 case TY.Tsarray:
990 e = el_una(OPER.OPaddr, TYM.TYnptr, e); 1001 e = addressElem(e, t);
991 dim = cast(uint)(cast(TypeSArray)t).dim.toInteger(); 1002 dim = cast(uint)(cast(TypeSArray)t).dim.toInteger();
992 e = el_pair(TYM.TYullong, el_long(TYM.TYint, dim), e); 1003 e = el_pair(TYM.TYullong, el_long(TYM.TYint, dim), e);
993 break; 1004 break;
994 1005
995 default: 1006 default:
1060 } 1071 }
1061 1072
1062 return el_combine(ef, e); 1073 return el_combine(ef, e);
1063 } 1074 }
1064 1075
1076 /************************************
1077 */
1065 elem* sarray_toDarray(Loc loc, Type tfrom, Type tto, elem* e) 1078 elem* sarray_toDarray(Loc loc, Type tfrom, Type tto, elem* e)
1066 { 1079 {
1067 //printf("sarray_toDarray()\n"); 1080 //printf("sarray_toDarray()\n");
1068 //elem_print(e); 1081 //elem_print(e);
1069 1082
1070 elem* elen;
1071 uint dim = cast(uint)(cast(TypeSArray)tfrom).dim.toInteger(); 1083 uint dim = cast(uint)(cast(TypeSArray)tfrom).dim.toInteger();
1072 1084
1073 if (tto) 1085 if (tto)
1074 { 1086 {
1075 uint fsize = cast(uint)tfrom.nextOf().size(); 1087 uint fsize = cast(uint)tfrom.nextOf().size();
1082 } 1094 }
1083 dim = (dim * fsize) / tsize; 1095 dim = (dim * fsize) / tsize;
1084 } 1096 }
1085 1097
1086 L1: 1098 L1:
1087 elen = el_long(TYM.TYint, dim); 1099 elem* elen = el_long(TYM.TYint, dim);
1088 e = el_una(OPER.OPaddr, TYM.TYnptr, e); 1100 e = addressElem(e, tfrom);
1089 e = el_pair(TYM.TYullong, elen, e); 1101 e = el_pair(TYM.TYullong, elen, e);
1090 return e; 1102 return e;
1091 } 1103 }
1092 1104
1093 elem* eval_Darray(IRState* irs, Expression e) 1105 elem* eval_Darray(IRState* irs, Expression e)