Skip to content

Commit

Permalink
Complete CG for class
Browse files Browse the repository at this point in the history
Fix frames mechanism
  • Loading branch information
Mohammed Ghanem committed May 23, 2016
1 parent 28cd73a commit 6b49f6f
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 147 deletions.
27 changes: 21 additions & 6 deletions src/Code Generator/AsmGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,12 @@ void AsmGenerator::f_greater_or_equal_operation(string dest_reg,string reg1,stri
void AsmGenerator::add_instruction(string instruction)
{
instruction += "\n";
(current_stream == MAIN_STREAM) ? main_stream << instruction: functions_stream << instruction;
if (current_stream == MAIN_STREAM)
main_stream << instruction;
if (current_stream == FUNCUTION_STREAM)
functions_stream << instruction;
if (current_stream == TEMP_STEARM)
temp_stream << instruction;
}

void AsmGenerator::add_data(string data_instruction)
Expand All @@ -583,7 +588,12 @@ void AsmGenerator::comment(string comment_message)
void AsmGenerator::system_call(int system_call_code)
{
AsmGenerator::li("v0",system_call_code);
(current_stream == MAIN_STREAM) ? main_stream << "syscall\n" : functions_stream << "syscall\n";
if (current_stream == MAIN_STREAM)
main_stream << "syscall\n";
if (current_stream == FUNCUTION_STREAM)
functions_stream << "syscall\n";
if (current_stream == TEMP_STEARM)
temp_stream << "syscall\n";
}

void AsmGenerator::sbrk (string amount_reg,string returned_address_memory)
Expand Down Expand Up @@ -643,26 +653,30 @@ void AsmGenerator::f_move(string dest_reg,string source_reg)
void AsmGenerator::function_prologue (int frame_size)
{
AsmGenerator::comment("Callee Prologue:");
AsmGenerator::add_instruction("\tsubu $sp, $sp, 8");
AsmGenerator::add_instruction("\tsubu $sp, $sp, 12");
AsmGenerator::add_instruction("\tsw $fp, 0($sp)");
AsmGenerator::add_instruction("\tsw $ra, 4($sp)");
AsmGenerator::add_instruction("\taddi $fp, $sp, 8");
AsmGenerator::add_instruction("\tsw $a3, 8($sp)");
AsmGenerator::add_instruction("\taddi $fp, $sp, 12");
if (frame_size > 0 ) {
AsmGenerator::comment("# Reserve space (" + to_string(frame_size) + "b) for function local vars.");
AsmGenerator::add_instruction("\tsubu $sp, $sp, " + to_string(frame_size));
}
AsmGenerator::add_instruction("");

AsmGenerator::functions_stream << AsmGenerator::temp_stream.rdbuf();
}

void AsmGenerator::function_epilogue(int frame_size)
{
AsmGenerator::comment("Callee Epilogue:");
if (frame_size > 0) {
AsmGenerator::add_instruction("\taddu $fp, $sp, "+to_string(frame_size));
AsmGenerator::add_instruction("\taddu $sp, $sp, "+to_string(frame_size));
}
AsmGenerator::add_instruction("\tlw $a3, 8($sp)");
AsmGenerator::add_instruction("\tlw $ra, 4($sp)");
AsmGenerator::add_instruction("\tlw $fp, 0($sp)");
AsmGenerator::add_instruction("\taddu $sp, $sp, 8" );
AsmGenerator::add_instruction("\taddu $sp, $sp, 12" );
AsmGenerator::add_instruction("");
}

Expand Down Expand Up @@ -790,6 +804,7 @@ void AsmGenerator::strcpy()
ofstream AsmGenerator::assembly_code_file;
stringstream AsmGenerator::data_stream;
stringstream AsmGenerator::main_stream;
stringstream AsmGenerator::temp_stream;
stringstream AsmGenerator::functions_stream;

map<string,int> AsmGenerator::strings_map;
Expand Down
4 changes: 3 additions & 1 deletion src/Code Generator/AsmGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

#define MAIN_STREAM 0
#define FUNCUTION_STREAM 1
#define TEMP_STEARM 2 //used when generate code for function body (becase we need to know to mush local variables)

using namespace std;

class AsmGenerator{
private:
public:
static ofstream assembly_code_file;
static stringstream data_stream;
static stringstream main_stream;
static stringstream temp_stream;
static stringstream functions_stream;

static int current_stream;
Expand Down
Loading

1 comment on commit 6b49f6f

@ghanem-mhd
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sample:

Input:

In index.php file add the following:

class XY
{
    int $x;
    int $y;
    string $className;

    function sum (): int {
      return $x + $y;
    }

    function sub (): int {
      return $x + $y;
    }

    function mult (): int {
      return $x * $y;
    }

    function div (): int {
      return $x / $y;
    }

    function to_string(): string {
        return $className +" $x $y";
    }
}


XY $obj = new XY();

$obj->x = 10;
$obj->y = 5;
$obj->className = "CLass XY";

echo $obj->sum();
echo $obj->sub();
echo $obj->mult();
echo $obj->div();
echo $obj->to_string();

Output:

25
15
250
2
CLass XY 10 5

Please sign in to comment.