diff --git a/clang/CodeGen/CodeGenFunction.cpp b/clang/CodeGen/CodeGenFunction.cpp index cfe5d9d95217..ff31a835ce95 100644 --- a/clang/CodeGen/CodeGenFunction.cpp +++ b/clang/CodeGen/CodeGenFunction.cpp @@ -16,7 +16,7 @@ #include "clang/Basic/TargetInfo.h" #include "clang/AST/AST.h" #include "llvm/DerivedTypes.h" -#include "llvm/Support/LLVMBuilder.h" +#include "llvm/Function.h" using namespace llvm; using namespace clang; using namespace CodeGen; @@ -101,9 +101,45 @@ const llvm::Type *CodeGenFunction::ConvertType(QualType T, SourceLocation Loc) { } -void CodeGenFunction::GenerateCode(FunctionDecl *FD) { +void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { const llvm::Type *Ty = ConvertType(FD->getType(), FD->getLocation()); - Ty->dump(); + llvm::Function *F = new Function(cast(Ty), + Function::ExternalLinkage, + FD->getName(), &CGM.getModule()); + BasicBlock *EntryBB = new BasicBlock("entry", F); + + // TODO: Walk the decls, creating allocas etc. + + Builder.SetInsertPoint(EntryBB); + + EmitStmt(FD->getBody()); } + + +//===----------------------------------------------------------------------===// +// Statement Emission +//===----------------------------------------------------------------------===// + +void CodeGenFunction::EmitStmt(const Stmt *S) { + assert(S && "Null statement?"); + + switch (S->getStmtClass()) { + default: + printf("Unimplemented stmt!\n"); + S->dump(); + break; + case Stmt::NullStmtClass: break; + case Stmt::CompoundStmtClass: EmitCompoundStmt(cast(*S)); break; + } +} + +void CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S) { + // FIXME: handle vla's etc. + + for (CompoundStmt::const_body_iterator I = S.body_begin(), E = S.body_end(); + I != E; ++I) + EmitStmt(*I); +} + diff --git a/clang/CodeGen/CodeGenFunction.h b/clang/CodeGen/CodeGenFunction.h index e7b1a05ebcc2..51f9e1bb51ad 100644 --- a/clang/CodeGen/CodeGenFunction.h +++ b/clang/CodeGen/CodeGenFunction.h @@ -14,15 +14,18 @@ #ifndef CODEGEN_CODEGENFUNCTION_H #define CODEGEN_CODEGENFUNCTION_H +#include "llvm/Support/LLVMBuilder.h" + namespace llvm { class Module; - class Type; namespace clang { class ASTContext; class FunctionDecl; class QualType; class SourceLocation; class TargetInfo; + class Stmt; + class CompoundStmt; namespace CodeGen { class CodeGenModule; @@ -32,12 +35,20 @@ namespace CodeGen { class CodeGenFunction { CodeGenModule &CGM; // Per-module state. TargetInfo &Target; + LLVMBuilder Builder; public: CodeGenFunction(CodeGenModule &cgm); const llvm::Type *ConvertType(QualType T, SourceLocation Loc); - void GenerateCode(FunctionDecl *FD); + void GenerateCode(const FunctionDecl *FD); + + //===--------------------------------------------------------------------===// + // Statement Emission + //===--------------------------------------------------------------------===// + + void EmitStmt(const Stmt *S); + void EmitCompoundStmt(const CompoundStmt &S); }; } // end namespace CodeGen } // end namespace clang diff --git a/clang/CodeGen/CodeGenModule.h b/clang/CodeGen/CodeGenModule.h index c5d3b9395ff0..7de3bc0e33b3 100644 --- a/clang/CodeGen/CodeGenModule.h +++ b/clang/CodeGen/CodeGenModule.h @@ -31,6 +31,7 @@ public: CodeGenModule(ASTContext &C, Module &M) : Context(C), TheModule(M) {} ASTContext &getContext() const { return Context; } + Module &getModule() const { return TheModule; } void EmitFunction(FunctionDecl *FD);