Skip to content

Commit

Permalink
Merge branch 'jtv/v2' into ee7/ci-add-tests-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Feb 22, 2024
2 parents f4d34d5 + 9443793 commit 9cefa7b
Show file tree
Hide file tree
Showing 15 changed files with 1,202 additions and 1,061 deletions.
136 changes: 91 additions & 45 deletions src/codegen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ proc genContainerIndex(ctx: CodeGenState, sym: SymbolInfo = nil,
else:
ctx.genTCall(FIndex)

proc genPushStaticString(ctx: CodeGenState, name: string) =
ctx.emitInstruction(ZPushStaticPtr, ctx.addStaticObject(name), tid = TString)
proc genPushStaticString(ctx: CodeGenState, s: string) =
ctx.emitInstruction(ZPushStaticPtr, ctx.addStaticObject(s), tid = TString)

proc genLoadAttr(ctx: CodeGenState, tid: TypeId, mode = 0) =
ctx.emitInstruction(ZLoadFromAttr, tid = tid, arg = mode)
Expand Down Expand Up @@ -1234,73 +1234,115 @@ proc genFunction(ctx: CodeGenState, fn: FuncInfo) =

ctx.mcur.objInfo.codesyms[zinfo.offset] = fullname

# Returns true if native.
proc backpatch_callback(ctx: CodeGenState, patchloc: int,
node: IrNode): (int32, bool) =
## m.instructions[offset] is ZPushVmPtr, which needs to be patched.
## next one is ZRunCallback
var
m = ctx.mcur
offset = patchloc
cb = cast[ptr Callback](node.value)
fnid: int32
native: bool

if cb.impl.externInfo != nil:
# TODO Shouldn't the offset already be in cb.impl?
# Not sure we need to loop; just copying from above.
native = false

for i, item in ctx.zobj.ffiInfo:
let
p = cast[pointer](addr ctx.zobj.staticData[item.nameOffset])
s = $(cast[cstring](p))

if s == cb.impl.externName:
let to = cb.tid.idToTypeRef()
fnid = int32(i)
m.objinfo.instructions[offset].arg = fnid
m.objinfo.instructions[offset].typeInfo = to.items[^1]
break
else:
fnid = int32(cb.impl.internalId)
native = true
m.objinfo.instructions[offset].arg = fnid

offset += 1
m.objinfo.instructions[offset].typeInfo = cb.tid

return (fnid, native)

proc stashParam(ctx: CodeGenState, m: Module, info: ParamInfo) =

var zparam = ZParamInfo(shortdoc: info.shortdoc,
longdoc: info.longdoc)

longdoc: info.longdoc,
private: info.private)
if info.sym.isAttr:
zparam.attr = info.sym.name
else:
zparam.offset = info.sym.offset

if info.defaultIr.isSome():
# TODO: do I need to check if this is constant? Don't remember
# TODO: I think may need to ensure const?
zparam.haveDefault = true
zparam.default = info.defaultIr.get().value
zparam.default = info.defaultIr.get().value

zparam.tid = info.sym.tid

if info.validatorIr.isSome():
if info.initializeIr.isSome():
var
offset = info.backpatchLoc
### m.instructions[offset] is ZPushVmPtr
### next one is ZRunCallback
litnode = info.validatorIr.get()
cb = cast[ptr Callback](litnode.value)

if cb.impl.externInfo != nil:
# TODO Shouldn't the offset already be in cb.impl?
# Not sure we need to loop; just copying from above.
zparam.native = false
for i, item in ctx.zobj.ffiInfo:
let
p = cast[pointer](addr ctx.zobj.staticData[item.nameOffset])
s = $(cast[cstring](p))
offset = info.iPatchLoc
litnode = info.initializeIr.get()

if s == cb.impl.externName:
zparam.funcIx = int32(i)
m.objinfo.instructions[offset].arg = zparam.funcIx
m.objinfo.instructions[offset].typeInfo = zparam.tid
break
else:
zparam.native = true
zparam.funcIx = int32(cb.impl.internalId)
m.objinfo.instructions[offset].arg = zparam.funcIx
(zparam.iFnIx, zparam.iNative) = ctx.backpatch_callback(offset, litnode)
else:
zparam.iFnIx = -1

offset += 1
m.objinfo.instructions[offset].typeInfo = cb.tid
if info.validatorIr.isSome():
var
offset = info.vPatchLoc
litnode = info.validatorIr.get()

(zparam.vFnIx, zparam.iNative) = ctx.backpatch_callback(offset, litnode)
else:
zparam.funcIx = -1

zparam.vFnIx = -1

m.objInfo.parameters.add(zparam)

proc placeholdParamValidation(ctx: CodeGenState, m: Module, i: int) =
m.params[i].backpatchLoc = m.objInfo.instructions.len()
ctx.emitInstruction(ZPushVmPtr)
ctx.emitInstruction(ZRunCallback, arg = 1)
ctx.emitInstruction(ZMoveSp, -1)
ctx.emitInstruction(ZPushRes)
proc emitBail(ctx: CodeGenState, msg: string) =
ctx.genPushStaticString(msg)
ctx.emitInstruction(ZBail)

