Skip to content

Commit

Permalink
compiler: build functions separately
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Nov 20, 2012
1 parent 5b18f5e commit 05f5592
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -25,8 +25,8 @@ test: test-runner can
@./test-runner parser
@./test-runner scope
@./test-runner fullgen
@./test-runner hir
@./test-runner lir
# @./test-runner hir
# @./test-runner lir
@./test-runner functional
@./test-runner binary
@./test-runner numbers
Expand Down
52 changes: 29 additions & 23 deletions src/code-space.cc
Expand Up @@ -172,31 +172,37 @@ char* CodeSpace::Compile(const char* filename,
Root r(heap());
Masm masm(this);

if (true) {
// Generate CFG with SSA
HIRGen hir(heap(), &r, chunk->filename());

hir.Build(ast);

// Generate low-level representation:
// For each root in reverse order generate lir
// (Generate children first, parents later)
HIRBlockList::Item* head = hir.roots()->head();
for (; head != NULL; head = head->next()) {
// Generate LIR
LGen lir(&hir, chunk->filename(), head->value());

// Generate Masm code
lir.Generate(&masm, heap()->source_map());
}
} else {
Fullgen f(heap(), &r, chunk->filename());
FunctionIterator it(ast);

for (; !it.IsEnded(); it.Advance()) {
FunctionLiteral* current = it.Value();

if (true) {
// Generate CFG with SSA
HIRGen hir(heap(), &r, chunk->filename());

hir.Build(current);

// Create instruction list
f.Build(ast);
// Generate low-level representation:
// For each root in reverse order generate lir
// (Generate children first, parents later)
HIRBlockList::Item* head = hir.roots()->head();
for (; head != NULL; head = head->next()) {
// Generate LIR
LGen lir(&hir, chunk->filename(), head->value());

// Generate instructions
f.Generate(&masm);
// Generate Masm code
lir.Generate(&masm, heap()->source_map());
}
} else {
Fullgen f(heap(), &r, chunk->filename());

// Create instruction list
f.Build(current);

// Generate instructions
f.Generate(&masm);
}
}

// Store root
Expand Down
7 changes: 4 additions & 3 deletions src/fullgen.cc
Expand Up @@ -230,10 +230,11 @@ void Fullgen::LoadArguments(FunctionLiteral* fn) {
FInstruction* Fullgen::VisitFunction(AstNode* stmt) {
FunctionLiteral* fn = FunctionLiteral::Cast(stmt);

if (fn->label() == NULL) {
fn->label(new Label());
}

if (current_function()->root_ast() == stmt) {
if (fn->label() == NULL) {
fn->label(new Label());
}
current_function()->body = new FLabel(fn->label());
Add(current_function()->body);
current_function()->entry = new FEntry(stmt->context_slots());
Expand Down
38 changes: 17 additions & 21 deletions src/hir.cc
Expand Up @@ -57,29 +57,25 @@ HIRGen::~HIRGen() {


void HIRGen::Build(AstNode* root) {
HIRInstruction* hroot = new HIRFunction(root);
hroot->Init(this, NULL);
work_queue_.Push(hroot);
HIRFunction* current = new HIRFunction(root);
current->Init(this, NULL);

while (work_queue_.length() != 0) {
HIRFunction* current = HIRFunction::Cast(work_queue_.Shift());
HIRBlock* b = CreateBlock(current->ast()->stack_slots());
set_current_block(b);
set_current_root(b);

HIRBlock* b = CreateBlock(current->ast()->stack_slots());
set_current_block(b);
set_current_root(b);
roots_.Push(b);

roots_.Push(b);

// Lazily create labels
if (current->ast()->label() == NULL) {
current->ast()->label(new Label());
}
// Lazily create labels
if (current->ast()->label() == NULL) {
current->ast()->label(new Label());
}

Visit(current->ast());
Visit(current->ast());

set_current_root(NULL);
}
set_current_root(NULL);

// Optimize
PrunePhis();
FindReachableBlocks();
DeriveDominators();
Expand Down Expand Up @@ -654,11 +650,12 @@ HIRInstruction* HIRGen::Visit(AstNode* stmt) {
HIRInstruction* HIRGen::VisitFunction(AstNode* stmt) {
FunctionLiteral* fn = FunctionLiteral::Cast(stmt);

if (fn->label() == NULL) {
fn->label(new Label());
}

if (current_root() == current_block() &&
current_block()->IsEmpty()) {
if (fn->label() == NULL) {
fn->label(new Label());
}
Add(new HIREntry(fn->label(), stmt->context_slots()));
HIRInstruction* index = NULL;
int flat_index = 0;
Expand Down Expand Up @@ -753,7 +750,6 @@ HIRInstruction* HIRGen::VisitFunction(AstNode* stmt) {
HIRFunction* f = new HIRFunction(stmt);
f->arg_count = fn->args()->length();

work_queue_.Push(f);
return Add(f);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/visitor.cc
Expand Up @@ -122,6 +122,20 @@ void FunctionIterator::Advance() {
}


AstNode* FunctionIterator::VisitCall(AstNode* node) {
FunctionLiteral* fn = FunctionLiteral::Cast(node);

Visit(fn->variable());

AstList::Item* arg = fn->args()->head();
for (; arg != NULL; arg = arg->next()) {
Visit(arg->value());
}

return node;
}


AstNode* FunctionIterator::VisitFunction(AstNode* node) {
work_queue_.Push(node);
return node;
Expand Down
1 change: 1 addition & 0 deletions src/visitor.h
Expand Up @@ -139,6 +139,7 @@ class FunctionIterator : public Visitor<AstNode> {
void Advance();
FunctionLiteral* Value();

AstNode* VisitCall(AstNode* node);
AstNode* VisitFunction(AstNode* node);

private:
Expand Down

0 comments on commit 05f5592

Please sign in to comment.