# HG changeset patch # User Anders Halager # Date 1216726160 -7200 # Node ID 6cb2f4201e2aab7aee1509c532c86eba4ed239f4 # Parent a2d9121d6dff294e00fe1fcd78347ebe5722fbb0 Improved static arrays Here is a list of some stuff that works char[3] s = "hey" char[3] s2 = s; s2[1] = 98 // no support for chars, but 98 = 'b' :) int[2] i; i[0] = 2; Still can't pass static arrays to functions diff -r a2d9121d6dff -r 6cb2f4201e2a ast/Exp.d --- a/ast/Exp.d Tue Jul 22 00:34:49 2008 +0200 +++ b/ast/Exp.d Tue Jul 22 13:29:20 2008 +0200 @@ -485,9 +485,10 @@ this.str = str; } - override DType type() { return null; } + override DType type() { return DType.Char.getAsStaticArray(data.length); } char[] str; + ubyte[] data; } class NewExp : Exp diff -r a2d9121d6dff -r 6cb2f4201e2a gen/CodeGen.d --- a/gen/CodeGen.d Tue Jul 22 00:34:49 2008 +0200 +++ b/gen/CodeGen.d Tue Jul 22 13:29:20 2008 +0200 @@ -295,6 +295,14 @@ case NumberType.Real: return RValue(ConstantReal.Get(Type.X86_FP80, integerLit.number.floating)); } + case ExpType.StringExp: + auto stringExp = cast(StringExp)exp; + char[] data = cast(char[])stringExp.data; + auto string_constant = ConstantArray.GetString(data, true); + auto gv = m.addGlobal(string_constant, "string"); + gv.linkage = Linkage.Internal; + gv.globalConstant = true; + return RValue(gv); case ExpType.Negate: auto negateExp = cast(NegateExp)exp; auto target = genExpression(negateExp.exp); @@ -346,7 +354,9 @@ case ExpType.Identifier: auto id = cast(Identifier)exp; - if(id.type.isStruct() || id.type.isArray()) + if (id.type.isStruct() + || id.type.isArray() + || id.type.isStaticArray()) return RValue(table.find(id.get)); else return RValue(b.buildLoad(table.find(id.get), id.get)); @@ -701,6 +711,8 @@ if (auto st = t.asStruct()) genMemcpy(to, from, t); + else if (auto sa = t.asStaticArray()) + genMemcpy(to, from, t, sa.arrayOf.byteSize); else b.buildStore(from, to); } @@ -709,7 +721,7 @@ Copy from src into dst. The values are assumed to have the same size, and the amount of bytes to copy is taken from t. **/ - void genMemcpy(Value dst, Value src, DType t) + void genMemcpy(Value dst, Value src, DType t, int alignment = 16) { Value from = b.buildBitCast(src, BytePtr, ".copy_from"); Value to = b.buildBitCast(dst, BytePtr, ".copy_to"); @@ -717,7 +729,7 @@ args[0] = to; args[1] = from; args[2] = ConstantInt.GetS(Type.Int32, t.byteSize()); - args[3] = ConstantInt.GetS(Type.Int32, 32); + args[3] = ConstantInt.GetS(Type.Int32, alignment); b.buildCall(llvm_memcpy, args[], null); } diff -r a2d9121d6dff -r 6cb2f4201e2a parser/Parser.d --- a/parser/Parser.d Tue Jul 22 00:34:49 2008 +0200 +++ b/parser/Parser.d Tue Jul 22 13:29:20 2008 +0200 @@ -706,7 +706,7 @@ { // manually hardcoded to only support "type id [= exp];" // as that is the only thing the codegen understands - Id type = parseType; + Id type = parseType(); Id id = Id(next()); Exp init; if (skip(Tok.Assign)) diff -r a2d9121d6dff -r 6cb2f4201e2a sema/DType.d --- a/sema/DType.d Tue Jul 22 00:34:49 2008 +0200 +++ b/sema/DType.d Tue Jul 22 13:29:20 2008 +0200 @@ -100,6 +100,11 @@ return cast(hash_t)(cast(void*)this); } + char[] toString() + { + return id; + } + char[] name() { return id; } SourceLocation getLoc() { return loc; } int byteSize() { return 0; } @@ -473,7 +478,7 @@ { this(DType arrayOf, int size, DType actual = null) { - super(id, actual); + super(arrayOf.id ~ "[" ~ Integer.toString(size) ~ "]", actual); this.arrayOf = arrayOf; this.size = size; } diff -r a2d9121d6dff -r 6cb2f4201e2a sema/LiteralInterpreter.d --- a/sema/LiteralInterpreter.d Tue Jul 22 00:34:49 2008 +0200 +++ b/sema/LiteralInterpreter.d Tue Jul 22 13:29:20 2008 +0200 @@ -20,7 +20,8 @@ void visitStringExp(StringExp exp) { - auto type = parseString(exp.str, exp.loc, messages); + auto str = parseString(exp.str, exp.loc, messages); + exp.data = str.data; } void visitIntegerLit(IntegerLit exp) diff -r a2d9121d6dff -r 6cb2f4201e2a sema/TypeCheck.d --- a/sema/TypeCheck.d Tue Jul 22 00:34:49 2008 +0200 +++ b/sema/TypeCheck.d Tue Jul 22 13:29:20 2008 +0200 @@ -258,7 +258,7 @@ if(decl.init) { - auto varType = decl.type; + auto varType = decl.identifier.type; auto expType = decl.init.type; if(varType.byteSize != expType.byteSize) { diff -r a2d9121d6dff -r 6cb2f4201e2a test.td --- a/test.td Tue Jul 22 00:34:49 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ - - - -int main() -{ - int y = 4; - - karina k; - - - while (y > 0) - y = y - 1; - - return x; -} - -int x = 4; - - -struct karina -{ - anders a; - int age; - int width; - int height; - int lovers; -} -struct anders -{ - int hej; -} diff -r a2d9121d6dff -r 6cb2f4201e2a tests/parser/string_1.d --- a/tests/parser/string_1.d Tue Jul 22 00:34:49 2008 +0200 +++ b/tests/parser/string_1.d Tue Jul 22 13:29:20 2008 +0200 @@ -26,16 +26,14 @@ char[8] s16 = "\x61\u05D0\U000201A4"; char[2] s17 = "\122\522"; - char[6] s15 = x"61 62 63 64 + char[6] s18 = x"61 62 63 64 65 66 67 68"; - char[4] s16 = "\®\&"; + char[4] s19 = "\®\&"; - char[4] s16 = "\®\&"c; - wchar[2] s16 = "\®\&"w; - dchar[2] s16 = "\®\&"d; - - + char[4] s20 = "\®\&"c; + wchar[2] s21 = "\®\&"w; + dchar[2] s22 = "\®\&"d; return 0; }