Skip to content

Commit

Permalink
proc types can be aliased
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Jul 15, 2024
1 parent b745476 commit c19d425
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/nifc/codegen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type
ReturnKeyword = "return "
TypedefStruct = "typedef struct "
TypedefUnion = "typedef union "
TypedefKeyword = "typedef "
IncludeKeyword = "#include "

proc fillTokenTable(tab: var BiTable[Token, string]) =
Expand Down
13 changes: 0 additions & 13 deletions src/nifc/cprelude.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,5 @@ typedef NU8 NU;
# endif
#endif
#define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
#define nimAddInt64(a, b, L) ({long long int res; if(__builtin_saddll_overflow(a, b, &res)) goto L; res})
#define nimSubInt64(a, b, L) ({long long int res; if(__builtin_ssubll_overflow(a, b, &res)) goto L; res})
#define nimMulInt64(a, b, L) ({long long int res; if(__builtin_smulll_overflow(a, b, &res)) goto L; res})
#define nimAddInt32(a, b, L) ({long int res; if(__builtin_sadd_overflow(a, b, &res)) goto L; res})
#define nimSubInt32(a, b, L) ({long int res; if(__builtin_ssub_overflow(a, b, &res)) goto L; res})
#define nimMulInt32(a, b, L) ({long int res; if(__builtin_smul_overflow(a, b, &res)) goto L; res})
#define nimCheckRange(x, a, b, L) ({if (x < a || x > b) goto L; x})
#define nimCheckIndex(x, a, L) ({if (x >= a) goto L; x})
"""

23 changes: 18 additions & 5 deletions src/nifc/gentypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ proc traverseTypes(m: Module; o: var TypeOrder) =
of ArrayC:
traverseObjectBody m, o, t
o.ordered.add ch, TypedefStruct
of ProctypeC:
# XXX
#traverseProctypeBody m, o, t
o.ordered.add ch, TypedefKeyword
of EnumC:
o.ordered.add ch, TypedefKeyword
else: discard

template integralBits(types: TypeGraph; t: TypeId): string =
Expand Down Expand Up @@ -235,22 +241,29 @@ proc generateTypes(c: var GeneratedCode; types: TypeGraph; o: TypeOrder) =
let litId = types[decl.name].litId
if not c.generatedTypes.containsOrIncl(litId.int):
let s = mangle(c.m.lits.strings[litId])
c.add declKeyword
c.add CurlyLe
case types[decl.body].kind
of ArrayC:
c.add declKeyword
c.add CurlyLe
let (elem, size) = sons2(types, decl.body)
genType c, types, elem, "a"
c.add BracketLe
c.genIntLit types[size].litId
c.add BracketRi
c.add Semicolon
of EnumC, ProctypeC:
c.add CurlyRi
c.add s
of EnumC:
assert false, "too implement"
of ProctypeC:
c.add TypedefKeyword
genType c, types, decl.body, s
else:
c.add declKeyword
c.add CurlyLe
# XXX generate attributes and pragmas here
c.genObjectOrUnionBody types, decl.body
c.add CurlyRi
c.add s
c.add CurlyRi
c.add s
c.add Semicolon

1 change: 1 addition & 0 deletions tests/nifc/hello.nif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(stmts
(incl "<stdio.h>")
(type :MyObject.c . (object . ))
(type :MyProc . (proctype . (params) (void) . ))
(nodecl (proc :printf.c (params (param :f.1 . (ptr FILE.c))) (void) (pragmas (varargs)) .))
(proc :main.c . (i M 90) . (stmts
(call printf.c "hello %s" "abc")
Expand Down

0 comments on commit c19d425

Please sign in to comment.