Skip to content

Commit

Permalink
Indent nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 19, 2024
1 parent 749945e commit 1d795a5
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 107 deletions.
99 changes: 65 additions & 34 deletions Sources/backends/cstyle.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "cstyle.h"

#include "../errors.h"
#include "util.h"

#include <assert.h>
#include <inttypes.h>
Expand All @@ -11,41 +12,49 @@ static char *function_string(name_id func) {
return get_name(func);
}

void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func type_string) {
void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func type_string, int *indentation) {
switch (o->type) {
case OPCODE_VAR:
indent(code, offset, *indentation);
if (o->op_var.var.type.array_size > 0) {
*offset +=
sprintf(&code[*offset], "\t%s _%" PRIu64 "[%i];\n", type_string(o->op_var.var.type.type), o->op_var.var.index, o->op_var.var.type.array_size);
sprintf(&code[*offset], "%s _%" PRIu64 "[%i];\n", type_string(o->op_var.var.type.type), o->op_var.var.index, o->op_var.var.type.array_size);
}
else {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 ";\n", type_string(o->op_var.var.type.type), o->op_var.var.index);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 ";\n", type_string(o->op_var.var.type.type), o->op_var.var.index);
}
break;
case OPCODE_NOT:
*offset += sprintf(&code[*offset], "\t_%" PRIu64 " = !_%" PRIu64 ";\n", o->op_not.to.index, o->op_not.from.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64 " = !_%" PRIu64 ";\n", o->op_not.to.index, o->op_not.from.index);
break;
case OPCODE_STORE_VARIABLE:
*offset += sprintf(&code[*offset], "\t_%" PRIu64 " = _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64 " = _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
break;
case OPCODE_SUB_AND_STORE_VARIABLE:
*offset += sprintf(&code[*offset], "\t_%" PRIu64 " -= _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64 " -= _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
break;
case OPCODE_ADD_AND_STORE_VARIABLE:
*offset += sprintf(&code[*offset], "\t_%" PRIu64 " += _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64 " += _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
break;
case OPCODE_DIVIDE_AND_STORE_VARIABLE:
*offset += sprintf(&code[*offset], "\t_%" PRIu64 " /= _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64 " /= _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
break;
case OPCODE_MULTIPLY_AND_STORE_VARIABLE:
*offset += sprintf(&code[*offset], "\t_%" PRIu64 " *= _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64 " *= _%" PRIu64 ";\n", o->op_store_var.to.index, o->op_store_var.from.index);
break;
case OPCODE_STORE_MEMBER:
case OPCODE_SUB_AND_STORE_MEMBER:
case OPCODE_ADD_AND_STORE_MEMBER:
case OPCODE_DIVIDE_AND_STORE_MEMBER:
case OPCODE_MULTIPLY_AND_STORE_MEMBER:
*offset += sprintf(&code[*offset], "\t_%" PRIu64, o->op_store_member.to.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "_%" PRIu64, o->op_store_member.to.index);
type *s = get_type(o->op_store_member.member_parent_type);
bool is_array = o->op_store_member.member_parent_array;
for (size_t i = 0; i < o->op_store_member.member_indices_size; ++i) {
Expand Down Expand Up @@ -80,24 +89,26 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
}
break;
case OPCODE_LOAD_CONSTANT:
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = %f;\n", type_string(o->op_load_constant.to.type.type), o->op_load_constant.to.index,
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %f;\n", type_string(o->op_load_constant.to.type.type), o->op_load_constant.to.index,
o->op_load_constant.number);
break;
case OPCODE_CALL: {
indent(code, offset, *indentation);
debug_context context = {0};
if (o->op_call.func == add_name("sample")) {
check(o->op_call.parameters_size == 3, context, "sample requires three parameters");
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 ".Sample(_%" PRIu64 ", _%" PRIu64 ");\n", type_string(o->op_call.var.type.type),
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 ".Sample(_%" PRIu64 ", _%" PRIu64 ");\n", type_string(o->op_call.var.type.type),
o->op_call.var.index, o->op_call.parameters[0].index, o->op_call.parameters[1].index, o->op_call.parameters[2].index);
}
else if (o->op_call.func == add_name("sample_lod")) {
check(o->op_call.parameters_size == 4, context, "sample_lod requires four parameters");
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 ".SampleLevel(_%" PRIu64 ", _%" PRIu64 ", _%" PRIu64 ");\n",
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 ".SampleLevel(_%" PRIu64 ", _%" PRIu64 ", _%" PRIu64 ");\n",
type_string(o->op_call.var.type.type), o->op_call.var.index, o->op_call.parameters[0].index, o->op_call.parameters[1].index,
o->op_call.parameters[2].index, o->op_call.parameters[3].index);
}
else {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = %s(", type_string(o->op_call.var.type.type), o->op_call.var.index,
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = %s(", type_string(o->op_call.var.type.type), o->op_call.var.index,
function_string(o->op_call.func));
if (o->op_call.parameters_size > 0) {
*offset += sprintf(&code[*offset], "_%" PRIu64, o->op_call.parameters[0].index);
Expand All @@ -110,71 +121,84 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
break;
}
case OPCODE_ADD: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " + _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " + _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_SUB: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " - _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " - _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_MULTIPLY: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " * _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " * _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_DIVIDE: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " / _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " / _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_EQUALS: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " == _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " == _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_NOT_EQUALS: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " != _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " != _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_GREATER: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " > _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " > _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_GREATER_EQUAL: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " >= _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " >= _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_LESS: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " < _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " < _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_LESS_EQUAL: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " <= _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " <= _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_AND: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " && _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " && _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_OR: {
*offset += sprintf(&code[*offset], "\t%s _%" PRIu64 " = _%" PRIu64 " || _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "%s _%" PRIu64 " = _%" PRIu64 " || _%" PRIu64 ";\n", type_string(o->op_binary.result.type.type),
o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
break;
}
case OPCODE_IF: {
indent(code, offset, *indentation);
if (o->op_if.condition.index != 0) {
*offset += sprintf(&code[*offset], "\tif (_%" PRIu64, o->op_if.condition.index);
*offset += sprintf(&code[*offset], "if (_%" PRIu64, o->op_if.condition.index);
}
else {
*offset += sprintf(&code[*offset], "\tif (");
*offset += sprintf(&code[*offset], "if (");
}
for (uint8_t i = 0; i < o->op_if.exclusions_size; ++i) {
if (i == 0 && o->op_if.condition.index == 0) {
Expand All @@ -188,24 +212,31 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
break;
}
case OPCODE_WHILE_START: {
*offset += sprintf(&code[*offset], "\twhile (true)\n");
*offset += sprintf(&code[*offset], "\t{\n");
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "while (true)\n");
*offset += sprintf(&code[*offset], "{\n");
break;
}
case OPCODE_WHILE_CONDITION: {
*offset += sprintf(&code[*offset], "\tif (!_%" PRIu64 ") break;\n", o->op_while.condition.index);
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "if (!_%" PRIu64 ") break;\n", o->op_while.condition.index);
break;
}
case OPCODE_WHILE_END: {
*offset += sprintf(&code[*offset], "\t}\n");
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "}\n");
break;
}
case OPCODE_BLOCK_START: {
*offset += sprintf(&code[*offset], "\t{\n");
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "{\n");
*indentation += 1;
break;
}
case OPCODE_BLOCK_END: {
*offset += sprintf(&code[*offset], "\t}\n");
*indentation -= 1;
indent(code, offset, *indentation);
*offset += sprintf(&code[*offset], "}\n");
break;
}
default: {
Expand Down
2 changes: 1 addition & 1 deletion Sources/backends/cstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

typedef char *(*type_string_func)(type_id type);

void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func type_string);
void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func type_string, int *indentation);
Loading

0 comments on commit 1d795a5

Please sign in to comment.