proc genParamChecks(ctx: CodeGenState, m: Module) =
for i, param in m.params:
if param.validatorIr.isNone():
continue
ctx.genLoadSymbol(param.sym)
ctx.placeholdParamValidation(m, i)
ctx.emitInstruction(ZParamCheck, arg = i)
ctx.genPushTypeOf(param.sym)
let patchloc = m.objInfo.instructions.len()
ctx.startJnz()
ctx.emitBail("Parameter " & param.sym.name & " was not set when " &
"entering module " & m.modname)
ctx.backpatch(patchloc)

if param.validatorIr.isSome():
ctx.genLoadSymbol(param.sym)
m.params[i].vPatchLoc = m.objInfo.instructions.len()
ctx.emitInstruction(ZPushVmPtr) # Will overwrite this later.
ctx.emitInstruction(ZRunCallback, arg = 1)
ctx.emitInstruction(ZMoveSp, -1)
ctx.emitInstruction(ZPushRes)
ctx.emitInstruction(ZParamCheck, arg = i)

proc genInitializer(ctx: CodeGenState, p: ParamInfo) =
ctx.genPushTypeOf(p.sym)
let patchloc = ctx.mcur.objInfo.instructions.len()
ctx.startJnz()
# Not sure what kind of callback we're pushing yet.
# We'll come back to the next instruction we emit later.
p.iPatchloc = ctx.mcur.objInfo.instructions.len()
ctx.emitInstruction(ZPushVmPtr)
ctx.emitInstruction(ZRunCallback, arg = -1)
ctx.emitInstruction(ZPushRes)
ctx.genStore(p.sym)
ctx.backPatch(patchloc)

proc genModule(ctx: CodeGenState, m: Module) =
let curMod = ctx.minfo[m.key]
Expand All @@ -1315,6 +1357,10 @@ proc genModule(ctx: CodeGenState, m: Module) =
curMod.longdoc = m.longdoc

ctx.genLabel("Module '" & m.modname & "' :")
for param in m.params:
if param.initializeIr.isSome():
ctx.genInitializer(param)

ctx.emitInstruction(ZModuleEnter, arg = m.params.len())

if m.params.len() != 0:
Expand Down
8 changes: 4 additions & 4 deletions src/commands/objdump.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ proc get_basic_object_info*(obj: ZObjectFile): Rope =
proc format_module_params*(obj: ZObjectFile, params: seq[ZParamInfo]): Rope =
var cells: seq[seq[string]] = @[@["Attr or offset",
"Type",
"Native func?",
"Function index", "Short Doc", "Long Doc"]]
"Native Validator?",
"Validator index", "Short Doc", "Long Doc"]]
for item in params:
var row: seq[string] = @[]
if item.attr != "":
Expand All @@ -33,11 +33,11 @@ proc format_module_params*(obj: ZObjectFile, params: seq[ZParamInfo]): Rope =
echo "offset = ", item.offset
row.add($item.offset)
row.add(item.tid.toString())
if item.native:
if item.vnative:
row.add("")
else:
row.add("")
row.add($item.funcIx)
row.add($item.vfnIx)
row.add(item.shortdoc & " ")
row.add(item.longdoc & " ")
cells.add(row)
Expand Down
36 changes: 22 additions & 14 deletions src/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,29 @@ type
shortdoc*: string
longdoc*: string
validatorIr*: Option[IrNode]
initializeIr*: Option[IrNode]
defaultParse*: Option[ParseNode]
defaultIr*: Option[IrNode]
backpatchLoc*: int
private*: bool
iPatchLoc*: int
vPatchLoc*: int

ZParamInfo* = ref object
## What we stick in the obj file.
attr*: string # Either it's an attribute...
offset*: int # or in theerr current module.
default*: pointer
tid*: TypeId
haveDefault*: bool
native*: bool
funcIx*: int32
userparam*: pointer
userType*: TypeId
shortdoc*: string
longdoc*: string
attr*: string # Either it's an attribute...
offset*: int # or in theerr current module.
default*: pointer
tid*: TypeId
haveDefault*: bool
private*: bool
vFnIx*: int32
vNative*: bool # Whether the validation fn is native.
iFnIx*: int32
iNative*: bool # Whether the validation fn is native.
userparam*: pointer
userType*: TypeId
shortdoc*: string
longdoc*: string

ZParamExport* = ref object
## What we pass when asked what parameters need to be provided.
Expand All @@ -433,6 +439,7 @@ type
modid*: int32
paramid*: int32
tid*: TypeId
private*: bool
havedefault*: bool
default*: pointer

Expand Down Expand Up @@ -897,6 +904,9 @@ type
# through a ZAssignAttr.
ZLockOnWrite = 0xb0,

# Print an error message and abort.
ZBail = 0xee,

# A no-op. These double as labels for disassembly too.
ZNop = 0xff

Expand Down Expand Up @@ -1218,8 +1228,6 @@ template debug*(s: string, s2: string, moreargs: varargs[string]) =

debug(cells.quickTable(verticalHeaders = true))



# The current runtime, so that builtin functions can access the state.
# Was having a weird link error so moved this here.
var
Expand Down
Loading

0 comments on commit 9cefa7b

Please sign in to comment.