Compare commits

...

4 Commits

Author SHA1 Message Date
Øyvind Skaaden 9583009760 added some more test files to vsl 2022-03-31 23:12:42 +02:00
Øyvind Skaaden b6a3b145e0 clean before recompile 2022-03-31 23:12:24 +02:00
Øyvind Skaaden 36680f5c8d Finished two of the generator parts
- string table
-global vars

beginnings of function
2022-03-31 23:12:12 +02:00
Øyvind Skaaden 7b0cf372c8 Added some outfiles 2022-03-31 23:11:17 +02:00
5 changed files with 72 additions and 6 deletions

View File

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

View File

@ -1,5 +1,8 @@
#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. */
void generate_stringtable ( void );
/**Declare global variables in a bss section */
@ -20,6 +23,8 @@ static const char *record[6] = {
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
};
static uint64_t fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list);
void
generate_program ( void )
{
@ -30,12 +35,21 @@ generate_program ( void )
*/
// TODO: Implement
// - Generate string table
// - Declare global variables
// - Generate code for all functions
// - Generate main (function already implemented) by assigning either the
// function named main or the first function of the source file if no
// 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
@ -44,24 +58,48 @@ generate_stringtable ( void )
/* These can be used to emit numbers, strings and a run-time
* error msg. from main
*/
puts("# DATA SECTION");
puts(".data");
puts(".intout:\t.asciz \"\%ld \"");
puts(".strout:\t.asciz \"\%s \"");
puts(".errout:\t.asciz \"Wrong number of arguments\"");
// TODO: Implement the rest
for (uint64_t s = 0; s < stringc; s++)
{
printf(".STR%03ld:\t.asciz %s\n", s, string_list[s]);
}
putchar('\n');
}
void
generate_global_variables ( void )
{
// TODO: Create a .bss section and declare global variables
symbol_t **global_list;
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
generate_function ( symbol_t *function )
{
// 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
@ -122,3 +160,13 @@ generate_main ( symbol_t *first )
puts ( "\tmovq %rax, %rdi" );
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)
OBJECTS := $(PS5_OBJECTS) $(PS4_EXAMPLES:.sym=.bin) $(PS3_EXAMPLES:.sast=.bin) $(PS2_EXAMPLES:.ast=.bin)
all: $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES)
all: clean $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES) $(PS5_EXAMPLES)
ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES)
ps4: $(PS4_EXAMPLES)

View File

@ -0,0 +1,12 @@
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,4 +1,7 @@
// Checking symbol shadowing
var a
func shadow() begin
var a, b
a := 1