diff options
Diffstat (limited to 'lib/CodeGen')
| -rw-r--r-- | lib/CodeGen/CodeGen.cc | 143 | ||||
| -rw-r--r-- | lib/CodeGen/Designator.cc | 8 | ||||
| -rw-r--r-- | lib/CodeGen/FunctionGen.cc | 4 | ||||
| -rw-r--r-- | lib/CodeGen/StatementGen.cc | 5 | ||||
| -rw-r--r-- | lib/CodeGen/TypeSystem.cc | 5 |
5 files changed, 140 insertions, 25 deletions
diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index d3459ef..e7116dd 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -5,6 +5,7 @@ #include "cps/MemoryHandler.h" #include "cps/ExceptionHandler.h" #include "Builtins.h" +#include "cps/TypeAST.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Verifier.h" #include <cstdarg> @@ -152,7 +153,84 @@ const TypeInfo *CodeGen::resolveType(const std::string &TypeName) const { return Types->resolve(TypeName); } +bool CodeGen::isVisible(size_t Owner) const { + return !Units || (*Units)[CurrentUnit].Visible[Owner]; +} + +void CodeGen::selectUnit(size_t Unit) { + CurrentUnit = Unit; + DiagnosticSource = Units->size() > 1 ? (*Units)[Unit].Path : ""; + FuncGen->setCurrentUnit(Unit); +} + +void CodeGen::importSymbols() { + Symbols.clear(); + for (size_t I = 0; I < Exports.size(); ++I) { + if (I != CurrentUnit && isVisible(I)) + Symbols.insert(Exports[I].begin(), Exports[I].end()); + } +} + void CodeGen::compile(const std::vector<std::unique_ptr<StmtAST>> &Statements) { + compile(std::vector<SourceUnit>{{"", Statements, {true}}}); +} + +void CodeGen::compile(const std::vector<SourceUnit> &Sources) { + SaveAndRestore<const std::vector<SourceUnit> *> SourceContext(Units, &Sources); + SaveAndRestore<std::string> SourceName(DiagnosticSource); + if (Sources.empty()) { reportError("no main program"); return; } + Exports.resize(Sources.size()); + Types->setVisibility([this](const std::string &Name) { + auto It = TypeOwners.find(Name); + return It == TypeOwners.end() || isVisible(It->second); + }); + FuncGen->setVisibility([this](size_t Owner) { return isVisible(Owner); }); + + IdentifierMap<size_t> Definitions; + std::optional<size_t> CodeUnit; + auto Reserve = [&](const std::string &Name) { + auto [It, Added] = Definitions.emplace(Name, CurrentUnit); + if (!Added && It->second != CurrentUnit) + reportError("Duplicate definition %s in %s and %s", Name.c_str(), + Sources[It->second].Path.c_str(), Sources[CurrentUnit].Path.c_str()); + }; + for (size_t I = 0; I < Sources.size(); ++I) { + selectUnit(I); + for (const auto &S : Sources[I].Statements) { + SaveAndRestore<int> Location(DiagnosticLine, S->getLine()); + std::string TypeName; + if (auto *F = dynamic_cast<FunctionDefAST*>(S.get())) { + Reserve(F->getProto()->getName()); + } else if (auto *E = dynamic_cast<EnumTypeDeclAST*>(S.get())) { + TypeName = E->getName(); + for (const auto &V : E->getValueNames()) Reserve(V); + } else if (auto *R = dynamic_cast<RecordTypeDeclAST*>(S.get())) { + TypeName = R->getName(); + } else if (auto *P = dynamic_cast<PointerTypeDeclAST*>(S.get())) { + TypeName = P->getName(); + } else if (auto *C = dynamic_cast<CTypeDeclAST*>(S.get())) { + TypeName = C->getName(); + } else if (auto *C = dynamic_cast<ConstantStmtAST*>(S.get())) { + Reserve(C->getName()); + } else if (auto *G = dynamic_cast<GlobalBlockAST*>(S.get())) { + for (const auto &Body : G->getBody()) + if (auto *D = dynamic_cast<DeclareStmtAST*>(Body.get())) Reserve(D->getName()); + } else if (!dynamic_cast<IncludeStmtAST*>(S.get())) { + if (CodeUnit && *CodeUnit != I) { + reportError("Top-level code in both %s and %s", + Sources[*CodeUnit].Path.c_str(), Sources[I].Path.c_str()); + return; + } + CodeUnit = I; + } + if (!TypeName.empty()) { + Reserve(TypeName); + TypeOwners.emplace(TypeName, I); + } + } + } + if (HadError) return; + MainUnit = CodeUnit.value_or(0); FunctionType *FT = FunctionType::get(Type::getInt32Ty(*TheContext), false); Function *F = Function::Create(FT, Function::ExternalLinkage, "main", TheModule.get()); BasicBlock *BB = BasicBlock::Create(*TheContext, "entry", F); @@ -162,28 +240,61 @@ void CodeGen::compile(const std::vector<std::unique_ptr<StmtAST>> &Statements) { BasicBlock *UnhandledBB = BasicBlock::Create(*TheContext, "unhandled_exception", F); Exceptions->pushHandler(UnhandledBB); - // User types first: prototypes may name them in parameters/returns. - runTypePrePass(Statements); + // Reserve names across every unit before resolving any type payload. + for (size_t I = 0; I < Sources.size(); ++I) { + selectUnit(I); + runTypePrePass(Sources[I].Statements, 0); + } + // Complete included units first. The visited set makes include cycles harmless. + std::vector<bool> Visited(Sources.size(), false); + std::function<void(size_t)> DefineTypes = [&](size_t I) { + if (Visited[I]) return; + Visited[I] = true; + for (size_t J = 0; J < Sources.size(); ++J) + if (J != I && Sources[I].Visible[J]) DefineTypes(J); + selectUnit(I); + runTypePrePass(Sources[I].Statements, 1); + runTypePrePass(Sources[I].Statements, 2); + }; + for (size_t I = 0; I < Sources.size(); ++I) DefineTypes(I); - // Pre-register every top-level FUNCTION/PROCEDURE prototype so calls may - // precede definitions. - for (const auto &Stmt : Statements) { - SaveAndRestore<int> Location(DiagnosticLine, Stmt->getLine()); - if (auto *FuncDef = dynamic_cast<FunctionDefAST*>(Stmt.get())) { - FuncGen->emitPrototype(FuncDef->getProto()); + for (size_t I = 0; I < Sources.size(); ++I) { + selectUnit(I); + for (const auto &Stmt : Sources[I].Statements) { + SaveAndRestore<int> Location(DiagnosticLine, Stmt->getLine()); + if (auto *FuncDef = dynamic_cast<FunctionDefAST*>(Stmt.get())) + FuncGen->emitPrototype(FuncDef->getProto()); + if (auto *Class = dynamic_cast<ClassTypeDeclAST*>(Stmt.get())) { + for (const auto &Method : Class->getMethods()) { + SaveAndRestore<int> MethodLocation(DiagnosticLine, Method->getLine()); + FuncGen->emitPrototype(Method->getProto()); + } + } } } - for (const auto &[Class, Methods] : ClassMethods) { - for (const auto &[Name, Method] : Methods) { - SaveAndRestore<int> Location(DiagnosticLine, Method->getLine()); - FuncGen->emitPrototype(Method->getProto()); + // Constants are literal exports. Own constants are still installed only + // when their declaration is encountered during body emission. + for (size_t I = 0; I < Sources.size(); ++I) { + selectUnit(I); + Symbols.clear(); + for (const auto &Stmt : Sources[I].Statements) { + if (auto *Constant = dynamic_cast<ConstantStmtAST*>(Stmt.get())) { + SaveAndRestore<int> Location(DiagnosticLine, Stmt->getLine()); + emitConstantStmt(Constant); + } } + Exports[I] = Symbols; } - - for (const auto &Stmt : Statements) { - SaveAndRestore<int> Location(DiagnosticLine, Stmt->getLine()); - emitStmt(Stmt.get()); + for (size_t I = 0; I < Sources.size(); ++I) { + selectUnit(I); + importSymbols(); + for (const auto &Stmt : Sources[I].Statements) emitStmt(Stmt.get()); + } + selectUnit(MainUnit); + if (!CodeUnit && !HadError) { + SaveAndRestore<int> Location(DiagnosticLine, Sources.size() > 1 ? 1 : 0); + reportError("no main program"); } if (!Builder->GetInsertBlock()->hasTerminator()) { diff --git a/lib/CodeGen/Designator.cc b/lib/CodeGen/Designator.cc index 5f54a87..b31492b 100644 --- a/lib/CodeGen/Designator.cc +++ b/lib/CodeGen/Designator.cc @@ -11,10 +11,10 @@ using namespace cps; // (TYPE NodePtr = ^Node before TYPE Node — required for linked structures); // by-value record fields still require their record type to be complete, // which pass B's source order enforces. -void CodeGen::runTypePrePass(const std::vector<std::unique_ptr<StmtAST>> &Statements) { +void CodeGen::runTypePrePass(const std::vector<std::unique_ptr<StmtAST>> &Statements, int Phase) { std::string Err; - for (const auto &S : Statements) { + if (Phase == 0) for (const auto &S : Statements) { SaveAndRestore<int> Location(DiagnosticLine, S->getLine()); bool Ok = true; if (auto *C = dynamic_cast<CTypeDeclAST*>(S.get())) @@ -29,7 +29,7 @@ void CodeGen::runTypePrePass(const std::vector<std::unique_ptr<StmtAST>> &Statem if (!Ok) reportError("%s", Err.c_str()); } - for (const auto &S : Statements) { + if (Phase == 1) for (const auto &S : Statements) { SaveAndRestore<int> Location(DiagnosticLine, S->getLine()); bool Ok = true; if (auto *E = dynamic_cast<EnumTypeDeclAST*>(S.get())) { @@ -42,7 +42,7 @@ void CodeGen::runTypePrePass(const std::vector<std::unique_ptr<StmtAST>> &Statem if (!Ok) reportError("%s", Err.c_str()); } - for (const auto &S : Statements) { + if (Phase == 2) for (const auto &S : Statements) { auto *Class = dynamic_cast<ClassTypeDeclAST*>(S.get()); if (!Class) continue; auto &Methods = ClassMethods[Class->getName()]; diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index b66b55a..cc88a78 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -106,7 +106,8 @@ void FunctionGen::createArgumentAllocas(Function *F, const std::vector<ParamDecl const FuncSig *FunctionGen::getSignature(const std::string &Name) const { auto It = Signatures.find(Name); - return It == Signatures.end() ? nullptr : &It->second; + return It == Signatures.end() || (Visible && !Visible(It->second.OwnerUnit)) + ? nullptr : &It->second; } Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { @@ -134,6 +135,7 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { } FuncSig Sig; + Sig.OwnerUnit = CurrentUnit; Sig.Name = Proto->getLinkName(); Sig.ReturnTypeName = Proto->getReturnType(); diff --git a/lib/CodeGen/StatementGen.cc b/lib/CodeGen/StatementGen.cc index 529f7f0..5edb303 100644 --- a/lib/CodeGen/StatementGen.cc +++ b/lib/CodeGen/StatementGen.cc @@ -13,8 +13,9 @@ void CodeGen::emitStmt(StmtAST *Stmt) { SaveAndRestore<int> Location(DiagnosticLine, Stmt && Stmt->getLine() ? Stmt->getLine() : DiagnosticLine); if (!Stmt) return; - if (dynamic_cast<IncludeStmtAST*>(Stmt) || dynamic_cast<GlobalBlockAST*>(Stmt)) { - reportError("INCLUDE and GLOBAL are not supported yet"); + if (dynamic_cast<IncludeStmtAST*>(Stmt)) return; + if (dynamic_cast<GlobalBlockAST*>(Stmt)) { + reportError("GLOBAL is not supported yet"); return; } diff --git a/lib/CodeGen/TypeSystem.cc b/lib/CodeGen/TypeSystem.cc index 27814c2..34b8d2d 100644 --- a/lib/CodeGen/TypeSystem.cc +++ b/lib/CodeGen/TypeSystem.cc @@ -111,7 +111,7 @@ bool TypeSystem::sameType(const std::string &Left, const std::string &Right) con const TypeInfo *TypeSystem::resolve(const std::string &TypeName) const { auto It = Types.find(TypeName); - if (It == Types.end()) { + if (It == Types.end() || (Visible && !Visible(TypeName))) { return nullptr; } return &It->second; @@ -271,7 +271,8 @@ bool TypeSystem::definePointer(const std::string &Name, const EnumConstant *TypeSystem::lookupEnumConstant(const std::string &Name) const { auto It = EnumConstants.find(Name); - return It == EnumConstants.end() ? nullptr : &It->second; + return It == EnumConstants.end() || (Visible && !Visible(It->second.TypeName)) + ? nullptr : &It->second; } Type *TypeSystem::getLLVMType(const std::string &TypeName) const { |
