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 analyze($ast, $parent = NULL) { /* holds the name of the currently compiled function */ $function = ""; foreach ($ast as $i => $node) { /* set the function name in the objects */ if ($node instanceof \Parser\Parsers\FunctionDefinition) { $function = $node->name; $node->function = $node->name; } elseif (!empty($node->function)) { $function = $node->function; } else { if (isset($node->name) && $node->name == "extern") { continue; } elseif (!empty($function)) { $node->function = $function; } else { if (!empty($parent->function)) { $node->function = $parent->function; } elseif (!empty($function)) { $node->function = $function; } } } /* set the arguments as local variables */ if ($node instanceof \Parser\Parsers\FunctionDefinition && count($node->arguments) > 0) { foreach ($node->arguments as $arg) { \Tables::addLocalVariable($function, $arg->name); } } if ($node instanceof \Parser\Parsers\Assignment) { /* deal with assignments */ if (is_object($node->content)) { if (\Tables::localVariable($node->function, $node->name)) { $var =& \Tables::updateLocalVariable($node->function, $node->name); } else { $var =& \Tables::addLocalVariable($node->function, $node->name); } $var->type[$node->line] = \Tables::I32; continue; } switch (\Misc::baseClass($node->content[0])) { case "String": $type = \Tables::STRING; break; case "Integer": $type = \Tables::I32; break; case "Addition": case "Subtraction": case "Multiplication": case "Division": $type = \Tables::I32; break; default: error("unknown assignment type '" . \Misc::baseClass($node->content[0]) . "' in line {$node->line}"); } if (\Tables::localVariable($node->function, $node->name)) { $var =& \Tables::updateLocalVariable($node->function, $node->name); } else { $var =& \Tables::addLocalVariable($node->function, $node->name); } $var->type[$node->line] = $type; } elseif (!$node instanceof \Parser\Parsers\Variable && count($node->content) > 0) { /* analyze recursively if needed */ if ($node instanceof \Parser\Parsers\FunctionDefinition && !empty($node->arguments)) { foreach ($node->arguments as $arg) { $arg->function = $function; } } $this->analyze($node->content, $node); } elseif ($node instanceof \Parser\Parsers\Variable) { /* do type inference based on printf */ if ($parent instanceof \Parser\Parsers\Call) { if ($parent->name == "printf") { $matches = array(); preg_match_all("/%[scdiu]/", $ast[0]->string, $matches); $format = substr($matches[0][$i - 1], -1); $type = 0; switch ($format) { case 'd': case 'i': case 'c': $type |= \Tables::I32; break; case 's': $type |= \Tables::STRING; } $var =& \Tables::updateLocalVariable($node->function, $node->name); if ($var == NULL) { continue; } $node->type[$node->line] = $type; } } } } }