init commit
This commit is contained in:
403
Codegen.jai
Normal file
403
Codegen.jai
Normal file
@@ -0,0 +1,403 @@
|
||||
Output_Language :: enum {
|
||||
HLSL;
|
||||
GLSL;
|
||||
}
|
||||
|
||||
Codegen_State :: struct {
|
||||
path : string;
|
||||
|
||||
scope_stack : Scope_Stack;
|
||||
current_scope : Scope_Handle;
|
||||
|
||||
type_variables : []Type_Variable;
|
||||
root : *AST_Node;
|
||||
|
||||
output_language : Output_Language;
|
||||
|
||||
builder : String_Builder;
|
||||
|
||||
result : Codegen_Result;
|
||||
}
|
||||
|
||||
Codegen_Result :: struct {
|
||||
messages : [..]Compiler_Message;
|
||||
|
||||
had_error : bool;
|
||||
|
||||
result_text : string; // @Incomplete(nb): Result for now, should likely be far more sophisticated.
|
||||
}
|
||||
|
||||
init_codegen_state :: (state : *Codegen_State, root : *AST_Node, checker_result : Semantic_Check_Result, output_language : Output_Language) {
|
||||
state.root = root;
|
||||
state.scope_stack = checker_result.scope_stack;
|
||||
state.type_variables = checker_result.type_variables;
|
||||
state.current_scope = cast(Scope_Handle)1;
|
||||
init_string_builder(*state.builder);
|
||||
}
|
||||
|
||||
indent :: (state : *Codegen_State, indentation : int) {
|
||||
for 1..indentation append(*state.builder, " ");
|
||||
}
|
||||
|
||||
emit_field :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
find_result := find_symbol(state.scope_stack, node.name, state.current_scope);
|
||||
|
||||
field := h2tv(state.type_variables, find_result.type_variable);
|
||||
|
||||
indent(state, indentation);
|
||||
print_to_builder(*state.builder, "% ", type_to_string(field));
|
||||
print_to_builder(*state.builder, "%", node.name);
|
||||
|
||||
for i :0..node.children.count - 1 {
|
||||
|
||||
child := node.children[i];
|
||||
|
||||
print_to_builder(*state.builder, " = ");
|
||||
emit_node(state, child, 0);
|
||||
}
|
||||
|
||||
for i :0..field.child_count - 1 {
|
||||
child := h2tv(state.type_variables, field.children[i]);
|
||||
emit_node(state, child.source_node, 0);
|
||||
}
|
||||
|
||||
for hint : node.hint_tokens {
|
||||
if hint.ident_value == "position" {
|
||||
// @Incomplete(nb): Should be a lookup table somewhere
|
||||
append(*state.builder, " : POSITION");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit_block :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
for statement : node.children {
|
||||
emit_node(state, statement, indentation);
|
||||
|
||||
if it_index < node.children.count {
|
||||
append(*state.builder, ";\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit_call :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
indent(state, indentation);
|
||||
print_to_builder(*state.builder, "%(", node.name);
|
||||
|
||||
if node.children.count > 0 {
|
||||
args := node.children[0];
|
||||
|
||||
for child : args.children {
|
||||
emit_node(state, child, 0);
|
||||
|
||||
if it_index != args.children.count - 1 {
|
||||
append(*state.builder, ", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
append(*state.builder, ")");
|
||||
}
|
||||
|
||||
emit_properties :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
find_result := find_symbol(state.scope_stack, ifx node.name.count > 0 then node.name else "properties", state.current_scope);
|
||||
|
||||
if !find_result {
|
||||
message : Compiler_Message;
|
||||
message.message_kind = .Internal_Error;
|
||||
message.path = state.path;
|
||||
message.message = "Attempting to generate undeclared properties buffer. This should never happen at this stage.";
|
||||
array_add(*state.result.messages, message);
|
||||
}
|
||||
assert(find_result != null, "Attempting to generate undeclared properties buffer. This should never happen at this stage.");
|
||||
|
||||
variable := h2tv(state.type_variables, find_result.type_variable);
|
||||
|
||||
print_to_builder(*state.builder, "cbuffer __PROPERTIES : register(b%) \n{\n", variable.buffer_index);
|
||||
|
||||
previous_scope := state.current_scope;
|
||||
state.current_scope = variable.scope;
|
||||
|
||||
for child : node.children {
|
||||
if child.kind == .FieldList {
|
||||
for field : child.children {
|
||||
emit_node(state, field, 1);
|
||||
|
||||
append(*state.builder, ";\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
state.current_scope = previous_scope;
|
||||
|
||||
append(*state.builder, "}\n\n");
|
||||
}
|
||||
|
||||
emit_function :: (state : *Codegen_State, node : *AST_Node, indentation : int, emit_body := true) {
|
||||
name := get_actual_function_name(node);
|
||||
find_result := find_symbol(state.scope_stack, name, state.current_scope);
|
||||
|
||||
assert(find_result != null, "Attempting to generate undeclared function. This should never happen at this stage.");
|
||||
if !find_result {
|
||||
message : Compiler_Message;
|
||||
message.message_kind = .Internal_Error;
|
||||
message.path = state.path;
|
||||
message.message = "Attempting to generate undeclared function. This should never happen at this stage.";
|
||||
array_add(*state.result.messages, message);
|
||||
}
|
||||
|
||||
for func : find_result.functions {
|
||||
function_variable := h2tv(state.type_variables, func.type_variable);
|
||||
|
||||
indent(state, indentation);
|
||||
|
||||
if function_variable.return_var {
|
||||
return_variable := h2tv(state.type_variables, function_variable.return_var);
|
||||
print_to_builder(*state.builder, "% ", type_to_string(return_variable));
|
||||
} else {
|
||||
append(*state.builder, "void ");
|
||||
}
|
||||
|
||||
print_to_builder(*state.builder, "%", node.name);
|
||||
|
||||
previous_scope := state.current_scope;
|
||||
state.current_scope = function_variable.scope;
|
||||
|
||||
append(*state.builder, "(");
|
||||
|
||||
if node.children.count > 0 && node.children[0].kind == .FieldList {
|
||||
params := node.children[0];
|
||||
|
||||
for child : params.children {
|
||||
emit_node(state, child, 0);
|
||||
|
||||
if it_index != params.children.count - 1 {
|
||||
append(*state.builder, ", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
append(*state.builder, ")");
|
||||
|
||||
for hint : node.hint_tokens {
|
||||
if hint.ident_value == "position" {
|
||||
// @Incomplete(nb): Should be a lookup table somewhere
|
||||
append(*state.builder, " : SV_POSITION");
|
||||
}
|
||||
|
||||
if starts_with(hint.ident_value, "target") {
|
||||
// @Incomplete(nb): Should be a lookup table somewhere
|
||||
append(*state.builder, " : SV_TARGET");
|
||||
}
|
||||
}
|
||||
|
||||
if emit_body {
|
||||
append(*state.builder, "\n{\n");
|
||||
|
||||
|
||||
if node.children.count > 1 {
|
||||
emit_block(state, node.children[1], indentation + 1);
|
||||
}
|
||||
|
||||
append(*state.builder, "}\n");
|
||||
} else {
|
||||
append(*state.builder, ";");
|
||||
}
|
||||
|
||||
append(*state.builder, "\n");
|
||||
|
||||
|
||||
state.current_scope = previous_scope;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
emit_operator :: (state : *Codegen_State, op_kind : Token_Kind) {
|
||||
if op_kind == {
|
||||
case .TOKEN_PLUS; {
|
||||
append(*state.builder, "+");
|
||||
}
|
||||
case .TOKEN_MINUS; {
|
||||
append(*state.builder, "-");
|
||||
}
|
||||
case .TOKEN_STAR; {
|
||||
append(*state.builder, "*");
|
||||
}
|
||||
case .TOKEN_SLASH; {
|
||||
append(*state.builder, "/");
|
||||
}
|
||||
case .TOKEN_ISEQUAL; {
|
||||
append(*state.builder, "==");
|
||||
}
|
||||
case .TOKEN_ASSIGN; {
|
||||
append(*state.builder, "=");
|
||||
}
|
||||
case .TOKEN_ISNOTEQUAL; {
|
||||
append(*state.builder, "!=");
|
||||
}
|
||||
case .TOKEN_LOGICALOR; {
|
||||
append(*state.builder, "||");
|
||||
}
|
||||
case .TOKEN_LOGICALAND; {
|
||||
append(*state.builder, "&&");
|
||||
}
|
||||
case .TOKEN_LESS; {
|
||||
append(*state.builder, "<");
|
||||
}
|
||||
case .TOKEN_LESSEQUALS; {
|
||||
append(*state.builder, "<=");
|
||||
}
|
||||
case .TOKEN_GREATER; {
|
||||
append(*state.builder, ">");
|
||||
}
|
||||
case .TOKEN_GREATEREQUALS; {
|
||||
append(*state.builder, ">=");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit_node :: (state : *Codegen_State, node : *AST_Node, indentation : int) {
|
||||
if node.kind == {
|
||||
case .Integer; {
|
||||
print_to_builder(*state.builder, "%", node.integer_value);
|
||||
}
|
||||
case .Float; {
|
||||
print_to_builder(*state.builder, "%f", formatFloat(node.float_value, zero_removal=.ONE_ZERO_AFTER_DECIMAL));
|
||||
}
|
||||
case .Properties; {
|
||||
|
||||
}
|
||||
case .Field; {
|
||||
emit_field(state, node, indentation);
|
||||
}
|
||||
case .Block; {
|
||||
assert(false, "Not implemented yet: block");
|
||||
}
|
||||
case .Variable; {
|
||||
indent(*state.builder, indentation);
|
||||
|
||||
is_properties := node.name == "properties";
|
||||
|
||||
if !is_properties {
|
||||
print_to_builder(*state.builder, "%", node.name);
|
||||
}
|
||||
|
||||
if node.children.count > 0 {
|
||||
if !is_properties {
|
||||
append(*state.builder, ".");
|
||||
}
|
||||
emit_node(state, node.children[0], 0);
|
||||
}
|
||||
}
|
||||
case .Binary; {
|
||||
indent(*state.builder, indentation);
|
||||
lhs := node.children[0];
|
||||
rhs := node.children[1];
|
||||
emit_node(state, lhs, 0);
|
||||
|
||||
append(*state.builder, " ");
|
||||
emit_operator(state, node.token.kind);
|
||||
append(*state.builder, " ");
|
||||
emit_node(state, rhs, 0);
|
||||
}
|
||||
case .Unary; {
|
||||
assert(false, "Not implemented yet: unary");
|
||||
}
|
||||
case .Expression_Statement; {
|
||||
emit_node(state, node.children[0], indentation);
|
||||
}
|
||||
case .Call; {
|
||||
emit_call(state, node, indentation);
|
||||
}
|
||||
case .Return; {
|
||||
indent(*state.builder, indentation);
|
||||
append(*state.builder, "return ");
|
||||
emit_node(state, node.children[0], 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit_declaration :: (state : *Codegen_State, node : *AST_Node) {
|
||||
if node.kind == {
|
||||
case .Function; {
|
||||
emit_function(state, node, 0);
|
||||
}
|
||||
case .Properties; {
|
||||
emit_properties(state, node, 0);
|
||||
}
|
||||
case .Struct; {
|
||||
print_to_builder(*state.builder, "struct %", node.name);
|
||||
|
||||
current_scope := state.current_scope;
|
||||
state.current_scope = h2tv(state.type_variables, node.type_variable).scope;
|
||||
|
||||
field_list := node.children[0];
|
||||
|
||||
if field_list.children.count > 0 {
|
||||
append(*state.builder, "\n{\n");
|
||||
} else {
|
||||
append(*state.builder, " {");
|
||||
}
|
||||
|
||||
|
||||
for child : field_list.children {
|
||||
emit_node(state, child, 1);
|
||||
|
||||
if it_index < field_list.children.count {
|
||||
append(*state.builder, ";\n");
|
||||
}
|
||||
}
|
||||
|
||||
append(*state.builder, "}\n\n");
|
||||
state.current_scope = current_scope;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
codegen :: (state : *Codegen_State) -> Codegen_Result {
|
||||
found_function : bool = false;
|
||||
found_struct : bool = false;
|
||||
|
||||
for variable : state.type_variables {
|
||||
if variable.type == .Struct && variable.kind == .Declaration && !variable.builtin {
|
||||
if variable.source_node.kind == .Properties continue;
|
||||
if variable.source_node.kind == .Meta continue;
|
||||
print_to_builder(*state.builder, "struct %;\n", variable.source_node.name);
|
||||
found_struct = true;
|
||||
}
|
||||
}
|
||||
|
||||
if found_struct {
|
||||
append(*state.builder, "\n");
|
||||
}
|
||||
|
||||
for variable : state.type_variables {
|
||||
if variable.type == .Function && !variable.builtin
|
||||
&& !variable.source_node.vertex_entry_point && !variable.source_node.pixel_entry_point {
|
||||
emit_function(state, variable.source_node, 0, false);
|
||||
found_function = true;
|
||||
}
|
||||
}
|
||||
if found_function {
|
||||
append(*state.builder, "\n");
|
||||
}
|
||||
|
||||
for declaration : state.root.children {
|
||||
if declaration.foreign_declaration {
|
||||
continue;
|
||||
}
|
||||
emit_declaration(state, declaration);
|
||||
}
|
||||
|
||||
state.result.result_text = builder_to_string(*state.builder);
|
||||
|
||||
print("%\n", state.result.result_text);
|
||||
return state.result;
|
||||
}
|
||||
|
||||
codegen :: (ast_root : *AST_Node, checker_result : Semantic_Check_Result, output_language : Output_Language) -> Codegen_Result {
|
||||
codegen_state : Codegen_State;
|
||||
init_codegen_state(*codegen_state, ast_root, checker_result, output_language);
|
||||
return codegen(*codegen_state);
|
||||
}
|
||||
Reference in New Issue
Block a user