public function gen() { $ir = ""; $leftname = "\\Codegen\\Generator\\" . \Misc::baseClass($this->obj->left); $left = new $leftname($this->obj->left); $ir .= $left->gen(); $rightname = "\\Codegen\\Generator\\" . \Misc::baseClass($this->obj->right); $right = new $rightname($this->obj->right); $ir .= $right->gen(); $leftreg = \Tables::$register_alloc - 1; $rightreg = $leftreg + 1; if ($this->obj->left instanceof \Parser\Parsers\Integer) { $reg1 = \Tables::alloc(); /* holds the left value */ $ir .= "\t%{$reg1} = load i32* %{$leftreg}" . PHP_EOL; } else { $reg1 = $leftreg; } if ($this->obj->right instanceof \Parser\Parsers\Integer) { $reg2 = \Tables::alloc(); /* holds the right value */ $ir .= "\t%{$reg2} = load i32* %{$rightreg}" . PHP_EOL; } else { $reg2 = $rightreg; } $reg3 = \Tables::alloc(); /* holds the result */ $ir .= "\t%{$reg3} = sub nsw i32 %{$reg1}, %{$reg2}" . PHP_EOL; $this->resultreg = $reg3; return $ir; }
public function gen() { $reg = \Tables::alloc(); $ir = "\t%{$reg} = alloca i32" . PHP_EOL; $ir .= "\tstore {$this->arg()}, i32* %{$reg}" . PHP_EOL; return $ir; }
public function gen() { if (is_object($this->obj->content[0])) { /* it is a arithmetic expression */ $ir = ""; $name = "\\Codegen\\Generator\\" . \Misc::baseClass($this->obj->content[0]); $obj = new $name($this->obj->content[0]); $var =& \Tables::updateLocalVariable($this->obj->function, $this->obj->name); $ir .= $obj->gen(); for ($i = 0; $i <= $this->obj->line; $i++) { if (isset($var->type[$i])) { $type = $var->type[$i]; } } $var->type[$this->obj->line] = $type | \Tables::POINTER; $var->type[$this->obj->line] &= ~\Tables::POINTER; $var->name = \Tables::$register_alloc; } elseif (!is_null(\Tables::localVariable($this->obj->function, $this->obj->name))) { /* allocate and get everything ready */ $reg1 = \Tables::alloc(); /* the reg number */ $reg2 = \Tables::alloc(); /* the reg number */ $var =& \Tables::updateLocalVariable($this->obj->function, $this->obj->name); /* our reference to the variable */ /* get the correct type */ for ($i = 0; $i <= $this->obj->line; $i++) { if (isset($var->type[$i])) { $type = $var->type[$i]; } } $type = \Misc::getType($type); /* its type */ $name = "\\Codegen\\Generator\\" . \Misc::baseClass($this->obj->content[0]); $obj = new $name($this->obj->content[0]); /* the code that is to be moved into the reg */ $code = $obj->arg(); /* change the name of the variable to the new register */ $var->name = $reg2; /* generate the code */ $ir = "\t%{$reg1} = alloca {$type}" . PHP_EOL; /* allocate the register */ $ir .= "\tstore " . $code . ", {$type}* %{$reg1}" . PHP_EOL; /* store our value in it */ $ir .= "\t%{$reg2} = load {$type}* %{$reg1}" . PHP_EOL; /* move it into a register we don't have to reference it as a pointer */ } return $ir; }
public function gen() { $ir = ""; if (\Tables::declaration($this->obj->name) || \Tables::func($this->obj->name)) { $gen = array(); $ir .= "\t; prepare the arguments" . PHP_EOL; for ($i = 0; $i < count($this->obj->content); $i++) { $arg = $this->obj->content[$i]; if (\Misc::baseClass($arg) == "Addition" || \Misc::baseClass($arg) == "Subtraction" || \Misc::baseClass($arg) == "Multiplication" || \Misc::baseClass($arg) == "Division" || \Misc::baseClass($arg) == "Call") { $name = "\\Codegen\\Generator\\" . \Misc::baseClass($arg); $obj = new $name($arg); $ir .= $obj->gen(); $gen[$i] = $obj; } } if (\Tables::declaration($this->obj->name)) { $dec = \Tables::getDeclaration($this->obj->name); } elseif (\Tables::func($this->obj->name)) { $dec = \Tables::getFunc($this->obj->name); } $reg = \Tables::alloc(); $this->result = $reg; $ir .= "\t; call {$dec->name}" . PHP_EOL; if (empty($dec->args)) { $ir .= "\t%{$reg} = call {$dec->return} @{$dec->name}"; } else { $ir .= "\t%{$reg} = call {$dec->return} {$dec->args}* @{$dec->name}"; } if (count($this->obj->content) > 0) { for ($i = 0; $i < count($this->obj->content); $i++) { $arg = $this->obj->content[$i]; if (isset($gen[$i]) && !empty($gen[$i])) { $obj = $gen[$i]; } else { $name = "\\Codegen\\Generator\\" . \Misc::baseClass($arg); $obj = new $name($arg); } $ir .= $obj->arg() . ", "; } $ir = substr($ir, 0, -2); } $ir .= ")" . PHP_EOL; $ir .= "\t; end of call to {$dec->name}" . PHP_EOL . PHP_EOL; } else { error("unknown function in line {$this->obj->line}"); } return $ir; }