Skip to content

Commit

Permalink
Merge branch 'devel' into explicit-generic-unresolved
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn authored Nov 4, 2024
2 parents 286bba1 + f98964d commit c932c01
Show file tree
Hide file tree
Showing 18 changed files with 595 additions and 445 deletions.
68 changes: 40 additions & 28 deletions compiler/cbuilderbase.nim
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
import ropes, int128

type
Snippet = string
Builder = string
Snippet* = string
Builder* = object
buf*: string

template newBuilder*(s: string): Builder =
Builder(buf: s)

proc extract*(builder: Builder): Snippet =
builder.buf

proc add*(builder: var Builder, s: string) =
builder.buf.add(s)

template newBuilder(s: string): Builder =
s
proc add*(builder: var Builder, s: char) =
builder.buf.add(s)

proc addIntValue(builder: var Builder, val: int) =
builder.addInt(val)
proc addIntValue*(builder: var Builder, val: int) =
builder.buf.addInt(val)

proc addIntValue(builder: var Builder, val: int64) =
builder.addInt(val)
proc addIntValue*(builder: var Builder, val: int64) =
builder.buf.addInt(val)

proc addIntValue(builder: var Builder, val: uint64) =
builder.addInt(val)
proc addIntValue*(builder: var Builder, val: uint64) =
builder.buf.addInt(val)

proc addIntValue(builder: var Builder, val: Int128) =
builder.addInt128(val)
proc addIntValue*(builder: var Builder, val: Int128) =
builder.buf.addInt128(val)

template cIntValue(val: int): Snippet = $val
template cIntValue(val: int64): Snippet = $val
template cIntValue(val: uint64): Snippet = $val
template cIntValue(val: Int128): Snippet = $val
template cIntValue*(val: int): Snippet = $val
template cIntValue*(val: int64): Snippet = $val
template cIntValue*(val: uint64): Snippet = $val
template cIntValue*(val: Int128): Snippet = $val

import std/formatfloat

proc addFloatValue(builder: var Builder, val: float) =
builder.addFloat(val)
proc addFloatValue*(builder: var Builder, val: float) =
builder.buf.addFloat(val)

template cFloatValue(val: float): Snippet = $val
template cFloatValue*(val: float): Snippet = $val

proc int64Literal(i: BiggestInt; result: var Builder) =
proc addInt64Literal*(result: var Builder; i: BiggestInt) =
if i > low(int64):
result.add "IL64($1)" % [rope(i)]
else:
result.add "(IL64(-9223372036854775807) - IL64(1))"

proc uint64Literal(i: uint64; result: var Builder) =
proc addUint64Literal*(result: var Builder; i: uint64) =
result.add rope($i & "ULL")

proc intLiteral(i: BiggestInt; result: var Builder) =
proc addIntLiteral*(result: var Builder; i: BiggestInt) =
if i > low(int32) and i <= high(int32):
result.addIntValue(i)
elif i == low(int32):
Expand All @@ -49,19 +61,19 @@ proc intLiteral(i: BiggestInt; result: var Builder) =
else:
result.add "(IL64(-9223372036854775807) - IL64(1))"

proc intLiteral(i: Int128; result: var Builder) =
intLiteral(toInt64(i), result)
proc addIntLiteral*(result: var Builder; i: Int128) =
addIntLiteral(result, toInt64(i))

proc cInt64Literal(i: BiggestInt): Snippet =
proc cInt64Literal*(i: BiggestInt): Snippet =
if i > low(int64):
result = "IL64($1)" % [rope(i)]
else:
result = "(IL64(-9223372036854775807) - IL64(1))"

proc cUint64Literal(i: uint64): Snippet =
proc cUint64Literal*(i: uint64): Snippet =
result = $i & "ULL"

proc cIntLiteral(i: BiggestInt): Snippet =
proc cIntLiteral*(i: BiggestInt): Snippet =
if i > low(int32) and i <= high(int32):
result = rope(i)
elif i == low(int32):
Expand All @@ -72,5 +84,5 @@ proc cIntLiteral(i: BiggestInt): Snippet =
else:
result = "(IL64(-9223372036854775807) - IL64(1))"

