diff gen/CodeGen.d @ 181:59cd211a1bd3

Better support for function pointers
author Anders Halager <halager@gmail.com>
date Fri, 25 Jul 2008 01:39:01 +0200
parents c8e26556c24d
children 8ea749b7da91
line wrap: on
line diff
--- a/gen/CodeGen.d	Fri Jul 25 01:21:07 2008 +0200
+++ b/gen/CodeGen.d	Fri Jul 25 01:39:01 2008 +0200
@@ -218,7 +218,7 @@
                 auto id = varDecl.identifier;
                 Type t = llvm(id.type);
                 GlobalVariable g = m.addGlobal(t, id.get);
-                g.initializer = ConstantInt.GetS(t, 0);
+                g.initializer = Constant.GetNull(t);
                 table[varDecl.identifier.get] = g;
                 break;
 
@@ -342,6 +342,10 @@
                 auto derefExp = cast(DerefExp)exp;
                 auto target = genExpression(derefExp.exp);
                 return RValue(b.buildLoad(target.value, "deref"));
+            case ExpType.AddressOfExp:
+                auto addrExp = cast(AddressOfExp)exp;
+                auto target = genLValue(addrExp.exp);
+                return RValue(target.getAddress());
             case ExpType.AssignExp:
                 auto AE = cast(AssignExp)exp;
                 LValue dst = genLValue(AE.identifier);
@@ -372,11 +376,11 @@
                 // if delegate do a third thing
                 // if struct/class check for opCall
                 DType type = callExp.exp.type;
-                assert (type.isFunction(), "Can only call functions");
+                assert (type.isCallable(), "Can only call functions");
                 scope args = new Value[callExp.args.length];
                 foreach (i, arg; callExp.args)
                     args[i] = genExpression(arg).value;
-                DFunction ftype = type.asFunction();
+                DFunction ftype = type.asCallable();
                 Type llvm_ftype = llvm(ftype);
                 Value f = null;
                 if (callExp.callSym is null)
@@ -409,11 +413,19 @@
 
             case ExpType.Identifier:
                 auto id = cast(Identifier)exp;
-                if (id.type.isStruct()
-                        || id.type.isArray()
-                        || id.type.isStaticArray()
-                        || id.type.isClass())
+                auto type = id.type;
+                if (type.isStruct()
+                        || type.isArray()
+                        || type.isStaticArray()
+                        || type.isClass())
                     return RValue(table.find(id.get));
+                else if (type.isFunction())
+                {
+                    auto func_name = id.getSymbol().getMangledFQN();
+                    return RValue(m.getNamedFunction(func_name));
+                }
+                else if (type.isCallable())
+                    return RValue(genLValue(id).getAddress());
                 else
                     return RValue(b.buildLoad(table.find(id.get), id.get));
             case ExpType.MemberReference:
@@ -690,7 +702,10 @@
         {
             case ExpType.Identifier:
                 auto id = cast(Identifier)exp;
-                return LValue(table.find(id.get));
+                Value v = table.find(id.get);
+                if (v is null)
+                    v = m.getNamedFunction(id.getSymbol().getMangledFQN());
+                return LValue(v);
             case ExpType.Deref:
                 // LValue(*x): load(x)
                 // RValue(*x): load(load(x))