Search code examples
haskellhappy

Reference variable in quasiquoted LLVM


How do you pass a variable into a quasiquoted section?

I'm using llvm-quote-general and would like to pass a Type into an instruction I'm building with the quasiquoter.

I can build an instruction from just text:

alloci64 :: Instruction
alloci64 = [lli|alloc i64|]

I'd like to be able to pass in a type. What is the syntax to do so?

alloc :: Type -> Instruction
alloc t = [lli|alloc t|]

I've tried all of the following, and none of them have worked. They result in lexer errors.

[lli|alloc $t|]
[lli|alloc $$t|]
[lli|alloc $t$|]
[lli|alloc $$t$$|]

The quasiquoter in the library was generated with happy. Looking through the source code, the closest thing I can find to a token in relation to anti quotation is the symbol happy_dollar_dollar, which doesn't seem to be defined anywhere.

Edit: I solved the problem for Types; an example is [lli|alloc $type:t]

Operands

I solved the problem with Types, but still can't figure out how to anti-quote things in general. I'd like to use Operands building an instruction. Trying to do so results in an error.

add' :: Operand -> Operand -> Instruction
add' a b = [lli|add $opr:a $opr:b|]
                    ^
                    parse error on `ANTI_OPR'

The pre-happy source suggests that all of the following, including $opr: should do something

<0> {
 "$dl:"           / { allowAnti } { lexAnti Tanti_dl }
 "$dlM:"           / { allowAnti } { lexAntiM Tanti_dl }
 "$tt:"           / { allowAnti } { lexAnti Tanti_tt }
 "$ttM:"           / { allowAnti } { lexAntiM Tanti_tt }
 "$def:"          / { allowAnti } { lexAnti Tanti_def }
 "$defM:"          / { allowAnti } { lexAntiM Tanti_def }
 "$defs:"         / { allowAnti } { lexAnti Tanti_defs }
 "$defsM:"         / { allowAnti } { lexAntiM Tanti_defs }
 "$bb:"           / { allowAnti } { lexAnti Tanti_bb }
 "$bbM:"           / { allowAnti } { lexAntiM Tanti_bb }
 "$bbs:"          / { allowAnti } { lexAnti Tanti_bbs }
 "$bbsM:"          / { allowAnti } { lexAntiM Tanti_bbs }
 "$instr:"        / { allowAnti } { lexAnti Tanti_instr }
 "$instrM:"        / { allowAnti } { lexAntiM Tanti_instr }
 "$instrs:"       / { allowAnti } { lexAnti Tanti_instrs }
 "$instrsM:"       / { allowAnti } { lexAntiM Tanti_instrs }
 "$type:"         / { allowAnti } { lexAnti Tanti_type }
 "$typeM:"         / { allowAnti } { lexAntiM Tanti_type }
 "$opr:"          / { allowAnti } { lexAnti Tanti_opr }
 "$oprM:"          / { allowAnti } { lexAntiM Tanti_opr }
 "$const:"        / { allowAnti } { lexAnti Tanti_const }
 "$constM:"        / { allowAnti } { lexAntiM Tanti_const }
 "$id:"           / { allowAnti } { lexAnti Tanti_id }
 "$idM:"           / { allowAnti } { lexAntiM Tanti_id }
 "$gid:"          / { allowAnti } { lexAnti Tanti_gid }
 "$gidM:"          / { allowAnti } { lexAntiM Tanti_gid }
 "$param:"        / { allowAnti } { lexAnti Tanti_param }
 "$paramM:"        / { allowAnti } { lexAntiM Tanti_param }
 "$params:"       / { allowAnti } { lexAnti Tanti_params }
 "$paramsM:"       / { allowAnti } { lexAntiM Tanti_params }
}

Solution

  • Fiddling around some more, the following worked

    alloc :: Type -> Instruction
    alloc t = [lli|alloc $type:t|]
    

    The string "type" was in the source code and other examples contain text like $exp:e1 without explaining what it it.