Mercurial > projects > dang
changeset 119:c0b531362ca6
Non compileing commit. Work on floating points and casts
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Sun, 25 May 2008 19:13:07 +0200 |
parents | 54585ad7e426 |
children | 7d0898f77685 |
files | ast/Exp.d basic/LiteralParsing.d gen/CodeGen.d sema/DType.d sema/LiteralInterpreter.d sema/ScopeBuilder.d |
diffstat | 6 files changed, 74 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/ast/Exp.d Sun May 25 18:24:16 2008 +0200 +++ b/ast/Exp.d Sun May 25 19:13:07 2008 +0200 @@ -11,6 +11,8 @@ import sema.Scope, sema.DType; +import basic.LiteralParsing; + enum ExpType { Binary, @@ -314,13 +316,30 @@ return this; } - override DType type() { return DType.Int; } + override DType type() + { + switch(number.type) + { + case NumberType.Int: + return DType.Int; + case NumberType.Long: + return DType.Long; + case NumberType.ULong: + return DType.ULong; + case NumberType.Double: + return DType.Double; + case NumberType.Real: + return DType.Real; + } + } override SourceRange sourceRange() { return range; } + Number number; + char[] name; private SourceRange range; }
--- a/basic/LiteralParsing.d Sun May 25 18:24:16 2008 +0200 +++ b/basic/LiteralParsing.d Sun May 25 19:13:07 2008 +0200 @@ -1,4 +1,4 @@ -module basic.LiteralParsing.d; +module basic.LiteralParsing; import basic.SourceLocation, basic.Message,
--- a/gen/CodeGen.d Sun May 25 18:24:16 2008 +0200 +++ b/gen/CodeGen.d Sun May 25 19:13:07 2008 +0200 @@ -11,7 +11,8 @@ ast.Exp, ast.Module : DModule = Module; -import basic.SmallArray; +import basic.SmallArray, + basic.LiteralParsing; import lexer.Token; @@ -306,9 +307,22 @@ case ExpType.Binary: return RValue(genBinExp(cast(BinaryExp)exp)); case ExpType.IntegerLit: - auto integetLit = cast(IntegerLit)exp; - auto val = Integer.parse(integetLit.get); - return RValue(ConstantInt.GetS(Type.Int32, val)); + auto integerLit = cast(IntegerLit)exp; + switch(integerLit.number.type) + { + case NumberType.Int: + return RValue(ConstantInt.GetS(Type.Int32, integerLit.number.integer)); + case NumberType.Long: + return RValue(ConstantInt.GetS(Type.Int64, integerLit.number.integer)); + case NumberType.ULong: + return RValue(ConstantInt.GetS(Type.Int64, integerLit.number.integer)); + case NumberType.Float: + return RValue(ConstantReal.Get(Type.Float, integerLit.number.floating)); + case NumberType.Double: + return RValue(ConstantReal.Get(Type.Double, integerLit.number.floating)); + case NumberType.Real: + return RValue(ConstantReal.Get(Type.X86_FP80, integerLit.number.floating)); + } case ExpType.Negate: auto negateExp = cast(NegateExp)exp; auto target = genExpression(negateExp.exp); @@ -345,14 +359,10 @@ auto castExp = cast(CastExp)exp; auto value = genExpression(castExp.exp).value; - if (!castExp.type.hasImplicitConversionTo(castExp.type)) + if (!castExp.exp.type.hasImplicitConversionTo(castExp.type)) assert(0, "Invalid cast"); - Value v; - if(castExp.exp.type.byteSize <= castExp.type.byteSize) - v = b.buildZExt(value, llvm(castExp.type), "zext"); - else - v = b.buildTrunc(value, llvm(castExp.type), "trunc"); + Value v = buildCastLoad(castExp.exp.type, castExp.type, value); return RValue(v); @@ -820,6 +830,25 @@ assert(0, "Only integers, structs and functions are supported"); } + Value buildCastLoad(DType from, DType to, Value value) + { + if(typeof(from) == DInteger) + { + if(to == Type.Double) + return b.buildFPTrunc(value, llvm(_to), "fptrunc"); + if(to == Type.Float) + return b.buildFPTrunc(value, llvm(_to), "fptrunc"); + } + /* + if(from == Type.Int64) + if(castExp.exp.type.byteSize <= castExp.type.byteSize) + v = b.buildZExt(value, llvm(castExp.type), "zext"); + else + v = b.buildTrunc(value, llvm(castExp.type), "trunc"); + } +*/ + assert(0, "implicit cast need implimentation"); + } // Might as well insert all the basic types from the start void createBasicTypes() {
--- a/sema/DType.d Sun May 25 18:24:16 2008 +0200 +++ b/sema/DType.d Sun May 25 19:13:07 2008 +0200 @@ -268,6 +268,15 @@ super(name, bits, false); } + override bool hasImplicitConversionTo(DType that) + { + if (auto o = cast(DInteger)that) + return true; + if (auto o = cast(DReal)that) + return true; + return false; + } + override bool isReal() { return true; } override DReal asReal() { return this; } }
--- a/sema/LiteralInterpreter.d Sun May 25 18:24:16 2008 +0200 +++ b/sema/LiteralInterpreter.d Sun May 25 19:13:07 2008 +0200 @@ -25,7 +25,7 @@ void visitIntegerLit(IntegerLit exp) { - auto type = parseNumber(exp.name, exp.loc, messages); + exp.number = parseNumber(exp.name, exp.loc, messages); } MessageHandler messages;
--- a/sema/ScopeBuilder.d Sun May 25 18:24:16 2008 +0200 +++ b/sema/ScopeBuilder.d Sun May 25 19:13:07 2008 +0200 @@ -106,6 +106,10 @@ table[table.length-1].types["wchar"] = DType.WChar; table[table.length-1].types["dchar"] = DType.DChar; + table[table.length-1].types["float"] = DType.Float; + table[table.length-1].types["double"] = DType.Double; + table[table.length-1].types["real"] = DType.Real; + current().inModule = m; current().mHandle = mHandle; mHandle.add(m);