Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions languages/lang0.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ conv ::= (Reinterp <type> <type> <expr>)

memops ::= (Load <type> <expr>)

call ::= (Call <proc> <expr>*)
| (Call <type_id> <expr>+)
call ::= (Call <type_id> <expr>+)

intVal ::= (IntVal <int>)
floatVal ::= (FloatVal <float>)

simple ::= <intVal>
| <floatVal>
| (ProcVal <int>)
| <proc>
| (Copy <local>)
| (Copy (Global <int>))
expr ::= <simple>
Expand All @@ -89,9 +88,7 @@ exit ::= <goto>
| (Raise <expr> <err_goto>)
| (Branch <expr> false:<goto> true:<goto>)
| (Select <type> <simple> <choice>+)
| (CheckedCall <proc> <expr>* <goto> <err_goto>)
| (CheckedCall <type_id> <expr>+ <goto> <err_goto>)
| (CheckedCallAsgn <local> <proc> <expr>* <goto> <err_goto>)
| (CheckedCallAsgn <local> <type_id> <expr>+ <goto> <err_goto>)

stmt ::= (Asgn <local> <expr>)
Expand Down
7 changes: 2 additions & 5 deletions languages/lang25.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ procdef += (ProcDef <type_id> (Params <local>*) (Locals <type>*) (Stmts <stmt>+)
Control-flow statements can now appear in a normal statement context:

```grammar
exit -= (CheckedCall <proc> <expr>* <goto> <err_goto>)
| (CheckedCall <type_id> <expr>+ <goto> <err_goto>)
exit -= (CheckedCall <type_id> <expr>+ <goto> <err_goto>)

exit += (CheckedCall <proc> <expr>* <err_goto>)
| (CheckedCall <type_id> <expr>+ <err_goto>)
| (CheckedCallAsgn <local> <proc> <expr>* <err_goto>)
exit += (CheckedCall <type_id> <expr>+ <err_goto>)
| (CheckedCallAsgn <local> <type_id> <expr>+ <err_goto>)

stmt += <goto>
Expand Down
1 change: 0 additions & 1 deletion languages/lang30.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ stmt += (Block <single_stmt>)
| (Loop <single_stmt>)
| (If <expr> <single_stmt> <single_stmt>?)
| (Case <type_id> <simple> <choice>+)
| (CheckedCall <proc> <expr>*)
| (CheckedCall <type_id> <expr>+)
| (CheckedCallAsgn <local> <proc> <expr>*)
| (CheckedCallAsgn <local> <type_id> <expr>+)
Expand Down
2 changes: 0 additions & 2 deletions passes/ir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ proc use*(n: IrNode, lit: var Literals, bu: var Builder[NodeKind]) =
bu.subTree Load:
convert(n[0], lit, bu)
use(n[1], lit, bu)
of Proc:
bu.add Node(kind: ProcVal, val: n.id)
else:
convert(n, lit, bu)

Expand Down
10 changes: 5 additions & 5 deletions passes/pass0.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func imm(n: TreeNode[NodeKind]): int32 {.inline.} =
cast[int32](n.val)

func id(n: TreeNode[NodeKind]): int32 {.inline.} =
assert n.kind in {Local, Global, Proc, ProcVal}
assert n.kind in {Local, Global, Proc}
cast[int32](n.val)

func typ(n: TreeNode[NodeKind]): TypeId {.inline.} =
Expand Down Expand Up @@ -159,12 +159,12 @@ proc genCall(c; tree; call: NodeIndex,
template numArgs(bias: int): int16 =
int16((tree.len(call) - ord(fin)) - start - bias)

if tree[call, start].kind == Proc:
if tree[call, start + 1].kind == Proc:
# it's a static call
for it in tree.items(call, start + 1, fin):
for it in tree.items(call, start + 2, fin):
c.genExpr(tree, it)

c.instr(opcCall, tree[call, start].id, numArgs(0))
c.instr(opcCall, tree[call, start + 1].id, numArgs(1))
else:
# it's an indirect call
for it in tree.items(call, start + 2, fin):
Expand Down Expand Up @@ -222,7 +222,7 @@ proc genExpr(c; tree; val: NodeIndex) =
c.loadInt(tree.getInt(val))
of FloatVal:
c.loadFloat(tree.getFloat(val))
of ProcVal:
of Proc:
# turn into a procedure address by adding 1
c.loadInt(tree[val].id + 1)
of Copy:
Expand Down
33 changes: 11 additions & 22 deletions passes/pass_aggregateParams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ type

Context = object
addrType: Node
cache: seq[NodeIndex]
## indexed by procedure IDs. Caches the signature type for every
## procedure. This greatly speeds up type computation of calls, as it
## eliminates the tree traversal necessary for seeking to the procdef

# per-procedure state:
locals: NodeIndex
Expand Down Expand Up @@ -70,11 +66,9 @@ proc isAggregate(tree; n: Node): bool =
n = tree[tree.child(0), n.val]
n.kind in {Record, Union, Array}

proc signature(c; tree; n): NodeIndex =
case tree[n].kind
of Type: tree.child(tree.child(0), tree[n].val)
of Proc: c.cache[tree[n].val]
else: unreachable()
proc signature(tree; n): NodeIndex =
## Returns the index of the `ProcTy` node for the call at `n`.
tree.child(tree.child(0), tree[n].val)

proc typeof(c; tree; n): Node =
case tree[n].kind
Expand All @@ -92,7 +86,7 @@ proc typeof(c; tree; n): Node =
tree[c.locals, tree[n].val]
of Call:
# fetch the return type from the callee's signature
tree[c.signature(tree, tree.child(n, 0)), 0]
tree[signature(tree, tree.child(n, 0)), 0]
of Global:
# fetch the type from the ``GlobalDef``
tree[tree.child(tree.child(1), tree[n].val), 0]
Expand Down Expand Up @@ -167,7 +161,7 @@ proc lowerOperand(c; tree; n; reference: int, bu) =
# code and/or more work for later passes (more locals usually equals more
# work and bookkeeping).
case tree[n].kind
of IntVal, FloatVal, ProcVal:
of IntVal, FloatVal, NodeKind.Proc:
discard "nothing to do"
of Addr:
c.lowerAddr(tree, n, reference, bu)
Expand All @@ -180,8 +174,7 @@ proc lowerOperand(c; tree; n; reference: int, bu) =
c.lowerExpr(tree, n, bu)

proc lowerCallArgs(c; tree; n; start: int, last: BackwardsIndex; bu) =
let sig = c.signature(tree, tree.child(n, start))
assert tree[sig].kind == ProcTy
let sig = signature(tree, tree.child(n, start))
let reference = c.stmts.len

# don't consider the dynamic callee value, if any, during the loop, as it's
Expand Down Expand Up @@ -219,7 +212,7 @@ proc lowerCallArgs(c; tree; n; start: int, last: BackwardsIndex; bu) =

proc lowerExpr(c; tree; n, bu) =
case tree[n].kind
of IntVal, FloatVal, ProcVal:
of IntVal, FloatVal, NodeKind.Proc:
discard "nothing to do"
of Copy:
let x = tree.child(n, 0)
Expand Down Expand Up @@ -351,11 +344,12 @@ proc addStmts(c; tree; m: var TreeRef, at: NodeIndex, bu) =
bu.insert m, at, c.stmts[i]
c.stmts.shrink(0)

proc lowerProc(c; tree; n; sig: NodeIndex, bu) =
proc lowerProc(c; tree; n; bu) =
c.temps.shrink(0)
c.params.clear()

let (_, locals, blocks) = tree.triplet(n)
let (typ, locals, blocks) = tree.triplet(n)
let sig = tree.child(tree.child(0), tree[typ].val)
c.locals = locals
c.firstTemp = tree.len(locals)

Expand Down Expand Up @@ -425,12 +419,7 @@ proc lower*(tree; ptrSize: int): ChangeSet[NodeKind] =

let procs = tree.child(2)

# cache the signature type for each procedure:
c.cache.newSeq(tree.len(procs))
for i, it in tree.pairs(procs):
c.cache[i] = tree.child(tree.child(0), tree[it, 0].val.int)

# lower the procedures:
for i, it in tree.pairs(procs):
if tree[it].kind == ProcDef:
c.lowerProc(tree, it, c.cache[i], result)
c.lowerProc(tree, it, result)
10 changes: 2 additions & 8 deletions passes/source2il.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,8 @@ proc userCallToIL(c; t; n: NodeIndex, expr; stmts): SemType =
stmts.add callee.stmts

var call = IrNode(kind: Call)
if callee.expr.kind == Proc:
# the callee is a statically-known procedure; it's a static call
call.add callee.expr
else:
call.add IrNode(kind: Type, id: c.genProcType(callee.typ))
call.add callee.expr
call.add IrNode(kind: Type, id: c.genProcType(callee.typ))
call.add callee.expr

call.addArgs(c, t, n, callee.typ, stmts)

Expand Down Expand Up @@ -1189,8 +1185,6 @@ proc exprToIL(c; t: InTree, n: NodeIndex, expr, stmts): ExprType =
expr = newLocal(ent.id.uint32)
result = c.locals[ent.id] + {}
of ekProc:
# expand to a procedure address (`ProcVal`), which is always correct;
# the callsite can turn it into a static reference (`Proc`) as needed
expr = IrNode(kind: Proc, id: ent.id.uint32)
result = c.procList[ent.id].typ + {}
of ekNone:
Expand Down
5 changes: 2 additions & 3 deletions passes/syntax.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import

type
NodeKind* = enum
Immediate, IntVal, FloatVal, StringVal, ProcVal, Proc, Type, Local, Global
Immediate, IntVal, FloatVal, StringVal, Proc, Type, Local, Global
Int, UInt, Float

List
Expand Down Expand Up @@ -56,7 +56,6 @@ proc toSexp*(tree: PackedTree[NodeKind], idx: NodeIndex,
of IntVal: sexp([newSSymbol("IntVal"), sexp tree.getInt(idx)])
of FloatVal: sexp([newSSymbol("FloatVal"), sexp tree.getFloat(idx)])
of StringVal: sexp([newSSymbol("StringVal"), sexp tree.getString(idx)])
of ProcVal: sexp([newSSymbol("ProcVal"), sexp n.val.int])
of Proc: sexp([newSSymbol("Proc"), sexp n.val.int])
of Type: sexp([newSSymbol("Type"), sexp n.val.int])
of Local: sexp([newSSymbol("Local"), sexp n.val.int])
Expand All @@ -73,7 +72,7 @@ proc fromSexp*(kind: NodeKind, val: BiggestInt, lit): Node =
case kind
of IntVal:
Node(kind: kind, val: lit.pack(val))
of ProcVal, Proc, Type, Local, Global, Int, UInt, Float:
of Proc, Type, Local, Global, Int, UInt, Float:
Node(kind: kind, val: val.uint32)
else:
unreachable()
Expand Down
Loading