That example would indeed be great for the tutorial, please create a PR adding 
it!

The macro you've created works fine, but as you mentioned that you didn't like 
the way it looked I took a stab at improving it:
    
    
    import macros
    
    macro interproc(): untyped =
      const erroringVectors = [8, 10, 11, 12, 13, 14, 17, 21, 29, 30]
      
      # Create a statement list with a single [] element
      result = newStmtList(nnkBracket.newTree())
      
      for iv in 0..<32:
        let
          wrapperName = if iv in erroringVectors: "errorInterruptWrapper" else: 
"genericInterruptWrapper"
          name = ident("vectorPusher" & $iv)
          asmCode = nnkAsmStmt.newTree(
            newEmptyNode(),
            newLit("pushq " & $iv & ";jmp " & wrapperName))
        
        # Add proc to the statement list
        result.insert 0, quote do:
          proc `name`() {.exportc, asmNoStackFrame.} =
            `asmCode`
        # Add pointer to proc in the array
        result[^1].add quote do:
          cast[uint64](`name`)
      # `result` is implicitly returned and is now an an array of uint64s which 
can be assigned to a variable
    
    let interruptProcedures = interproc()
    
    
    Run

Whether or not it's an improvement is entirely subjective of course, but at 
least to me the creation of an array which can be directly assigned to any 
variable makes the invocation of the macro easier to read.

Reply via email to