Mercurial > projects > dang
comparison gen/CodeGen.d @ 166:9cfa33517526
Codegen support for new expressions (very lame so far)
Calls the correct constructor, but it doesn't get a this pointer
Objects are simply malloced, no GC
author | Anders Halager <halager@gmail.com> |
---|---|
date | Tue, 22 Jul 2008 21:34:53 +0200 |
parents | 6cb2f4201e2a |
children | f0385c044065 |
comparison
equal
deleted
inserted
replaced
165:7606387b2f0a | 166:9cfa33517526 |
---|---|
318 storeThroughLValue(dst, src, AE.exp.type()); | 318 storeThroughLValue(dst, src, AE.exp.type()); |
319 return src; | 319 return src; |
320 case ExpType.Index: | 320 case ExpType.Index: |
321 auto indexExp = cast(IndexExp)exp; | 321 auto indexExp = cast(IndexExp)exp; |
322 return loadLValue(genLValue(exp)); | 322 return loadLValue(genLValue(exp)); |
323 case ExpType.NewExp: | |
324 auto newExp = cast(NewExp)exp; | |
325 DClass type = newExp.newType.type().asClass(); | |
326 auto llvm_type = cast(PointerType)llvm(type); | |
327 auto pointer = b.buildMalloc(llvm_type.elementType(), "new"); | |
328 scope args = new Value[newExp.c_args.length]; | |
329 foreach (i, arg; newExp.c_args) | |
330 args[i] = genExpression(arg).value; | |
331 auto f = m.getNamedFunction(newExp.callSym.getMangledFQN()); | |
332 b.buildCall(f, args, ""); | |
333 return RValue(pointer); | |
323 case ExpType.CallExp: | 334 case ExpType.CallExp: |
324 auto callExp = cast(CallExp)exp; | 335 auto callExp = cast(CallExp)exp; |
325 // BUG: Might not be a simple identifier, a.foo(x) is also a | 336 // BUG: Might not be a simple identifier, a.foo(x) is also a |
326 // valid call - or foo(x)(y) for that matter. | 337 // valid call - or foo(x)(y) for that matter. |
327 | 338 |
354 | 365 |
355 case ExpType.Identifier: | 366 case ExpType.Identifier: |
356 auto id = cast(Identifier)exp; | 367 auto id = cast(Identifier)exp; |
357 if (id.type.isStruct() | 368 if (id.type.isStruct() |
358 || id.type.isArray() | 369 || id.type.isArray() |
359 || id.type.isStaticArray()) | 370 || id.type.isStaticArray() |
371 || id.type.isClass()) | |
360 return RValue(table.find(id.get)); | 372 return RValue(table.find(id.get)); |
361 else | 373 else |
362 return RValue(b.buildLoad(table.find(id.get), id.get)); | 374 return RValue(b.buildLoad(table.find(id.get), id.get)); |
363 case ExpType.MemberReference: | 375 case ExpType.MemberReference: |
364 return loadLValue(genLValue(exp)); | 376 return loadLValue(genLValue(exp)); |
837 foreach (m; array) | 849 foreach (m; array) |
838 members ~= llvm(m); | 850 members ~= llvm(m); |
839 | 851 |
840 Type res = StructType.Get(members.unsafe()); | 852 Type res = StructType.Get(members.unsafe()); |
841 type_map[t] = res; | 853 type_map[t] = res; |
842 m.addTypeName(s.name, res); | 854 m.addTypeName("struct." ~ s.name, res); |
855 return res; | |
856 } | |
857 else if (auto c = t.asClass) | |
858 { | |
859 SmallArray!(Type) members; | |
860 if (c.members.length > 0 && false) | |
861 { | |
862 DType[] array; | |
863 array.length = c.members.length; | |
864 | |
865 foreach (m; c.members) | |
866 array[m.index] = m.type; | |
867 | |
868 foreach (m; array) | |
869 members ~= llvm(m); | |
870 } | |
871 else members ~= Type.Int32; | |
872 | |
873 Type res = StructType.Get(members.unsafe()); | |
874 res = PointerType.Get(res); | |
875 type_map[t] = res; | |
876 m.addTypeName("class." ~ c.name, res); | |
843 return res; | 877 return res; |
844 } | 878 } |
845 else if (auto f = t.asFunction) | 879 else if (auto f = t.asFunction) |
846 { | 880 { |
847 // We should never have a function returning structs, because of | 881 // We should never have a function returning structs, because of |
858 else | 892 else |
859 params ~= llvm(param); | 893 params ~= llvm(param); |
860 | 894 |
861 Type res = FunctionType.Get(ret_t, params.unsafe()); | 895 Type res = FunctionType.Get(ret_t, params.unsafe()); |
862 type_map[t] = res; | 896 type_map[t] = res; |
863 /* | |
864 //TODO: create own DType -> FunctionType mapping without names | |
865 auto id = new Identifier(f.name); | |
866 id.setType(f); | |
867 | |
868 auto f_name = symbolName(id); | |
869 auto f_t = m.getNamedFunction(f_name); | |
870 if(f_t is null) | |
871 { | |
872 Stdout("oh noes").newline; | |
873 auto llfunc = m.addFunction(res, f_name); | |
874 | |
875 foreach (i, param; f.params) | |
876 if (param.isStruct) | |
877 llfunc.addParamAttr(i, ParamAttr.ByVal); | |
878 | |
879 if (f.firstParamIsReturnValue) | |
880 { | |
881 llfunc.removeParamAttr(0, ParamAttr.ByVal); | |
882 llfunc.addParamAttr(0, ParamAttr.StructRet); | |
883 } | |
884 } | |
885 */ | |
886 return res; | 897 return res; |
887 } | 898 } |
888 else if (auto f = t.asPointer) | 899 else if (auto f = t.asPointer) |
889 { | 900 { |
890 Type res = PointerType.Get(llvm(f.pointerOf)); | 901 Type res = PointerType.Get(llvm(f.pointerOf)); |