Compare commits
4 Commits
71f757ecdc
...
9583009760
Author | SHA1 | Date | |
---|---|---|---|
9583009760 | |||
b6a3b145e0 | |||
36680f5c8d | |||
7b0cf372c8 |
5
exercises/05/.gitignore
vendored
5
exercises/05/.gitignore
vendored
@ -11,4 +11,7 @@ y.tab.h
|
|||||||
# VSL treefiles
|
# VSL treefiles
|
||||||
*.ast
|
*.ast
|
||||||
*.sast
|
*.sast
|
||||||
*.sym
|
*.sym
|
||||||
|
*.bin
|
||||||
|
*.s
|
||||||
|
*.S
|
@ -1,5 +1,8 @@
|
|||||||
#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 */
|
||||||
@ -20,6 +23,8 @@ 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 )
|
||||||
{
|
{
|
||||||
@ -30,12 +35,21 @@ 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
|
||||||
@ -44,24 +58,48 @@ 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\"");
|
||||||
|
|
||||||
// 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
|
void
|
||||||
generate_global_variables ( 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
|
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
|
||||||
@ -122,3 +160,13 @@ 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;
|
||||||
|
}
|
||||||
|
@ -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: $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES)
|
all: clean $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES) $(PS5_EXAMPLES)
|
||||||
ps2: $(PS2_EXAMPLES)
|
ps2: $(PS2_EXAMPLES)
|
||||||
ps3: $(PS3_EXAMPLES)
|
ps3: $(PS3_EXAMPLES)
|
||||||
ps4: $(PS4_EXAMPLES)
|
ps4: $(PS4_EXAMPLES)
|
||||||
|
12
exercises/05/vslc/vsl_programs/ps5-codegen1/globals.vsl
Normal file
12
exercises/05/vslc/vsl_programs/ps5-codegen1/globals.vsl
Normal 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
|
@ -1,4 +1,7 @@
|
|||||||
// Checking symbol shadowing
|
// Checking symbol shadowing
|
||||||
|
|
||||||
|
var a
|
||||||
|
|
||||||
func shadow() begin
|
func shadow() begin
|
||||||
var a, b
|
var a, b
|
||||||
a := 1
|
a := 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user