diff gen/CodeGen.d @ 68:381975d76baf new_gen

A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
author Anders Johnsen <skabet@gmail.com>
date Thu, 01 May 2008 19:25:49 +0200
parents 3fdf20b08a81
children 70a002b3fba4
line wrap: on
line diff
--- a/gen/CodeGen.d	Tue Apr 29 20:15:22 2008 +0200
+++ b/gen/CodeGen.d	Thu May 01 19:25:49 2008 +0200
@@ -77,6 +77,45 @@
         auto temp = FunctionType.Get(Type.Void, [BytePtr, BytePtr, Type.Int32, Type.Int32]);
         llvm_memcpy = m.addFunction(temp, "llvm.memcpy.i32");
 
+        auto registerFunc =
+            (FuncDecl fd)
+            {
+                Type[] param_types;
+                foreach (i, p; fd.funcArgs)
+                {
+                    DType t = p.env.find(p.identifier).type;
+                    if(auto st = cast(DStruct)t)
+                    {
+                        Type pointer = PointerType.Get(llvm(st));
+                        param_types ~= pointer;
+                    }
+                    else
+                        param_types ~= llvm(t);
+                }
+                auto ret_t = fd.env.find(fd.identifier).type;
+                if(auto st = cast(DStruct)ret_t)
+                    ret_t = DType.Void;
+                else if(auto f = cast(DFunction)ret_t)
+                    ret_t = f.returnType;
+                auto func_t = FunctionType.Get(llvm(ret_t), param_types);
+                auto llfunc = m.addFunction(func_t, fd.identifier.get);
+
+                foreach (i, p; fd.funcArgs)
+                {
+                    if(i == 0 && fd.sret)
+                        llfunc.addParamAttr(0, ParamAttr.StructRet);
+
+                    DType t = p.env.find(p.identifier).type;
+                    if(auto st = cast(DStruct)t)
+                    {
+                        if(i == 0 && fd.sret)
+                            continue;
+                        llfunc.addParamAttr(i,ParamAttr.ByVal);
+                    }
+                }
+            };
+        auto visitor = new VisitFuncDecls(registerFunc);
+        visitor.visit(decls);
         // Before beginning we move all top level var-decls to the start
         // and then we generate the var-decls first
         // partition is NOT required to be stable, but that should not create
@@ -217,7 +256,7 @@
                 auto left = genExpression(binaryExp.left);
                 auto right = genExpression(binaryExp.right);
 
-                sextSmallerToLarger(left, right);
+//                sextSmallerToLarger(left, right);
 
                 OpBuilder op = opToLLVM[binaryExp.op];
 
@@ -248,6 +287,29 @@
                 if(callExp.sret)
                     return b.buildCall(m.getNamedFunction(func_sym.id.get), args, "");
                 return b.buildCall(m.getNamedFunction(func_sym.id.get), args, ".call");
+            case ExpType.CastExp:
+                auto castExp = cast(CastExp)exp;
+                auto value = genExpression(castExp.exp);
+
+                if(!castExp.type.hasImplicitConversionTo(castExp.type))
+                    assert(0, "Invalid cast");
+
+                Stdout(castExp.type.byteSize).newline;
+                Stdout(castExp.type.name).newline;
+                Stdout(castExp.exp.type.byteSize).newline;
+                Stdout(castExp.exp.type.name).newline;
+
+                Stdout(value).newline;
+
+                Value v;
+                if(castExp.exp.type.byteSize <= castExp.type.byteSize)
+                    v = b.buildZExt(value, llvm(castExp.type), "cast");
+                else
+                    v = b.buildTrunc(value, llvm(castExp.type), "cast");
+
+                Stdout(v).newline;
+                return v;
+
             case ExpType.Identifier:
                 auto identifier = cast(Identifier)exp;
                 auto sym = exp.env.find(identifier);
@@ -473,6 +535,9 @@
         Value t = getPointer(target);
         Value v = genExpression(exp);
 
+        Stdout(v).newline;
+        Stdout(t).newline;
+
         auto a = cast(PointerType)t.type;
 
         assert(a, "Assing to type have to be of type PointerType");
@@ -546,8 +611,14 @@
         else if (auto s = t.asStruct)
         {
             SmallArray!(Type, 8) members;
+            DType[] array;
+            array.length = s.members.length;
+
             foreach(m; s.members)
-                members ~= llvm(m.type);
+                array[m.index] = m.type;
+
+            foreach(m; array)
+                members ~= llvm(m);
 
             Type res = StructType.Get(members.unsafe());
             type_map[t] = res;
@@ -570,7 +641,7 @@
 
             Type res = FunctionType.Get(ret_t, params.unsafe());
             type_map[t] = res;
-            auto llfunc = m.addFunction(res, f.name);
+/*            auto llfunc = m.addFunction(res, f.name);
             
             foreach (i, param; f.params)
                 if (param.isStruct)
@@ -581,7 +652,7 @@
                 llfunc.removeParamAttr(0, ParamAttr.ByVal);
                 llfunc.addParamAttr(0, ParamAttr.StructRet);
             }
-
+*/
             return res;
         }
         assert(0, "Only integers, structs and functions are supported");