Mercurial > projects > dang
changeset 85:9375bd975730 new_gen
Allow indexing of pointers also
author | Anders Halager <halager@gmail.com> |
---|---|
date | Fri, 02 May 2008 20:18:50 +0200 |
parents | cc05c041e6a3 |
children | 29f486ccc203 |
files | ast/Exp.d gen/CodeGen.d |
diffstat | 2 files changed, 18 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/ast/Exp.d Fri May 02 20:17:22 2008 +0200 +++ b/ast/Exp.d Fri May 02 20:18:50 2008 +0200 @@ -317,9 +317,12 @@ override DType type() { - assert(target.type().isArray(), "Can only index arrays"); - DArray array = target.type().asArray(); - return array.arrayOf; + DType type = target.type(); + if (type.isArray()) + return type.asArray().arrayOf; + else if (type.isPointer()) + return type.asPointer().pointerOf; + else assert(0, "Can only index pointers and arrays"); } Exp simplify()
--- a/gen/CodeGen.d Fri May 02 20:17:22 2008 +0200 +++ b/gen/CodeGen.d Fri May 02 20:18:50 2008 +0200 @@ -503,12 +503,22 @@ return b.buildLoad(target, "deref"); case ExpType.Index: auto indexExp = cast(IndexExp)exp; - auto array = getPointer(indexExp.target); + auto type = indexExp.target.type; auto index = genExpression(indexExp.index); Value[2] gep_indices; gep_indices[0] = ConstantInt.Get(IntegerType.Int32, 0, false); gep_indices[1] = index; - return b.buildGEP(array, gep_indices[], "index"); + if (type.isArray()) + { + auto array = getPointer(indexExp.target); + return b.buildGEP(array, gep_indices[0 .. 2], "index"); + } + else if (type.isPointer()) + { + auto array = genExpression(indexExp.target); + return b.buildGEP(array, gep_indices[1 .. 2], "index"); + } + else assert(0, "Can only index pointers and arrays"); case ExpType.MemberReference: auto mem = cast(MemberReference)exp; Stdout(mem.target).newline;