changeset 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 7606387b2f0a
children cbebde9ba2c8 7982eb63c0eb
files gen/CodeGen.d
diffstat 1 files changed, 36 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/gen/CodeGen.d	Tue Jul 22 18:24:15 2008 +0200
+++ b/gen/CodeGen.d	Tue Jul 22 21:34:53 2008 +0200
@@ -320,6 +320,17 @@
             case ExpType.Index:
                 auto indexExp = cast(IndexExp)exp;
                 return loadLValue(genLValue(exp));
+            case ExpType.NewExp:
+                auto newExp = cast(NewExp)exp;
+                DClass type = newExp.newType.type().asClass();
+                auto llvm_type = cast(PointerType)llvm(type);
+                auto pointer = b.buildMalloc(llvm_type.elementType(), "new");
+                scope args = new Value[newExp.c_args.length];
+                foreach (i, arg; newExp.c_args)
+                    args[i] = genExpression(arg).value;
+                auto f = m.getNamedFunction(newExp.callSym.getMangledFQN());
+                b.buildCall(f, args, "");
+                return RValue(pointer);
             case ExpType.CallExp:
                 auto callExp = cast(CallExp)exp;
                 // BUG: Might not be a simple identifier, a.foo(x) is also a
@@ -356,7 +367,8 @@
                 auto id = cast(Identifier)exp;
                 if (id.type.isStruct()
                         || id.type.isArray()
-                        || id.type.isStaticArray())
+                        || id.type.isStaticArray()
+                        || id.type.isClass())
                     return RValue(table.find(id.get));
                 else
                     return RValue(b.buildLoad(table.find(id.get), id.get));
@@ -839,7 +851,29 @@
 
             Type res = StructType.Get(members.unsafe());
             type_map[t] = res;
-            m.addTypeName(s.name, res);
+            m.addTypeName("struct." ~ s.name, res);
+            return res;
+        }
+        else if (auto c = t.asClass)
+        {
+            SmallArray!(Type) members;
+            if (c.members.length > 0 && false)
+            {
+                DType[] array;
+                array.length = c.members.length;
+
+                foreach (m; c.members)
+                    array[m.index] = m.type;
+
+                foreach (m; array)
+                    members ~= llvm(m);
+            }
+            else members ~= Type.Int32;
+
+            Type res = StructType.Get(members.unsafe());
+            res = PointerType.Get(res);
+            type_map[t] = res;
+            m.addTypeName("class." ~ c.name, res);
             return res;
         }
         else if (auto f = t.asFunction)
@@ -860,29 +894,6 @@
 
             Type res = FunctionType.Get(ret_t, params.unsafe());
             type_map[t] = res;
-            /*
-            //TODO: create own DType -> FunctionType mapping without names
-            auto id = new Identifier(f.name);
-            id.setType(f);
-
-            auto f_name = symbolName(id);
-            auto f_t = m.getNamedFunction(f_name);
-            if(f_t is null)
-            {
-                Stdout("oh noes").newline;
-                auto llfunc = m.addFunction(res, f_name);
-
-                foreach (i, param; f.params)
-                    if (param.isStruct)
-                        llfunc.addParamAttr(i, ParamAttr.ByVal);
-
-                if (f.firstParamIsReturnValue)
-                {
-                    llfunc.removeParamAttr(0, ParamAttr.ByVal);
-                    llfunc.addParamAttr(0, ParamAttr.StructRet);
-                }
-            }
-            */
             return res;
         }
         else if (auto f = t.asPointer)