Template to Bytecode mapping

December 8, 2015 ยท View on GitHub

Expressions

expressionbytecode
<expr>expr
write
<(expr)>expr
tostr
write
<expr; o1=e1, o2=e2>expr
options
e1
store_option o1-option-index
e2
store_option o2-option-index
write_opt
textload_str string-pool-index
truetrue
falsefalse
aload_local attribute-index ; if a is template argument
iload_local attribute-index
i0load_local attribute-index
aload_attr a-string-pool-index
a.bload_attr a ; from now now, a means its string index
load_prop b
a.(b)load_attr a
load_attr b
load_prop_ind
t()new t,0 ; string pool index of t
super.r()super_new region_t_r,0 ; region r in template t
t(e1,e2,e3)e1
e2
e3
new t,3
t(...)args
passthru t
new_box_args t
t(a1=e1,a2=e2,a3=e3)args
e1
store_arg a1
e2
store_arg a2
e3
store_arg a3
new_box_args t
t(a1=e1,a2=e2,...)args
e1
store_arg a1
e2
store_arg a2
passthru t
new_box_args t
(expr)(args)expr
tostr
args
new_ind num-args
a:t()load_attr a
null
new t,1
map
a:t(x)load_attr a
null
x
new t,2
map
a:t(),u()load_attr a
null
new t,1
null
new u,1
rot_map 2
a,b:t()load_attr a
load_attr b
null
null
new t,2
zip_map 2
first(expr)expr
first ; predefined function
[a,b,c]list
a
add
b
add
c
add

Anonymous templates

expressionbytecode
{t}new _subN,0
`a:{x...}`
`a,b:{x,y...}`

If statements

upon if, create 'end' label.
upon else, create 'else' label.

<if(a)>t<endif>:

    load_attr a                                          
    brf end 
    t 
    write 
end:

<if(a)>t<else>u<endif>:

   load_attr a 
    brf else 
    t 
    write 
    br end 
else: 
    u 
    write 
end:

<if(a)>t<elseif(b)>u<else>v<endif>:

    load_attr a 
    brf lab1 
    t 
    write 
    br end 
lab1: 
    load_attr b 
    brf lab2 
    u 
    write 
    br end 
lab2: 
    v 
    write 
end:

<if(!a)>t<endif>:

    load_attr a 
    not 
    brf end 
    t 
    write 
end:

a||b:

    load_attr a 
    load_attr b 
    or

a&&b:

    load_attr a 
    load_attr b 
    and

Auto-indentation

<expr>\n:

expr 
write 
newline

\n\t<expr>:

newline 
indent "\t" 
expr 
write 
dedent

Size limitations

I use unsigned shorts not ints for the bytecode operands and addresses. This limits size of templates but not the output size. In single template, you can have only 64k of:

  • attributes
  • unique property name refs
  • unique template name refs
  • options (there are only about 5 now)
  • lists or template names in a map/iteration operation
  • bytecodes (short addressed)
  • chunks of text outside of expressions. effectively same thing as saying can have at most 64k / n expressions where n is avg size of bytecode to emit an expression. E.g., 3 bytes to write a chunk of text.