From 9ef367db62e6b0acff8d6c845375830080d0d77d Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 31 Dec 2012 17:53:37 +0100 Subject: [PATCH] fixed a newly introduced c2nim bug; many untested improvements to the FFI at compile time --- compiler/c2nim/cpp.nim | 2 + compiler/c2nim/tests/systest.c | 2 + compiler/evalffi.nim | 230 +++++++++++++++++++++++++++------ compiler/evals.nim | 29 ++++- compiler/msgs.nim | 3 + todo.txt | 6 +- 6 files changed, 225 insertions(+), 47 deletions(-) diff --git a/compiler/c2nim/cpp.nim b/compiler/c2nim/cpp.nim index 3b7f58fcc8..c210eca3a1 100755 --- a/compiler/c2nim/cpp.nim +++ b/compiler/c2nim/cpp.nim @@ -42,6 +42,7 @@ proc parseDefine(p: var TParser): PNode = result = newNodeP(nkTemplateDef, p) getTok(p) addSon(result, skipIdentExport(p)) + addSon(result, ast.emptyNode) eat(p, pxParLe) var params = newNodeP(nkFormalParams, p) # return type; not known yet: @@ -60,6 +61,7 @@ proc parseDefine(p: var TParser): PNode = addSon(result, ast.emptyNode) # no generic parameters addSon(result, params) addSon(result, ast.emptyNode) # no pragmas + addSon(result, ast.emptyNode) var kind = parseDefineBody(p, result) params.sons[0] = newIdentNodeP(kind, p) eatNewLine(p, result) diff --git a/compiler/c2nim/tests/systest.c b/compiler/c2nim/tests/systest.c index 241526e077..2a9dd6c287 100755 --- a/compiler/c2nim/tests/systest.c +++ b/compiler/c2nim/tests/systest.c @@ -17,6 +17,8 @@ int aw_instance_callback_set (AW_CALLBACK c, callback_t callback); unsigned long int wawa; +#define MAX(x, y) ((x) < (y)? (y) : (x)) + #define AW_BUILD 85 // AW 5.0 // Limits #define AW_MAX_AVCHANGE_PER_SECOND 10 diff --git a/compiler/evalffi.nim b/compiler/evalffi.nim index 5a2f71042a..8f708b76ca 100644 --- a/compiler/evalffi.nim +++ b/compiler/evalffi.nim @@ -31,7 +31,7 @@ proc getDll(cache: var TDllCache; dll: string; info: TLineInfo): pointer = result = LoadLib(c) if not result.isNil: break if result.isNil: - GlobalError(info, errGenerated, "cannot load: " & dll) + GlobalError(info, "cannot load: " & dll) cache[dll] = result const @@ -50,8 +50,7 @@ proc importcSymbol*(sym: PSym): PNode = else: let lib = sym.annex if lib != nil and lib.path.kind notin {nkStrLit..nkTripleStrLit}: - GlobalError(sym.info, errGenerated, - "dynlib needs to be a string lit for the REPL") + GlobalError(sym.info, "dynlib needs to be a string lit for the REPL") var theAddr: pointer if lib.isNil and not gExehandle.isNil: # first try this exe itself: @@ -75,8 +74,7 @@ proc mapType(t: ast.PType): ptr libffi.TType = of 2: result = addr libffi.type_sint16 of 4: result = addr libffi.type_sint32 of 8: result = addr libffi.type_sint64 - else: - InternalError("cannot map type to FFI") + else: result = nil of tyFloat, tyFloat64: result = addr libffi.type_double of tyFloat32: result = addr libffi.type_float of tyVar, tyPointer, tyPtr, tyRef, tyCString, tySequence, tyString, tyExpr, @@ -85,22 +83,80 @@ proc mapType(t: ast.PType): ptr libffi.TType = of tyDistinct: result = mapType(t.sons[0]) else: - InternalError("cannot map type to FFI") + result = nil # too risky: #of tyFloat128: result = addr libffi.type_longdouble -proc mapCallConv(cc: TCallingConvention): TABI = +proc mapCallConv(cc: TCallingConvention, info: TLineInfo): TABI = case cc of ccDefault: result = DEFAULT_ABI of ccStdCall: result = when defined(windows): STDCALL else: DEFAULT_ABI of ccCDecl: result = DEFAULT_ABI - else: InternalError("cannot map calling convention to FFI") + else: + GlobalError(info, "cannot map calling convention to FFI") template rd(T, p: expr): expr {.immediate.} = (cast[ptr T](p))[] template wr(T, p, v: expr) {.immediate.} = (cast[ptr T](p))[] = v template `+!`(x, y: expr): expr {.immediate.} = cast[pointer](cast[TAddress](x) + y) +proc packSize(v: PNode, typ: PType): int = + ## computes the size of the blob + case typ.kind + of tyPtr, tyRef, tyVar: + if v.kind in {nkNilLit, nkPtrLit}: + result = sizeof(pointer) + else: + result = sizeof(pointer) + packSize(v.sons[0], typ.sons[0]) + of tyDistinct, tyGenericInst: + result = packSize(v, typ.sons[0]) + of tyArray, tyArrayConstr: + # consider: ptr array[0..1000_000, int] which is common for interfacing; + # we use the real length here instead + if v.kind in {nkNilLit, nkPtrLit}: + result = sizeof(pointer) + elif v.len != 0: + result = v.len * packSize(v.sons[0], typ.sons[1]) + else: + result = typ.getSize.int + +proc pack(v: PNode, typ: PType, res: pointer) + +proc getField(n: PNode; position: int): PSym = + case n.kind + of nkRecList: + for i in countup(0, sonsLen(n) - 1): + result = getField(n.sons[i], position) + if result != nil: return + of nkRecCase: + result = getField(n.sons[0], position) + if result != nil: return + for i in countup(1, sonsLen(n) - 1): + case n.sons[i].kind + of nkOfBranch, nkElse: + result = getField(lastSon(n.sons[i]), position) + if result != nil: return + else: internalError(n.info, "getField(record case branch)") + of nkSym: + if n.sym.position == position: result = n.sym + else: nil + +proc packObject(x: PNode, typ: PType, res: pointer) = + InternalAssert x.kind == nkPar + # compute the field's offsets: + discard typ.getSize + for i in countup(0, sonsLen(x) - 1): + var it = x.sons[i] + if it.kind == nkExprColonExpr: + internalAssert it.sons[0].kind == nkSym + let field = it.sons[0].sym + pack(it.sons[1], field.typ, res +! field.offset) + elif typ.n != nil: + let field = getField(typ.n, i) + pack(it, field.typ, res +! field.offset) + else: + GlobalError(x.info, "cannot pack unnamed tuple") + proc pack(v: PNode, typ: PType, res: pointer) = template awr(T, v: expr) {.immediate, dirty.} = wr(T, res, v) @@ -125,8 +181,7 @@ proc pack(v: PNode, typ: PType, res: pointer) = of 4: awr(int32, v.intVal.int32) of 8: awr(int64, v.intVal.int64) else: - GlobalError(v.info, errGenerated, - "cannot map value to FFI (tyEnum, tySet)") + GlobalError(v.info, "cannot map value to FFI (tyEnum, tySet)") of tyFloat: awr(float, v.floatVal) of tyFloat32: awr(float32, v.floatVal) of tyFloat64: awr(float64, v.floatVal) @@ -138,7 +193,7 @@ proc pack(v: PNode, typ: PType, res: pointer) = elif v.kind == nkPtrLit: awr(pointer, cast[pointer](v.intVal)) else: - GlobalError(v.info, errGenerated, "cannot map pointer/proc value to FFI") + GlobalError(v.info, "cannot map pointer/proc value to FFI") of tyPtr, tyRef, tyVar: if v.kind == nkNilLit: # nothing to do since the memory is 0 initialized anyway @@ -146,33 +201,106 @@ proc pack(v: PNode, typ: PType, res: pointer) = elif v.kind == nkPtrLit: awr(pointer, cast[pointer](v.intVal)) else: - # XXX this is pretty hard: we need to allocate a new buffer and store it - # somewhere to be freed; we also need to write back any changes to the - # data! - GlobalError(v.info, errGenerated, "cannot map pointer/proc value to FFI") + pack(v.sons[0], typ.sons[0], res +! sizeof(pointer)) + awr(pointer, res +! sizeof(pointer)) of tyCString, tyString: if v.kind == nkNilLit: nil - else: + elif v.kind in {nkStrLit..nkTripleStrLit}: awr(cstring, cstring(v.strVal)) + else: + GlobalError(v.info, "cannot map string value to FFI") of tyArray, tyArrayConstr: let baseSize = typ.sons[1].getSize - assert(v.len == lengthOrd(typ.sons[0])) for i in 0 ..