#include #include #include #include #include static char * read_file (const char *filename); void output_code (OrcProgram *p, FILE *output); void output_code_header (OrcProgram *p, FILE *output); void output_code_test (OrcProgram *p, FILE *output); typedef enum { MUTEX_STYLE_SCHRO, MUTEX_STYLE_GLIB } MutexStyle; MutexStyle mutex_style = MUTEX_STYLE_GLIB; int main (int argc, char *argv[]) { char *code; int n; int i; OrcProgram **programs; const char *filename = "test.orc"; FILE *output; orc_init (); orc_test_init (); if (argc >= 2) { filename = argv[1]; } code = read_file (filename); if (!code) { printf("orcc \n"); exit(1); } n = orc_parse (code, &programs); output = fopen ("out.c", "w"); fprintf(output, "\n"); fprintf(output, "/* autogenerated from %s */\n", filename); fprintf(output, "\n"); fprintf(output, "#include \n"); fprintf(output, "#include \n"); fprintf(output, "#include \n"); switch (mutex_style) { case MUTEX_STYLE_SCHRO: fprintf(output, "#include \n"); fprintf(output, "#define MUTEX_LOCK() schro_mutex_lock (orc_mutex)\n"); fprintf(output, "#define MUTEX_UNLOCK() schro_mutex_unlock (orc_mutex)\n"); fprintf(output, "SchroMutex *orc_mutex;\n"); break; case MUTEX_STYLE_GLIB: fprintf(output, "#include \n"); fprintf(output, "#define MUTEX_LOCK() g_static_mutex_lock (&orc_mutex)\n"); fprintf(output, "#define MUTEX_UNLOCK() g_static_mutex_unlock (&orc_mutex)\n"); fprintf(output, "static GStaticMutex orc_mutex = G_STATIC_MUTEX_INIT;\n"); break; default: break; } fprintf(output, "\n"); fprintf(output, "#ifndef MUTEX_LOCK\n"); fprintf(output, "#define MUTEX_LOCK do { } while (0)\n"); fprintf(output, "#endif\n"); fprintf(output, "#ifndef MUTEX_UNLOCK\n"); fprintf(output, "#define MUTEX_UNLOCK do { } while (0)\n"); fprintf(output, "#endif\n"); fprintf(output, "\n"); fprintf(output, "\n"); for(i=0;i\n"); //fprintf(output, "\n"); fprintf(output, "#ifndef _ORC_OUT_H_\n"); fprintf(output, "#define _ORC_OUT_H_\n"); fprintf(output, "\n"); for(i=0;i\n"); fprintf(output, "#include \n"); fprintf(output, "#include \n"); fprintf(output, "#include \n"); fprintf(output, "\n"); fprintf(output, "\n"); fprintf(output, "int\n"); fprintf(output, "main (int argc, char *argv[])\n"); fprintf(output, "{\n"); fprintf(output, " int error = FALSE;\n"); fprintf(output, "\n"); fprintf(output, " orc_test_init ();\n"); fprintf(output, "\n"); for(i=0;iname); for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_D1 + i]; if (var->size) { if (var->type_name) { fprintf(output, "%s * %s, ", var->type_name, varnames[ORC_VAR_D1 + i]); } else { fprintf(output, "uint%d_t * %s, ", var->size*8, varnames[ORC_VAR_D1 + i]); } } } for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_A1 + i]; if (var->size) { if (var->type_name) { fprintf(output, "%s * %s, ", var->type_name, varnames[ORC_VAR_A1 + i]); } else { fprintf(output, "uint%d_t * %s, ", var->size*8, varnames[ORC_VAR_A1 + i]); } } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_S1 + i]; if (var->size) { if (var->type_name) { fprintf(output, "%s * %s, ", var->type_name, varnames[ORC_VAR_S1 + i]); } else { fprintf(output, "uint%d_t * %s, ", var->size*8, varnames[ORC_VAR_S1 + i]); } } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_P1 + i]; if (var->size) { fprintf(output, "int %s, ", varnames[ORC_VAR_P1 + i]); } } fprintf(output, "int n);\n"); } void output_code (OrcProgram *p, FILE *output) { OrcVariable *var; int i; fprintf(output, "\n"); fprintf(output, "/* %s */\n", p->name); fprintf(output, "void\n"); fprintf(output, "%s (", p->name); for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_D1 + i]; if (var->size) { if (var->type_name) { fprintf(output, "%s * %s, ", var->type_name, varnames[ORC_VAR_D1 + i]); } else { fprintf(output, "uint%d_t * %s, ", var->size*8, varnames[ORC_VAR_D1 + i]); } } } for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_A1 + i]; if (var->size) { if (var->type_name) { fprintf(output, "%s * %s, ", var->type_name, varnames[ORC_VAR_A1 + i]); } else { fprintf(output, "uint%d_t * %s, ", var->size*8, varnames[ORC_VAR_A1 + i]); } } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_S1 + i]; if (var->size) { if (var->type_name) { fprintf(output, "%s * %s, ", var->type_name, varnames[ORC_VAR_S1 + i]); } else { fprintf(output, "uint%d_t * %s, ", var->size*8, varnames[ORC_VAR_S1 + i]); } } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_P1 + i]; if (var->size) { fprintf(output, "int %s, ", varnames[ORC_VAR_P1 + i]); } } fprintf(output, "int n)\n"); fprintf(output, "{\n"); fprintf(output, " static int p_inited = 0;\n"); fprintf(output, " static OrcProgram *p = NULL;\n"); fprintf(output, " OrcExecutor _ex, *ex = &_ex;\n"); fprintf(output, "\n"); fprintf(output, " if (!p_inited) {\n"); fprintf(output, " MUTEX_LOCK ();\n"); fprintf(output, " if (!p_inited) {\n"); fprintf(output, " OrcCompileResult result;\n"); fprintf(output, "\n"); fprintf(output, " p = orc_program_new ();\n"); fprintf(output, " orc_program_set_name (p, \"%s\");\n", p->name); for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_D1 + i]; if (var->size) { fprintf(output, " orc_program_add_destination (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_D1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_S1 + i]; if (var->size) { fprintf(output, " orc_program_add_source (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_S1 + i]); } } for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_A1 + i]; if (var->size) { fprintf(output, " orc_program_add_accumulator (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_A1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_C1 + i]; if (var->size) { if (var->value != 0x80000000) { fprintf(output, " orc_program_add_constant (p, %d, %u, \"%s\");\n", var->size, var->value, varnames[ORC_VAR_C1 + i]); } else { fprintf(output, " orc_program_add_constant (p, %d, 0x%08x, \"%s\");\n", var->size, var->value, varnames[ORC_VAR_C1 + i]); } } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_P1 + i]; if (var->size) { fprintf(output, " orc_program_add_parameter (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_P1 + i]); } } for(i=0;i<16;i++){ var = &p->vars[ORC_VAR_T1 + i]; if (var->size) { fprintf(output, " orc_program_add_temporary (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_T1 + i]); } } fprintf(output, "\n"); for(i=0;in_insns;i++){ OrcInstruction *insn = p->insns + i; if (p->vars[insn->src_args[1]].size != 0) { fprintf(output, " orc_program_append (p, \"%s\", %s, %s, %s);\n", insn->opcode->name, enumnames[insn->dest_args[0]], enumnames[insn->src_args[0]], enumnames[insn->src_args[1]]); } else { fprintf(output, " orc_program_append_ds (p, \"%s\", %s, %s);\n", insn->opcode->name, enumnames[insn->dest_args[0]], enumnames[insn->src_args[0]]); } } fprintf(output, "\n"); fprintf(output, " result = orc_program_compile (p);\n"); fprintf(output, " if (!ORC_COMPILE_RESULT_IS_SUCCESSFUL (result)) {\n"); fprintf(output, " abort ();\n"); fprintf(output, " }\n"); fprintf(output, " }\n"); fprintf(output, " p_inited = TRUE;\n"); fprintf(output, " MUTEX_UNLOCK ();\n"); fprintf(output, " }\n"); fprintf(output, "\n"); //fprintf(output, " orc_executor_set_program (ex, p);\n"); fprintf(output, " ex->program = p;\n"); //fprintf(output, " orc_executor_set_n (ex, n);\n"); fprintf(output, " ex->n = n;\n"); for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_D1 + i]; if (var->size) { //fprintf(output, " orc_executor_set_array (ex, %s, %s);\n", // enumnames[ORC_VAR_D1 + i], varnames[ORC_VAR_D1 + i]); fprintf(output, " ex->arrays[%s] = %s;\n", enumnames[ORC_VAR_D1 + i], varnames[ORC_VAR_D1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_S1 + i]; if (var->size) { //fprintf(output, " orc_executor_set_array (ex, %s, %s);\n", // enumnames[ORC_VAR_S1 + i], varnames[ORC_VAR_S1 + i]); fprintf(output, " ex->arrays[%s] = %s;\n", enumnames[ORC_VAR_S1 + i], varnames[ORC_VAR_S1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_P1 + i]; if (var->size) { //fprintf(output, " orc_executor_set_param (ex, %s, %s);\n", // enumnames[ORC_VAR_P1 + i], varnames[ORC_VAR_P1 + i]); fprintf(output, " ex->params[%s] = %s;\n", enumnames[ORC_VAR_P1 + i], varnames[ORC_VAR_P1 + i]); } } fprintf(output, "\n"); fprintf(output, " orc_executor_run (ex);\n"); //fprintf(output, " ((void (*)(OrcExecutor *))ex->program->code_exec)(ex);\n"); for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_A1 + i]; if (var->size) { fprintf(output, " *%s = orc_executor_get_accumulator (ex, %s);\n", varnames[ORC_VAR_A1 + i], enumnames[ORC_VAR_A1 + i]); } } fprintf(output, "}\n"); } void output_code_test (OrcProgram *p, FILE *output) { OrcVariable *var; int i; fprintf(output, " /* %s */\n", p->name); fprintf(output, " {\n"); fprintf(output, " OrcProgram *p = NULL;\n"); fprintf(output, " int ret;\n"); fprintf(output, "\n"); fprintf(output, " OrcCompileResult result;\n"); fprintf(output, "\n"); fprintf(output, " printf (\"%s:\\n\");\n", p->name); fprintf(output, " p = orc_program_new ();\n"); fprintf(output, " orc_program_set_name (p, \"%s\");\n", p->name); for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_D1 + i]; if (var->size) { fprintf(output, " orc_program_add_destination (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_D1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_S1 + i]; if (var->size) { fprintf(output, " orc_program_add_source (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_S1 + i]); } } for(i=0;i<4;i++){ var = &p->vars[ORC_VAR_A1 + i]; if (var->size) { fprintf(output, " orc_program_add_accumulator (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_A1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_C1 + i]; if (var->size) { fprintf(output, " orc_program_add_constant (p, %d, %d, \"%s\");\n", var->size, var->value, varnames[ORC_VAR_C1 + i]); } } for(i=0;i<8;i++){ var = &p->vars[ORC_VAR_P1 + i]; if (var->size) { fprintf(output, " orc_program_add_parameter (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_P1 + i]); } } for(i=0;i<16;i++){ var = &p->vars[ORC_VAR_T1 + i]; if (var->size) { fprintf(output, " orc_program_add_temporary (p, %d, \"%s\");\n", var->size, varnames[ORC_VAR_T1 + i]); } } fprintf(output, "\n"); for(i=0;in_insns;i++){ OrcInstruction *insn = p->insns + i; if (insn->src_args[1] != -1) { fprintf(output, " orc_program_append (p, \"%s\", %s, %s, %s);\n", insn->opcode->name, enumnames[insn->dest_args[0]], enumnames[insn->src_args[0]], enumnames[insn->src_args[1]]); } else { fprintf(output, " orc_program_append_ds (p, \"%s\", %s, %s);\n", insn->opcode->name, enumnames[insn->dest_args[0]], enumnames[insn->src_args[0]]); } } fprintf(output, "\n"); fprintf(output, " ret = orc_test_compare_output (p);\n"); fprintf(output, " if (!ret) {\n"); fprintf(output, " error = TRUE;\n"); fprintf(output, " }\n"); fprintf(output, "\n"); fprintf(output, " orc_program_free (p);\n"); fprintf(output, " }\n"); fprintf(output, "\n"); }