Get string literals working again.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
20 #include "../Interpreter.h"
22 #include "CompileContext.h"
24 #include "NameBinding.h"
25 #include "SemanticAnalysis.h"
26 #include "../Spaces-inl.h"
27 #include "../Heap-inl.h"
31 CompileContext::CompileContext(Zone *zone, const char *file)
38 CompileContext::~CompileContext()
41 zone_->endCompilationErrors();
45 CompileContext::addTranslationUnit(TranslationUnit *unit)
47 if (!units_.append(unit))
50 if (!unparsed_.append(unit))
57 CompileContext::loadSource(const char *file, size_t *lengthp)
62 FILE *fp = fopen(file, "rb");
64 reportError(SourcePosition(), Message_CantOpenFile);
68 if (fseek(fp, 0, SEEK_END)) {
69 reportError(SourcePosition(), Message_ErrorSeekingFile);
73 long size = ftell(fp);
75 reportError(SourcePosition(), Message_ErrorSeekingFile);
80 length = size_t(size);
81 bytes = new char[length + 1];
82 if (fread(bytes, 1, length, fp) != length) {
83 reportError(SourcePosition(), Message_ErrorReadingFile);
95 CompileContext::parse(TranslationUnit *unit)
100 char *source = loadSource(unit->path_, &length);
104 Parser parser(ZONE(), *this, source, length);
105 if ((unit->tree_ = parser.parse()) == NULL)
112 CompileContext::evaluateImports(TranslationUnit *unit)
114 if (units_.length() > 1)
118 // For now we hardcode this...
119 TranslationUnit *u2 = new (ZONE()->pool()) TranslationUnit();
120 u2->path_ = "C:\\Users\\dvander\\jitcraft\\jitcraft-2\\src\\Debug\\wizard.sp";
122 ImportStatement *stmt = unit->tree()->imports()->at(0)->toImportStatement();
123 stmt->setImportSource(u2);
125 if (!u2->dependents_.append(unit))
128 if (!addTranslationUnit(u2))
136 CompileContext::propagateFailure()
138 Vector<TranslationUnit *> worklist;
139 for (size_t i = 0; i < units_.length(); i++) {
140 if (units_[i]->failed() && !worklist.append(units_[i]))
144 while (!worklist.empty()) {
145 TranslationUnit *unit = worklist.popCopy();
146 for (size_t i = 0; i < unit->dependents().length(); i++) {
147 TranslationUnit *other = unit->dependents().at(i);
151 other->failed_ = true;
152 if (!worklist.append(other))
161 CompileContext::compile()
163 // Phase 1. Parse all modules we're importing. This process may load new
164 // modules off disk, and add them to the parse queue.
165 while (!unparsed_.empty()) {
166 TranslationUnit *unit = unparsed_.popCopy();
169 unit->failed_ = true;
171 if (!evaluateImports(unit))
172 unit->failed_ = true;
175 // Phase 2. Create scopes and symbol tables for every lexical region with
176 // a declaration. This phase excludes declarations created via imports.
177 for (size_t i = 0; i < units_.length(); i++) {
178 TranslationUnit *unit = units_[i];
182 if (!PopulateNamesAndTypes(zone_, *this, unit))
183 unit->failed_ = true;
186 // Phase 3. Complete imports and bind all names.
187 for (size_t i = 0; i < units_.length(); i++) {
188 TranslationUnit *unit = units_[i];
192 if (!BindNamesAndTypes(zone_, *this, unit))
193 unit->failed_ = true;
196 // Phase 4. Assign types to every public name in the module.
197 for (size_t i = 0; i < units_.length(); i++) {
198 TranslationUnit *unit = units_[i];
204 // Intermediate phase. Decide which modules we should not perform
205 // semantic analysis on.
206 if (!propagateFailure())
209 // Phase 4. Perform full semantic analysis on each module.
210 for (size_t i = 0; i < units_.length(); i++) {
211 TranslationUnit *unit = units_[i];
218 // Intermediate phase. Propagate failure from semantic analysis.
219 if (!propagateFailure())
222 // Phase 5. Now we can finally perform bytecode compilation.
223 for (size_t i = 0; i < units_.length(); i++) {
224 TranslationUnit *unit = units_[i];
228 SemanticAnalysis sema(zone_, *this, unit);
232 // Phase 6. Run modules.
233 for (size_t i = 0; i < units_.length(); i++) {
234 TranslationUnit *unit = units_[i];
239 if (!zone_->stack().pushModuleFrame(unit->module_, args))
246 if (!Interpret(zone_, NULL, &slot))
254 CompileContext::reportErrorVa(const SourcePosition &pos, Message msg, va_list ap)
257 zone_->beginCompilationErrors(file_);
259 zone_->reportCompilationError(msg, pos.line, pos.col, ap);
265 CompileContext::reportError(const SourcePosition &pos, Message msg, ...)
269 reportErrorVa(pos, msg, ap);