Module Bmdirc (.ml)


module Bmdirc: sig .. end
Bombs-Must-Detonate: IR Compiler

Bombs-Must-Detonate: IR Compiler Signature
Author(s): : Brian Go, : Brian Go



type instruction =
| Push
| Pop
| Read
| Print
| PrintLn
| Swap
| Stop
| Return
| Cons
| Car
| Cdr
| IsNull
| Nil
| GetElem
| SetElem
| And
| Or
| Add
| Sub
| Mul
| Div
| DivI
| RemI
| Concat
| Neg
| Frac
| Int
| Lt
| Gt
| Lte
| Gte
| Eq
| Neq
| ConstUninit
| Apply (*1-arity1-arity*)
| ConstInt of int
| ConstFloat of float
| ConstString of string
| PushSf of string
| Assign of int
| Acc of int
| Rev of int
| MakeBlock of int
| MakeBlockFilled of int
| AllocFields of int
| GetField of int
| SetField of int
| Call of string
| Jmp of string
| Jz of string
| Jnz of string
| RPC of string
| Label of string
| Comment of string
| Annotation of string
| GetElemStatic of int
| SetElemStatic of int (*2-arity2-arity*)
| MakeBlockStatic of int * int
Final instruction

type ir_instruction =
| Instruction of instruction (*Ir-SpecificIr-Specific*)
| GetVar of string
| SetVar of string
| NoteVar of string * int
| NoteFunction of string
| If of ir_buffer * ir_buffer * ir_buffer (*test, then, elsetest, then, else*)
| For of ir_buffer * ir_buffer * ir_buffer * ir_buffer (*init, test, step, body*)
| While of ir_buffer * ir_buffer (*test, body*)
| DoWhile of ir_buffer * ir_buffer (*body, test*)
| Break
| Continue
| BeginScope
| EndScope
| EndScopeSf
| EndScopeSfRPC
| DeclareGlobal of string
IR Instruction
type ir_buffer = ir_instruction list 
val concat : string list -> string
Shorthand for String.concat ""
exception Ircompile_error of string
val init_buffer : ir_instruction list Pervasives.ref
Keeps track of the buffer of code used to initialize structs/arrays/lists. We need to initialize values of these types as null but of the correct structure. i.e. someStructType someStruct; stomeStruct.someField = someValue; should be valid.
val raise_compile_error : string list -> 'a
Raises an IR compile error by concatenating s_list
val repeat_list : 'a list -> int -> 'a list
Concatenates n repetitions of the given list
val label_id : int Pervasives.ref
Used by make_label
val make_label : string -> string
Generate labels with unique prefixes

Makes a label from the given string which has a unique prefix

val note_arg_list : ('a * string) list -> int -> ir_instruction list
Takes an argument list and notes the positions
val compile_datatype_init : Ast.data_type -> ir_instruction list
Compiles the initialization code of data type dt
val compile_datatypes_pushing : Ast.data_type list -> ir_instruction list
Compiles each data type in dt_list, inserting push instructions between each compilation result
val compile_expression : Ast.expression -> ir_buffer
Compiles an expression
val compile_expression_list : Ast.expression list -> ir_buffer
Compiles a list of expressions
val compile_variable_declaration : Ast.var_declare -> ir_buffer
Compiles a variable declarations
val compile_conditional : Ast.conditional -> ir_buffer
Compiles a conditional. Would be nice to add short-circuiting in the future.
val compile_continued_conditional : Ast.continued_conditional -> ir_buffer
Compiles an else case
val compile_loop : Ast.loop -> ir_buffer
Compiles a loop
val compile_value : Ast.value -> ir_buffer
Compiles a value
val compile_value_producer : Ast.value_producer -> ir_buffer
Compiles a value produder
val compile_value_producer_list_pushing : Ast.value_producer list -> ir_instruction list
Compiles each value producer in order, pushing the results onto the stack
val compile_list_value_producer : Ast.list_value_producer -> ir_buffer
Compiles a list value producer
val compile_variable_assignment : Ast.var_assign -> ir_buffer
Compiles a variable assignment
val compile_set_variable_identifier : Ast.var_ident -> ir_instruction list
Produces code that sets the variable identifier to the value in the accumulator. Leaves the value in the accumulator
val compile_get_variable_identifier : Ast.var_ident -> ir_buffer
Get a variable identifier into the accumulator
val compile_binary_operation : Ast.value_producer -> Ast.binop -> Ast.value_producer -> ir_buffer
Compiles a binary operation expression
val compile_prefix_unary_operation : Ast.pre_unop -> Ast.value_producer -> ir_buffer
Compiles unary operations
val compile_function_call : Ast.function_call -> ir_buffer
Compiles a function call. Doesn't use tail-recursion -- this would be nice to add in the future.

success ||
val compile_return : Ast.return_statement -> ir_buffer
Compiles a return statement
val compile_function_definition : string ->
('a * string) list -> Ast.expression list -> ir_instruction list
Compiles a function definition. Maybe this should have gone to IR first, but it works nicely as is.
val compile_global_expression : Ast.global_expression -> ir_instruction list
Compile global expression
val compile_global_expression_list : Ast.global_expression list -> ir_instruction list
Compile a global expression list
val compile_ir : Ast.global_expression list -> ir_instruction list
Compile a source program to IR representation, including the init buffer

Compiles the AST to a list of IR instructions

val string_of_instruction : instruction -> string
Produces a string representation of the given instruction

Gets the string representation of a given IR instruction

val string_of_ir_instruction : ir_instruction -> string
Produces a string representation of the given IR instruction
val string_of_ir_buffer : ir_buffer -> string
Produces a string representation of the given IR instruction buffer
val print_ir_buffer : Pervasives.out_channel -> ir_buffer -> unit
Prints the IR buffer to the given output channel

Writes the IR buffer to the given output channel