WONKY



LOG | FILES | OVERVIEW


F diff --git a/src/backend/asm/intel/intel_asm.c b/src/backend/asm/intel/intel_asm.c --- a/src/backend/asm/intel/intel_asm.c +++ b/src/backend/asm/intel/intel_asm.c
data=get_compile_data_for_intel_asm();
+ for(it=program->functions_without_a_definition->first;it!=NULL;it=it->prev)
+ import_function_definition(data,it->data);
+
+ for(it=program->external_objects_without_an_initialiser->first;it!=NULL;it=it->prev)
+ import_object_definition(data,it->data);
for(it=program->translation_units->first;it!=NULL;it=it->prev)
compile_translation_unit_to_intel_asm(data,it->data);
ret->type=COMPILE_TO_INTEL_ASM;
ret->errors=data->errors;
ret->instructions=data->instructions;
+ ret->data_block=data->data_block;
return ret;
}
void save_compiled_intel_asm(struct Compiled_Object_Intel_Asm *object,FILE *out)
{
struct Queue_Node *it;
+ for(it=object->data_block->first;it!=NULL;it=it->prev)
+ save_intel_asm_instruction(it->data,out);
+ for(it=object->instructions->first;it!=NULL;it=it->prev)
+ save_intel_asm_instruction(it->data,out);
+ }
+ void save_compiled_intel_asm_as_nasm(struct Compiled_Object_Intel_Asm *object,FILE *out)
+ {
+ struct Queue_Node *it;
+ fprintf(out,"[BITS 64]\n");
+ fprintf(out,"section .data\n");
+ for(it=object->data_block->first;it!=NULL;it=it->prev)
+ save_intel_asm_instruction(it->data,out);
+ fprintf(out,"\nsection .text\n");
+ fprintf(out,"\nextern _exit\n");
for(it=object->instructions->first;it!=NULL;it=it->prev)
save_intel_asm_instruction(it->data,out);
}
ret->errors=malloc(sizeof(struct Queue));
Queue_Init(ret->errors);
+ ret->data_block=malloc(sizeof(struct Queue));
+ Queue_Init(ret->data_block);
+
ret->instructions=malloc(sizeof(struct Queue));
Queue_Init(ret->instructions);
db=get_intel_asm_define_bytes(string->value,string_type->size);
label=get_intel_asm_new_unique_label(compile_data);
- Queue_Push_Front(compile_data->instructions,db);
- Queue_Push_Front(compile_data->instructions,label);
+ Queue_Push(compile_data->data_block,label);
+ Queue_Push(compile_data->data_block,db);
return get_intel_asm_label_location((struct Intel_Asm_Label*)label);
}
function->location=(struct Location*)location;
}
+ void export_function_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Definition *function)
+ {
+ wonky_assert(function!=NULL && function->type==ST_FUNCTION_DEFINITION);
+ Queue_Push(compile_data->instructions,get_intel_asm_export(gstr_dup(function->function->id->data,function->function->id->data+function->function->id->data_size,1024)));
+
+ }
+ void export_object_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Object_Declaration *object)
+ {
+ wonky_assert(object!=NULL && object->type==ST_OBJECT_DECLARATION);
+ Queue_Push(compile_data->instructions,get_intel_asm_export(gstr_dup(object->object->id->data,object->object->id->data+object->object->id->data_size,1024)));
+ }
+ void import_function_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Declaration *function)
+ {
+ wonky_assert(function!=NULL && function->type==ST_FUNCTION_DECLARATION);
+ Queue_Push(compile_data->instructions,get_intel_asm_import(gstr_dup(function->function->id->data,function->function->id->data+function->function->id->data_size,1024)));
+ }
+ void import_object_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Object_Declaration *object)
+ {
+ wonky_assert(object!=NULL && object->type==ST_OBJECT_DECLARATION);
+ wonky_assert(object->initializer==NULL);/*we only ask for objects without initialisers*/
+ Queue_Push(compile_data->instructions,get_intel_asm_import(gstr_dup(object->object->id->data,object->object->id->data+object->object->id->data_size,1024)));
+ }
#endif
F diff --git a/src/backend/asm/intel/intel_asm.h b/src/backend/asm/intel/intel_asm.h --- a/src/backend/asm/intel/intel_asm.h +++ b/src/backend/asm/intel/intel_asm.h
enum Compilation_Type type;
struct Queue *errors;
+ struct Queue *data_block;
struct Queue *instructions;
int number_of_anon_labels;
enum Compilation_Type type;
struct Queue *errors;
+ struct Queue *data_block;
struct Queue *instructions;
};
void compile_program_static_objects(struct Compile_Data *obj,struct Program *program);
void save_compiled_intel_asm(struct Compiled_Object_Intel_Asm *object,FILE *out);
+ void save_compiled_intel_asm_as_nasm(struct Compiled_Object_Intel_Asm *object,FILE *out);
struct Compile_Data_Intel_Asm* get_compile_data_for_intel_asm();
void intel_asm_anotate_denoted(struct Denoted *denoted,struct Compile_Data_Intel_Asm *compile_data);
void intel_asm_anotate_function(struct Compile_Data_Intel_Asm *compile_data,struct Denoted_Function *func);
+ void export_function_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Definition *function);
+ void export_object_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Object_Declaration *object);
+ void import_function_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Declaration *function);
+ void import_object_definition(struct Compile_Data_Intel_Asm *compile_data,struct AST_Object_Declaration *object);
#endif
F diff --git a/src/backend/asm/intel/intel_compile.c b/src/backend/asm/intel/intel_compile.c --- a/src/backend/asm/intel/intel_compile.c +++ b/src/backend/asm/intel/intel_compile.c
}
void compile_function_definition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Definition *def)
{
- push_intel_asm_instruction(compile_data,((struct Intel_Asm_Instruction*)def->function->location));
+ if(def->function->location==NULL)
+ {
+ def->function->location=(struct Location*)
+ get_intel_asm_label_location(
+ (struct Intel_Asm_Label*)get_intel_asm_label(gstr_dup(def->function->id->data,def->function->id->data+def->function->id->data_size,1024))
+ );
+
+ }
+ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)((struct Intel_Asm_Memory_Location_By_Label*)def->function->location)->label);
compile_compound_statement_to_intel_asm(compile_data,def->body);
/*if there are no returns we return jiberrish*/
F diff --git a/src/backend/asm/intel/intel_instruction.c b/src/backend/asm/intel/intel_instruction.c --- a/src/backend/asm/intel/intel_instruction.c +++ b/src/backend/asm/intel/intel_instruction.c
#define WONKY_INTEL_ASM_INSTRUCTION_C WONKY_INTEL_ASM_INSTRUCTION_C
#include <intel_instruction.h>
- struct Intel_Asm_Instruction* get_intel_asm_label(char *label)
+ struct Intel_Asm_Instruction* get_intel_asm_import(char *what)
+ {
+ return get_intel_asm_label_inner(what,INTEL_ASM_OP_IMPORT);
+ }
+ struct Intel_Asm_Instruction* get_intel_asm_export(char *what)
+ {
+ return get_intel_asm_label_inner(what,INTEL_ASM_OP_EXPORT);
+ }
+ struct Intel_Asm_Instruction* get_intel_asm_label_inner(char *label,enum Intel_Asm_Instruction_Type type)
{
+
struct Intel_Asm_Label *ret;
ret=malloc(sizeof(struct Intel_Asm_Label));
- ret->type=INTEL_ASM_OP_LABEL;
+ ret->type=type;
ret->label_name=label;
return (struct Intel_Asm_Instruction*)ret;
}
+ struct Intel_Asm_Instruction* get_intel_asm_label(char *label)
+ {
+ return get_intel_asm_label_inner(label,INTEL_ASM_OP_LABEL);
+ }
struct Intel_Asm_Instruction* get_intel_asm_new_unique_label(struct Compile_Data_Intel_Asm *compile_data)
{
[INTEL_ASM_OP_OR]=(map_entry)save_intel_asm_binary_instruction,
[INTEL_ASM_OP_NOP]=(map_entry)save_intel_asm_simple_instruction,
[INTEL_ASM_OP_LABEL]=(map_entry)save_intel_asm_label,
+ [INTEL_ASM_OP_IMPORT]=(map_entry)save_intel_asm_import,
+ [INTEL_ASM_OP_EXPORT]=(map_entry)save_intel_asm_export,
[INTEL_ASM_OP_POP]=(map_entry)save_intel_asm_unary_instruction,
[INTEL_ASM_OP_PUSH]=(map_entry)save_intel_asm_unary_instruction,
[INTEL_ASM_OP_DEFINE_BYTES]=(map_entry)save_intel_asm_define_bytes,
{
fprintf(out,"%s:\n",label->label_name);
}
+ void save_intel_asm_import(struct Intel_Asm_Label *im,FILE *out)
+ {
+ fprintf(out,"extern %s\n",im->label_name);
+ }
+ void save_intel_asm_export(struct Intel_Asm_Label *ex,FILE *out)
+ {
+ fprintf(out,"global %s:\n",ex->label_name);
+ }
void save_intel_asm_binary_instruction(struct Intel_Asm_Instruction_Binary *bin,FILE *out)
{
const char *map[INTEL_ASM_OP_END]
F diff --git a/src/backend/asm/intel/intel_instruction.h b/src/backend/asm/intel/intel_instruction.h --- a/src/backend/asm/intel/intel_instruction.h +++ b/src/backend/asm/intel/intel_instruction.h
unsigned char *bytes;
};
-
+ struct Intel_Asm_Instruction* get_intel_asm_import(char *what);
+ struct Intel_Asm_Instruction* get_intel_asm_export(char *what);
+ struct Intel_Asm_Instruction* get_intel_asm_label_inner(char *label,enum Intel_Asm_Instruction_Type type);
struct Intel_Asm_Instruction* get_intel_asm_label(char *label);
struct Intel_Asm_Instruction* get_intel_asm_new_unique_label(struct Compile_Data_Intel_Asm *compile_data);
struct Intel_Asm_Instruction* get_intel_asm_binary_instruction(struct Intel_Asm_Memory_Location *left,struct Intel_Asm_Memory_Location *right,enum Intel_Asm_Instruction_Type type);
void save_intel_asm_instruction(struct Intel_Asm_Instruction *instruction,FILE *out);
void save_intel_asm_label(struct Intel_Asm_Label *label,FILE *out);
+ void save_intel_asm_import(struct Intel_Asm_Label *im,FILE *out);
+ void save_intel_asm_export(struct Intel_Asm_Label *ex,FILE *out);
void save_intel_asm_binary_instruction(struct Intel_Asm_Instruction_Binary *bin,FILE *out);
void save_intel_asm_unary_instruction(struct Intel_Asm_Instruction_Unary *unary,FILE *out);
void save_intel_asm_jump_instruction(struct Intel_Asm_Instruction_Jump *jmp,FILE *out);
F diff --git a/src/backend/asm/intel/intel_instruction.hh b/src/backend/asm/intel/intel_instruction.hh --- a/src/backend/asm/intel/intel_instruction.hh +++ b/src/backend/asm/intel/intel_instruction.hh
INTEL_ASM_OP_POP,
INTEL_ASM_OP_PUSH,
INTEL_ASM_OP_DEFINE_BYTES,
+ INTEL_ASM_OP_EXPORT,
+ INTEL_ASM_OP_IMPORT,
INTEL_ASM_OP_END
};
F diff --git a/src/environment/command_arguments/gcc_arguments.c b/src/environment/command_arguments/gcc_arguments.c --- a/src/environment/command_arguments/gcc_arguments.c +++ b/src/environment/command_arguments/gcc_arguments.c
struct Queue *source_names;
ret=malloc(sizeof(struct Command_Arguments));
- ret->print_ast=ret->print_tokens=ret->transpile_to_js=ret->is_quiet=ret->compile_to_intel_asm=0;
+ ret->print_ast=ret->print_tokens=ret->transpile_to_js=ret->is_quiet=ret->compile_to_intel_asm=ret->compile_to_nasm=0;
ret->output_file=ret->javascript_extern_file=NULL;
source_names=malloc(sizeof(struct Queue));
}else if(gstr_cmp(*argv,"--transpile_to_js") || gstr_cmp(*argv,"-js"))
{
ret->transpile_to_js=1;
+ }else if(gstr_cmp(*argv,"--compile-nasm") || gstr_cmp(*argv,"-nasm"))
+ {
+ ret->compile_to_nasm=1;
}else if(gstr_cmp(*argv,"--compile-asm") || gstr_cmp(*argv,"-asm"))
{
ret->compile_to_intel_asm=1;
F diff --git a/src/environment/command_arguments/gcc_arguments.h b/src/environment/command_arguments/gcc_arguments.h --- a/src/environment/command_arguments/gcc_arguments.h +++ b/src/environment/command_arguments/gcc_arguments.h
char transpile_to_js:1;
char insert_html:1;
char compile_to_intel_asm:1;
+ char compile_to_nasm:1;
FILE* output_file;
F diff --git a/src/semantics/program/program.h b/src/semantics/program/program.h --- a/src/semantics/program/program.h +++ b/src/semantics/program/program.h
struct Linkage *external_linkage;
+ /*ASTs*/
struct Queue *functions_without_a_definition;
struct Queue *external_objects_without_an_initialiser;
};
F diff --git a/src/wonky.c b/src/wonky.c --- a/src/wonky.c +++ b/src/wonky.c
{
//transpile_to_javascript(command_arguments->output_file,program,command_arguments);
fprintf(stderr,"JS transpilation is not currently supported\n");
+ }else if(command_arguments->compile_to_nasm)
+ {
+ struct Compiled_Object_Intel_Asm *object;
+ object=compile_program_to_intel_asm(program);
+ save_compiled_intel_asm_as_nasm(object,stdout);
}else if(command_arguments->compile_to_intel_asm)
{
struct Compiled_Object_Intel_Asm *object;