Fix Linux 64-bit build again.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
21 #include "../Strings.h"
22 #include "../NativeTable.h"
23 #include "../TypeManager.h"
24 #include "../Modules.h"
26 #include "SemanticAnalysis.h"
27 #include "../Heap-inl.h"
33 typedef BytecodeEmitter::Label Label;
38 AutoEnterScope(Scope **prevp, Scope *scope)
47 assert(prev_ == scope->enclosing());
60 SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit)
72 SemanticAnalysis::SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun)
73 : zone_(parent->zone_),
78 scope_(parent->scope_),
80 module_(parent->module_),
88 SemanticAnalysis::analyze()
90 scope_ = unit_->globalScope();
92 module_ = Module::New(zone_);
94 for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
95 Statement *stmt = unit_->tree()->statements()->at(i);
99 Local<Struct> globals(zone_, unit_->globalScope()->build(zone_));
102 module_->setGlobals(globals);
104 Local<Code> code(zone_, __ finish());
107 code->setModule(module_);
108 module_->setCode(code);
111 // Disassemble(code, stdout);
114 unit_->module_ = module_;
118 SemanticAnalysis::visitFunctionStatement(FunctionStatement *stmt)
120 FunctionSymbol *sym = stmt->sym();
121 Local<FunctionType> type(zone_, sym->type());
123 assert(scope_->kind() == Scope::GLOBAL);
125 if (stmt->token() == TOK_NATIVE) {
126 // Define the native now.
128 if (!zone_->nativeTable()->add(stmt->name(), &index))
131 Local<Native> native(zone_, Native::New(zone_, type, index));
140 // If the function is a forward or native, we don't have to do anything.
141 if (stmt->token() == TOK_NATIVE || stmt->token() == TOK_FORWARD)
144 SemanticAnalysis child(this, type);
145 Local<Function> funobj(zone_, child.compile(stmt));
155 SemanticAnalysis::compile(FunctionStatement *stmt)
157 __ setFirstLine(stmt->pos().line);
159 AutoEnterScope argScope(&scope_, stmt->argScope());
160 AutoEnterScope varScope(&scope_, stmt->varScope());
162 stmt->body()->accept(this);
164 Local<Code> code(zone_, __ finish());
167 code->setModule(module_);
169 Local<Function> fun(zone_, Function::New(zone_, code, fun_, stmt->name(), stmt->pos()));
174 // Disassemble(code, stdout);
181 SemanticAnalysis::visitBlockStatement(BlockStatement *node)
183 AutoEnterScope scope(&scope_, node->scope());
185 for (size_t i = 0; i < node->statements()->length(); i++) {
186 Statement *stmt = node->statements()->at(i);
188 assert(__ stackDepth() == 0);
193 SemanticAnalysis::visitExpressionStatement(ExpressionStatement *node)
195 // While building HIR, we enter new pool and root scopes, since HIR
196 // creation is purely temporary.
197 PoolScope poolScope(zone_->pool());
198 RootScope rootScope(zone_);
200 HIR *hir = eval(node->expression());
209 SemanticAnalysis::visitForStatement(ForStatement *node)
211 AutoEnterScope scope(&scope_, node->scope());
212 PoolScope poolScope(zone_->pool());
213 RootScope rootScope(zone_);
216 LoopScope loop(scope_, &join, &update, &loop_);
218 if (node->initialization())
219 node->initialization()->accept(this);
222 if (node->condition())
226 node->body()->accept(this);
230 node->update()->accept(this);
232 if (node->condition()) {
234 visitForTest(node->condition(), &header, &join, &join);
241 assert(__ stackDepth() == 0);
245 SemanticAnalysis::visitVariableDeclaration(VariableDeclaration *node)
247 PoolScope poolScope(zone_->pool());
248 RootScope rootScope(zone_);
251 if (node->initialization()) {
252 if ((hir = eval(node->initialization())) == NULL)
254 if ((hir = coerce(hir, node->sym()->type(), Coerce_Assign)) == NULL)
262 if (!__ allocate(node->sym()))
270 SemanticAnalysis::visitReturnStatement(ReturnStatement *node)
272 if (!node->expression()) {
273 if (!fun_->returnType()->isVoid()) {
274 cc_.reportError(node->pos(), Message_UsedVoidReturn);
282 PoolScope poolScope(zone_->pool());
283 RootScope rootScope(zone_);
285 HIR *hir = eval(node->expression());
289 Local<Type> returnType(zone_, fun_->returnType());
290 if ((hir = coerce(hir, returnType, Coerce_Return)) == NULL)
298 SemanticAnalysis::visitBinaryExpression(BinaryExpression *node)
300 HIR *left = eval(node->left());
301 HIR *right = eval(node->right());
305 hir_ = binary(node->token(), left, right);
309 SemanticAnalysis::checkArgumentCount(Handle<FunctionType> type, unsigned actual)
311 if (type->parameters()->length() == actual)
314 if (actual > type->parameters()->length()) {
315 if (!type->isNative() || !type->isNativeVariadic())
320 if (!type->defaults())
323 // Missing arguments. See if there's a default argument for each missing
325 for (unsigned i = actual; i < type->parameters()->length(); i++) {
326 if (!type->defaults()->at(i))
334 SemanticAnalysis::visitNameProxy(NameProxy *proxy)
336 Symbol *sym = proxy->sym();
337 Handle<Type> type = sym->type();
339 VariableSymbol *var = sym->asVariable();
341 Scope *in = sym->scope();
342 switch (in->kind()) {
344 hir_ = new (pool_) HGlobal(sym);
348 assert(in->kind() == Scope::BLOCK);
349 assert(var->storage() == VariableSymbol::Local);
350 hir_ = new (pool_) HLocal(var);
356 SemanticAnalysis::visitIntegerLiteral(IntegerLiteral *lit)
358 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
359 hir_ = new (pool_) HInteger(type, lit->value());
363 SemanticAnalysis::visitFloatLiteral(FloatLiteral *node)
365 Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
366 hir_ = new (pool_) HFloat(type, (float)node->value());
370 SemanticAnalysis::visitCallExpression(CallExpression *node)
372 HIR *callee = eval(node->callee());
376 if (!callee->type()->isFunction()) {
377 cc_.reportError(node->pos(), Message_CalleeNotFunction);
381 Local<FunctionType> fun(zone_, FunctionType::cast(callee->type()));
383 if (!checkArgumentCount(fun, node->arguments()->length())) {
384 cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
388 if (fun->isForward()) {
389 cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
393 HIRList *args = new (pool_) HIRList;
394 for (unsigned i = 0; i < node->arguments()->length(); i++) {
395 Expression *expression = node->arguments()->at(i);
396 HIR *arg = eval(expression);
400 Handle<Type> given = arg->type();
402 bool mustComputeReference;
403 if (i >= fun->parameters()->length()) {
404 // Special handling for natives. Arguments past the formal must
405 // always have references computed. However, arrays are already
406 // references for natives.
407 assert(fun->isNative() && fun->isNativeVariadic());
408 mustComputeReference = !given->isReference() &&
411 Local<Type> actual(zone_, fun->parameterAt(i));
413 if (expression->isIndexExpression()) {
414 // Temporarily disabled.
415 assert(!actual->isArray());
416 if (actual->isReference()) {
417 cc_.reportError(expression->pos(), Message_CannotComputeIndexRef);
422 // When we start overloading, coercion will have to happen later.
423 arg = coerce(arg, actual, Coerce_Arg);
427 mustComputeReference = actual->isReference() && !given->isReference();
430 // :TODO: handle mustComputeReference
431 assert(!mustComputeReference);
433 if (!args->append(arg))
437 hir_ = new (pool_) HCall(fun->returnType(), callee, args);
441 SemanticAnalysis::visitAssignment(Assignment *node)
443 HLValue *lval = lvalue(node->lvalue());
447 HIR *hir = eval(node->expression());
448 if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
451 hir_ = new (pool_) HStore(lval, hir);
455 SemanticAnalysis::coercionType(TokenKind token, HIR *left, HIR *right)
457 Handle<Type> l = left->type();
458 Handle<Type> r = right->type();
460 if (token >= TOK_PLUS && token <= TOK_SLASH) {
461 // We allow: [float|int32] + [float|int32]
462 if (l->isFloat() && r->isInt32())
464 if (l->isInt32() && r->isFloat())
466 if (l->isInt32() && r->isInt32())
476 SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
478 Handle<Type> from = hir->type();
480 if (from->isInt32() && to->isFloat())
481 return new (pool_) HConvert(to, OP_CVT_I2F, hir);
489 SemanticAnalysis::set(VariableSymbol *sym)
491 if (sym->scope()->kind() == Scope::GLOBAL) {
494 assert(sym->storage() == VariableSymbol::Local);
500 SemanticAnalysis::binary(TokenKind token, HIR *left, HIR *right)
502 Local<Type> coercion(zone_, coercionType(token, left, right));
505 if ((left = coerce(left, coercion, Coerce_Arg)) == NULL)
507 if ((right = coerce(right, coercion, Coerce_Arg)) == NULL)
522 return new (pool_) HBinary(left->type(), token, left, right);
533 Local<Type> result(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
534 return new (pool_) HBinary(result, token, left, right);
543 static inline TokenKind
544 ToCompare(Expression *expr)
546 if (expr->isBinaryExpression()) {
547 BinaryExpression *b = expr->toBinaryExpression();
548 if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
550 } else if (expr->isUnaryExpression()) {
551 UnaryExpression *u = expr->toUnaryExpression();
552 if (u->token() == TOK_NOT)
559 SemanticAnalysis::visitForTest(Expression *expr,
564 TokenKind token = ToCompare(expr);
565 if (token == TOK_EOF || token == TOK_NOT) {
567 //visitForUnaryTest(expr, trueBranch, falseBranch, fallthrough);
571 BinaryExpression *bin = expr->toBinaryExpression();
572 if (token == TOK_AND || token == TOK_OR) {
574 //visitForLogicTest(expr, trueBranch, falseBranch, fallthrough);
578 HIR *left = eval(bin->left());
579 HIR *right = eval(bin->right());
583 HIR *hir = binary(bin->token(), left, right);
588 if (fallthrough == trueBranch)
589 __ j(OP_JF, falseBranch);
591 __ j(OP_JT, trueBranch);
595 SemanticAnalysis::lvalue(Expression *expr)
597 HIR *hir = eval(expr);
601 if (!hir->isLValue()) {
602 cc_.reportError(expr->pos(), Message_ExpectedLValue);
606 return static_cast<HLValue *>(hir);
610 SemanticAnalysis::eval(Expression *expr)
622 SemanticAnalysis::discharge(HIR *hir)
624 EmitHIR(zone_, emitter_, hir);