MCPcopy Create free account
hub / github.com/WheretIB/nullc / BuildModule

Method BuildModule

NULLC/Compiler.cpp:1049–1102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1047}
1048
1049char* Compiler::BuildModule(const char* file, const char* altFile)
1050{
1051 unsigned int fileSize = 0;
1052 int needDelete = false;
1053 bool failedImportPath = false;
1054
1055 char *fileContent = (char*)NULLC::fileLoad(file, &fileSize, &needDelete);
1056 if(!fileContent && BinaryCache::GetImportPath())
1057 {
1058 failedImportPath = true;
1059 fileContent = (char*)NULLC::fileLoad(altFile, &fileSize, &needDelete);
1060 }
1061
1062 if(fileContent)
1063 {
1064 unsigned lexPos = lexer.GetStreamSize();
1065 if(!Compile(fileContent, true))
1066 {
1067 unsigned int currLen = (unsigned int)strlen(CodeInfo::lastError.errLocal);
1068 SafeSprintf(CodeInfo::lastError.errLocal + currLen, NULLC_ERROR_BUFFER_SIZE - currLen, " [in module %s]", file);
1069
1070 if(needDelete)
1071 NULLC::dealloc(fileContent);
1072 return NULL;
1073 }
1074 char *bytecode = NULL;
1075#ifdef VERBOSE_DEBUG_OUTPUT
1076 printf("Bytecode for module %s. ", file);
1077#endif
1078 GetBytecode(&bytecode);
1079
1080 if(needDelete)
1081 {
1082 const char *newStart = (const char*)bytecode + ((ByteCode*)bytecode)->offsetToSource;
1083 // We have to fix lexeme positions to the code that is saved in bytecode
1084 for(Lexeme *c = lexer.GetStreamStart() + lexPos, *e = lexer.GetStreamStart() + lexer.GetStreamSize(); c != e; c++)
1085 {
1086 // Exit fix up if lexemes exited scope of the current file
1087 if(c->pos < fileContent || c->pos > (fileContent + fileSize))
1088 break;
1089 c->pos = (c->pos - fileContent) + newStart;
1090 }
1091 NULLC::dealloc(fileContent);
1092 }
1093
1094 BinaryCache::PutBytecode(failedImportPath ? altFile : file, bytecode);
1095
1096 return bytecode;
1097 }else{
1098 CodeInfo::lastError.Init("", NULL);
1099 SafeSprintf(CodeInfo::lastError.errLocal, NULLC_ERROR_BUFFER_SIZE, "ERROR: module %s not found", altFile);
1100 }
1101 return NULL;
1102}
1103
1104#ifdef NULLC_LLVM_SUPPORT
1105const char *llvmBinary = NULL;

Callers

nothing calls this directly

Calls 4

GetStreamSizeMethod · 0.80
GetStreamStartMethod · 0.80
InitMethod · 0.80
SafeSprintfFunction · 0.70

Tested by

no test coverage detected