views:

1203

answers:

1

hello well I hope Im not breaking some spamming rule here with this. I just asked a question about how the erlang compiler implements pattern matching, and I got some great responses, one of which is the compiled bytecode (obtained with a parameter passed to the c() directive):

{function, match, 1, 2}. {label,1}. {func_info,{atom,match},{atom,match},1}. {label,2}. {test,is_tuple,{f,3},[{x,0}]}. {test,test_arity,{f,3},[{x,0},2]}. {get_tuple_element,{x,0},0,{x,1}}. {test,is_eq_exact,{f,3},[{x,1},{atom,a}]}. return. {label,3}. {badmatch,{x,0}}

its all just plain erlang tuples. I was expecting some cryptic binary thingy, guess not. so Im asking this on impulse here (I could look at the compiler source but asking questions always ends up better with extra insight), how is this output translated in the binary level?

say {test,is_tuple,{f,3},[{x,0}]} for example. Im assuming this is one instruction, called 'test'... anyway, so this output would essentially be the AST of the bytecode level language, from which the binary encoding is just a 1-1 translation? This is all so exciting, I had no idea that I can this easily see what the erlang compiler break things into.

thanks so much

+2  A: 

ok so I dug into the compiler source code to find the answer, and to my surprise the asm file produced with the 'S' parameter to the compile:file() function is actually consulted in as is (file:consult()) and then the tuples are checked one by one for further action(line 661 - beam_consult_asm(St) -> - compile.erl). further on then there's a generated mapping table in there (compile folder of the erlang source) that shows what the serial number of each bytecode label is, and Im guessing this is used to generate the actual binary signature of the bytecode. great stuff. but you just gotta love the consult() function, you can almost have a lispy type syntax for a random language and avoid the need for a parser/lexer fully and just consult source code into the compiler and do stuff with it... code as data data as code...

deepblue
Have you looked at Robert Virding's Lisp-Flavoured Erlang (http://forum.trapexit.org/viewtopic.php?p=40268)
Gordon Guthrie
yeah I've seen it, havent used it yet although its pretty high on my list of things to do in the short term. thanks tho
deepblue