-
Notifications
You must be signed in to change notification settings - Fork 2
Bytecode compiler pipeline
Yotam Barnoy edited this page Apr 22, 2015
·
15 revisions
The Bytecode compiler translates OCaml source code to a simple stack-based bytecode to be run by the Bytecode Runtime.
The bytecode compiler starts with its driver, which has functions for dealing with either compiling an interface or an implementation.
- We call drive/compmisc.ml:init_path, which sets the path in Config.load_path based on environment variables and parameters (such as '-I'). It also resets the cache in the environment via Env.reset_cache.
- We set current_unit in the Env Module to the current module.
- We initialize the environment via a call to Compmisc.initial_env. This function calls Ident.reinit, and creates an initial environment. It does so by indirectly calling Predef.build_initial_env, which creates the basic types in the type environment, and then opening Pervasives (by default) and any other included modules, sending each of these modules as argument to the Typemod.type_open_ function.
- Many print_if calls are used to print out stages of the pipeline as requested.
- The Parsing Driver (PParse.parse_implementation) is called to parse the file to AST.
- Typing information is added to the AST by Typemod.type_implementation
- The typed AST is then translated to the Lambda AST via Translmod.transl_implementation
- It is simplified down by Simplif.simplify_lambda
- The Lambda AST is compiled to the interpreter bytecode AST by calling Bytegen.compile_implementation
- Emitcode.to_file is called to convert the bytecode AST to real bytecode.
The Bytecode consists of the following instructions (to be found in bytecomp/instruct.ml). Many of the instructions have shortened common variants for a certain number of arguments and for combining instructions together, allowing for better performance, since less memory needs to be read.
- Kacc/ACC: load the accumulator from a location on the stack (shortcuts exist up to ACC7).
- Kconst/CONST: a constant value. Shortcuts for numbers 1-3.
- Kpush/PUSH: push the accumulator onto the stack, decrementing the stack pointer.
- Shortcuts exist for PUSHACC_(1-7), allowing to push and load the accumulator in one instruction.
- Kapply/APPLY: apply a function. The accumulator serves as the new PC as well as the environment.
- Kccall/C_CALL: perform a call of a C function. On the C side, shortcuts exist up to C_CALL5, whereas higher arities require another argument.