proc cIntLiteral(i: Int128): Snippet =
proc cIntLiteral*(i: Int128): Snippet =
result = cIntLiteral(toInt64(i))
20 changes: 16 additions & 4 deletions compiler/cbuilderdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ template addVarWithType(builder: var Builder, kind: VarKind = Local, name: strin
builder.add(name)
builder.add(";\n")

template addVarWithInitializer(builder: var Builder, kind: VarKind = Local, name: string,
typ: Snippet, initializerBody: typed) =
## adds a variable declaration to the builder, with
## `initializerBody` building the initializer. initializer must be provided
builder.addVarHeader(kind)
builder.add(typ)
builder.add(" ")
builder.add(name)
builder.add(" = ")
initializerBody
builder.add(";\n")

template addVarWithTypeAndInitializer(builder: var Builder, kind: VarKind = Local, name: string,
typeBody, initializerBody: typed) =
## adds a variable declaration to the builder, with `typeBody` building the type, and
Expand Down Expand Up @@ -252,12 +264,12 @@ proc startSimpleStruct(obj: var Builder; m: BModule; name: string; baseType: Sni
obj.add(baseType)
obj.add(" ")
obj.add("{\n")
result.preFieldsLen = obj.len
result.preFieldsLen = obj.buf.len
if result.baseKind == bcSupField:
obj.addField(name = "Sup", typ = baseType)

proc finishSimpleStruct(obj: var Builder; m: BModule; info: StructBuilderInfo) =
if info.baseKind == bcNone and info.preFieldsLen == obj.len:
if info.baseKind == bcNone and info.preFieldsLen == obj.buf.len:
# no fields were added, add dummy field
obj.addField(name = "dummy", typ = "char")
if info.named:
Expand Down Expand Up @@ -308,7 +320,7 @@ proc startStruct(obj: var Builder; m: BModule; t: PType; name: string; baseType:
obj.add(baseType)
obj.add(" ")
obj.add("{\n")
result.preFieldsLen = obj.len
result.preFieldsLen = obj.buf.len
case result.baseKind
of bcNone:
# rest of the options add a field or don't need it due to inheritance,
Expand All @@ -328,7 +340,7 @@ proc startStruct(obj: var Builder; m: BModule; t: PType; name: string; baseType:
obj.addField(name = "Sup", typ = baseType)

proc finishStruct(obj: var Builder; m: BModule; t: PType; info: StructBuilderInfo) =
if info.baseKind == bcNone and info.preFieldsLen == obj.len and
if info.baseKind == bcNone and info.preFieldsLen == obj.buf.len and
t.itemId notin m.g.graph.memberProcsPerType:
# no fields were added, add dummy field
obj.addField(name = "dummy", typ = "char")
Expand Down
26 changes: 26 additions & 0 deletions compiler/cbuilderstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ template addElseBranch(builder: var Builder, stmt: var IfStmt, body: typed) =
body
builder.add("}")

proc addForRangeHeader(builder: var Builder, i, start, bound: Snippet, inclusive: bool = false) =
builder.add("for (")
builder.add(i)
builder.add(" = ")
builder.add(start)
builder.add("; ")
builder.add(i)
if inclusive:
builder.add(" <= ")
else:
builder.add(" < ")
builder.add(bound)
builder.add("; ")
builder.add(i)
builder.add("++) {\n")

template addForRangeExclusive(builder: var Builder, i, start, bound: Snippet, body: typed) =
addForRangeHeader(builder, i, start, bound, false)
body
builder.add("}\n")

template addForRangeInclusive(builder: var Builder, i, start, bound: Snippet, body: typed) =
addForRangeHeader(builder, i, start, bound, true)
body
builder.add("}\n")

template addScope(builder: var Builder, body: typed) =
builder.add("{")
body
Expand Down
3 changes: 1 addition & 2 deletions compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ proc genOpenArraySlice(p: BProc; q: PNode; formalType, destType: PType; prepareF
result = ("($3*)(($1)+($2))" % [rdLoc(a), rdLoc(b), dest],
lengthExpr)
else:
var lit = newRopeAppender()
intLiteral(first, lit)
let lit = cIntLiteral(first)
result = ("($4*)($1)+(($2)-($3))" %
[rdLoc(a), rdLoc(b), lit, dest],
lengthExpr)
Expand Down
Loading

0 comments on commit c932c01

Please sign in to comment.