# HG changeset patch # User Anders Johnsen # Date 1210092668 -7200 # Node ID 438e6ed4cda127d99cd34d45011daf808251d8a3 # Parent 6aecbe5a770644c8f3a9adebb2b812871093a168 Now D-Mangling the function types. Still need to mangle "scopes" - by that i mean structs, classes and modules. diff -r 6aecbe5a7706 -r 438e6ed4cda1 ast/Exp.d --- a/ast/Exp.d Tue May 06 17:01:00 2008 +0200 +++ b/ast/Exp.d Tue May 06 18:51:08 2008 +0200 @@ -505,6 +505,16 @@ { return name; } + + char[] getMangled() + { + DType t = type; + + if(name == "main") + return "_Dmain"; + + return "_D"~name~t.mangle; + } hash_t toHash() { diff -r 6aecbe5a7706 -r 438e6ed4cda1 gen/CodeGen.d --- a/gen/CodeGen.d Tue May 06 17:01:00 2008 +0200 +++ b/gen/CodeGen.d Tue May 06 18:51:08 2008 +0200 @@ -118,7 +118,7 @@ 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); + auto llfunc = m.addFunction(func_t, fd.identifier.getMangled); foreach (i, p; fd.funcArgs) { @@ -171,7 +171,7 @@ return; llvm(funcDecl.type); - auto llfunc = m.getNamedFunction(funcDecl.type.name); + auto llfunc = m.getNamedFunction(funcDecl.identifier.getMangled); auto func_tp = cast(PointerType)llfunc.type; auto func_t = cast(FunctionType)func_tp.elementType(); auto ret_t = func_t.returnType(); diff -r 6aecbe5a7706 -r 438e6ed4cda1 sema/DType.d --- a/sema/DType.d Tue May 06 17:01:00 2008 +0200 +++ b/sema/DType.d Tue May 06 18:51:08 2008 +0200 @@ -97,6 +97,15 @@ } private DPointer myPointer; + /** + Mangle the DType following the specs at http://digitalmars.com/d/1.0/abi.html + **/ + char[] mangle() + { + /// expects to be void + return "v"; + } + /** Get a type representing a static array of this type with length 'size' */ @@ -137,6 +146,24 @@ */ class DInteger : DType { + private static char[][DInteger] mangle_types; + + static this() + { + mangle_types = + [ + Bool : "b", + Byte : "g", + UByte : "h", + Short : "s", + UShort : "t", + Int : "i", + UInt : "k", + Long : "l", + ULong : "m" + ]; + } + this(char[] name, int bits, bool unsigned) { super(name, null); @@ -157,6 +184,11 @@ override bool isInteger() { return true; } override DInteger asInteger() { return this; } + override char[] mangle() + { + return mangle_types[this]; + } + int bits; bool unsigned; } @@ -199,6 +231,11 @@ DStructMember[char[]] members; private int bytes_total; + override char[] mangle() + { + return "S"~Integer.toString(name.length)~name; + } + struct DStructMember { DType type; @@ -220,6 +257,11 @@ int byteSize() { return arrayOf.byteSize * size; } + override char[] mangle() + { + return "G"~Integer.toString(size)~arrayOf.mangle; + } + DType arrayOf; const int size; } @@ -237,6 +279,11 @@ int byteSize() { return DType.Int.byteSize; } + override char[] mangle() + { + return "P"~pointerOf.mangle; + } + DType pointerOf; } @@ -250,6 +297,19 @@ override bool isFunction() { return true; } override DFunction asFunction() { return this; } + override char[] mangle() + { + char[] res; + res ~= "F"; + + foreach(param ; params) + res ~= "J" ~ param.mangle; + + res ~= "Z" ~ returnType.mangle; + + return res; + } + DType[] params; DType returnType; bool firstParamIsReturnValue = false; diff -r 6aecbe5a7706 -r 438e6ed4cda1 tests/code/array_1.d --- a/tests/code/array_1.d Tue May 06 17:01:00 2008 +0200 +++ b/tests/code/array_1.d Tue May 06 18:51:08 2008 +0200 @@ -2,7 +2,7 @@ struct Array { int[] data; - int length + int length; } void insert(Array a, int v) @@ -13,10 +13,10 @@ int main() { - array a; + Array a; a.length = 0; insert(a, 5); - return a.data[0] + return a.data[0]; }