Compare commits

..

No commits in common. "95830097605d60de9c913d76a833c4fc58608a99" and "71f757ecdcece890da2ab0f60244573c9efb1175" have entirely different histories.

5 changed files with 6 additions and 72 deletions

View File

@ -11,7 +11,4 @@ y.tab.h
# VSL treefiles # VSL treefiles
*.ast *.ast
*.sast *.sast
*.sym *.sym
*.bin
*.s
*.S

View File

@ -1,8 +1,5 @@
#include <vslc.h> #include <vslc.h>
#define ASM(opcode, args...) puts("\t"#opcode"\t"#args)
#define LABEL(label) printf("_%s:\n", (char*)label)
/**Generate table of strings in a rodata section. */ /**Generate table of strings in a rodata section. */
void generate_stringtable ( void ); void generate_stringtable ( void );
/**Declare global variables in a bss section */ /**Declare global variables in a bss section */
@ -23,8 +20,6 @@ static const char *record[6] = {
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9" "%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
}; };
static uint64_t fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list);
void void
generate_program ( void ) generate_program ( void )
{ {
@ -35,21 +30,12 @@ generate_program ( void )
*/ */
// TODO: Implement // TODO: Implement
// - Generate string table
// - Declare global variables
// - Generate code for all functions // - Generate code for all functions
// - Generate main (function already implemented) by assigning either the // - Generate main (function already implemented) by assigning either the
// function named main or the first function of the source file if no // function named main or the first function of the source file if no
// main exists. // main exists.
generate_stringtable();
generate_global_variables();
symbol_t **global_list;
uint64_t no_globals = fetch_symbols(global_names, &global_list);
for (uint64_t g = 0; g < no_globals; g++)
{
if (global_list[g]->type == SYM_FUNCTION)
generate_function(global_list[g]);
}
} }
void void
@ -58,48 +44,24 @@ generate_stringtable ( void )
/* These can be used to emit numbers, strings and a run-time /* These can be used to emit numbers, strings and a run-time
* error msg. from main * error msg. from main
*/ */
puts("# DATA SECTION");
puts(".data"); puts(".data");
puts(".intout:\t.asciz \"\%ld \""); puts(".intout:\t.asciz \"\%ld \"");
puts(".strout:\t.asciz \"\%s \""); puts(".strout:\t.asciz \"\%s \"");
puts(".errout:\t.asciz \"Wrong number of arguments\""); puts(".errout:\t.asciz \"Wrong number of arguments\"");
for (uint64_t s = 0; s < stringc; s++) // TODO: Implement the rest
{
printf(".STR%03ld:\t.asciz %s\n", s, string_list[s]);
}
putchar('\n');
} }
void void
generate_global_variables ( void ) generate_global_variables ( void )
{ {
symbol_t **global_list; // TODO: Create a .bss section and declare global variables
uint64_t no_globals = fetch_symbols(global_names, &global_list);
puts("# GLOBAL VARIABLES");
puts(".bss");
puts(".align 8");
for (uint64_t g = 0; g < no_globals; g++) {
if (global_list[g]->type == SYM_GLOBAL_VAR)
printf(".%s:\n", global_list[g]->name);
}
putchar('\n');
free(global_list);
} }
void void
generate_function ( symbol_t *function ) generate_function ( symbol_t *function )
{ {
// TODO: Generate code for declaring and entering function, then generate its body // TODO: Generate code for declaring and entering function, then generate its body
node_t *func_root = function->node;
LABEL(function->name);
ASM(pushq, %rbp);
ASM(movq, %rsp, %rbp);
putchar('\n');
} }
void void
@ -160,13 +122,3 @@ generate_main ( symbol_t *first )
puts ( "\tmovq %rax, %rdi" ); puts ( "\tmovq %rax, %rdi" );
puts ( "\tcall exit" ); puts ( "\tcall exit" );
} }
static uint64_t
fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list)
{
uint64_t no_symbols = tlhash_size(symbol_table);
*symbol_list = malloc(no_symbols * sizeof(symbol_t));
tlhash_values(symbol_table, (void **)*symbol_list );
return no_symbols;
}

View File

@ -17,7 +17,7 @@ PS5_EXAMPLES := $(patsubst ps5-codegen1/%.vsl, ps5-codegen1/%.S, $(wildcard ps5-
PS5_OBJECTS := $(PS5_EXAMPLES:.S=.bin) PS5_OBJECTS := $(PS5_EXAMPLES:.S=.bin)
OBJECTS := $(PS5_OBJECTS) $(PS4_EXAMPLES:.sym=.bin) $(PS3_EXAMPLES:.sast=.bin) $(PS2_EXAMPLES:.ast=.bin) OBJECTS := $(PS5_OBJECTS) $(PS4_EXAMPLES:.sym=.bin) $(PS3_EXAMPLES:.sast=.bin) $(PS2_EXAMPLES:.ast=.bin)
all: clean $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES) $(PS5_EXAMPLES) all: $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES)
ps2: $(PS2_EXAMPLES) ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES) ps3: $(PS3_EXAMPLES)
ps4: $(PS4_EXAMPLES) ps4: $(PS4_EXAMPLES)

View File

@ -1,12 +0,0 @@
var global_var0, global_var1
func my_func(param0, param1) begin
var a
return 0
end
func main() begin
var a
print "a string"
return 0
end

View File

@ -1,7 +1,4 @@
// Checking symbol shadowing // Checking symbol shadowing
var a
func shadow() begin func shadow() begin
var a, b var a, b
a := 1 a := 1