Ejemplo n.º 1
0
void write_globals()
{
    int globals_width = list_length(bytecode->globals);
    printf("Need to allocate %i bytes for global variables\n", globals_width);
    if(globals_width == 0) return;

    fprintf(output, ";Declaring global variables\n");
    fprintf(output, "\tmovlw %i\n", globals_width);
    fprintf(output, "\tsubwf H'0c'\n\n"); // Move frame pointer to make room for globals
    
    allocate_variables(bytecode->globals);
}
Ejemplo n.º 2
0
void write_functions()
{
    fprintf(output, "main_loop:\n");
    fprintf(output, "\tcall func_main\n");
    fprintf(output, "\tgoto main_loop\n");

    List *functions = (List *) bytecode->functions;
    while(functions->data != NULL)
    {
        Function *function = functions->data;
        // Write function label
        fprintf(output, "func_%s:\n", function->name);

        // A stack frame needs 1 byte per static variable + 1 byte for saved frame pointer
        int frame_width = list_length(function->symbol_table) + 1;
        printf("Need to allocate %i byte stack frame for function %s\n", frame_width, function->name);

        fprintf(output, "; Saving frame pointer\n");
        fprintf(output, "\tmovf H'0c', 0\n"); // Load top of stack - 1 into FSR
        fprintf(output, "\taddlw -1\n");
        fprintf(output, "\tmovwf H'04'\n");
        fprintf(output, "\tmovf H'0c', 0\n"); // Store the current frame pointer into the top of the stack
        fprintf(output, "\tmovwf H'00'\n");
        fprintf(output, "\tmovlw %i\n", frame_width); // Now set the new frame pointer
        fprintf(output, "\tsubwf H'0c'\n\n");

        allocate_variables(function->symbol_table);

        List *quads = function->statements;
        while(quads->data != NULL)
        {
            Quad *quad = quads->data;
            write_quad(quad, function);
            quads = quads->next;
        }

        // Restore previous frame pointer and return
        fprintf(output, "; Restoring previous frame pointer\n");
        fprintf(output, "\tmovf H'0c', 0\n"); // Load previous frame pointer
        fprintf(output, "\taddlw %i\n", frame_width - 1);
        fprintf(output, "\tmovwf H'04'\n");
        fprintf(output, "\tmovf H'00', 0\n");
        fprintf(output, "\tmovwf H'0c'\n");
        fprintf(output, "\treturn\n\n");
        
        functions = functions->next;
    }

    fprintf(output, "\tend\n");
}
Ejemplo n.º 3
0
Grid_2D& Grid_2D :: operator=(const Grid_2D &rhs)
{
	if(this != &rhs)
	{
		delete this->connections;
		delete this->displays;
		delete this->weights;
		delete this->path;
		this->connections = NULL;
		this->displays = NULL;
	    this->weights = NULL;
	    this->path = NULL;
	    allocate_variables(rhs);
	}
	return *this;
}
Ejemplo n.º 4
0
Grid_2D :: Grid_2D(const Grid_2D &rhs)
{
	allocate_variables(rhs);
}