Skip to content

Instantly share code, notes, and snippets.

@hansihe
Created July 13, 2017 10:36
Show Gist options
  • Save hansihe/9a86f3d8cbc15f2f4a21d8b2cf4601d9 to your computer and use it in GitHub Desktop.
Save hansihe/9a86f3d8cbc15f2f4a21d8b2cf4601d9 to your computer and use it in GitHub Desktop.
read_tag_val() {
tag = read_u8();
tag_type = tag & 0b00000111;
tag_expanded = tag & 0b00001000;
tag_adv_expanded = tag & 0b00010000;
if tag_expanded == 0 {
val = tag >> 4;
} else if tag_adv_expanded == 0 {
expand_val = read_u8();
val = (tag >> 5) << 8;
val |= expand_val;
} else {
TODO
}
(tag_type, val)
}
read_code() {
while true {
opcode = read_u8();
arity = lookup_arity_from_optab(opcode);
arg_num = 0;
while arg_num < arity {
(tag, val) = read_tag_val();
match tag {
0 => //val is label
2 => //val is atom
3 => //val is x reg
4 => //val is y reg
5 => //val is failure label
7 => {
val is extended tag
match val {
0 => //float literal, not codegen anymore, maybe handle later,
1 => { //extended arity
assert arg_num+1 == arity;
(l_tag, l_val) = read_tag_val();
assert l_tag == 0;
arity += l_val;
}
4 => { //literal
(l_tag, l_val) = read_tag_val();
assert l_tag == 0;
l_val is literal index
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment