Permalink
Browse files

Updated assembler to output linker entries for directly used labels.

Fixed returning references for expression types in compiler.
  • Loading branch information...
James Rhodes
James Rhodes committed May 12, 2012
1 parent 2c543ee commit 4764891daf10cdfc1d8673cb204144e4a10616f0
Showing with 84 additions and 70 deletions.
  1. +10 −1 dtasm/aout.c
  2. +4 −4 libdcpu-ci-lang-c/nodes/NAddressOfOperator.cpp
  3. +1 −1 libdcpu-ci-lang-c/nodes/NAddressOfOperator.h
  4. +1 −1 libdcpu-ci-lang-c/nodes/NAssignment.cpp
  5. +1 −1 libdcpu-ci-lang-c/nodes/NAssignment.h
  6. +9 −6 libdcpu-ci-lang-c/nodes/NBinaryOperator.cpp
  7. +1 −1 libdcpu-ci-lang-c/nodes/NBinaryOperator.h
  8. +2 −2 libdcpu-ci-lang-c/nodes/NBlock.cpp
  9. +1 −1 libdcpu-ci-lang-c/nodes/NBlock.h
  10. +2 −2 libdcpu-ci-lang-c/nodes/NCharacter.cpp
  11. +1 −1 libdcpu-ci-lang-c/nodes/NCharacter.h
  12. +5 −4 libdcpu-ci-lang-c/nodes/NDereferenceOperator.cpp
  13. +1 −1 libdcpu-ci-lang-c/nodes/NDereferenceOperator.h
  14. +1 −1 libdcpu-ci-lang-c/nodes/NExpression.h
  15. +2 −2 libdcpu-ci-lang-c/nodes/NIdentifier.cpp
  16. +1 −1 libdcpu-ci-lang-c/nodes/NIdentifier.h
  17. +2 −2 libdcpu-ci-lang-c/nodes/NInteger.cpp
  18. +1 −1 libdcpu-ci-lang-c/nodes/NInteger.h
  19. +12 −11 libdcpu-ci-lang-c/nodes/NMethodCall.cpp
  20. +1 −1 libdcpu-ci-lang-c/nodes/NMethodCall.h
  21. +1 −1 libdcpu-ci-lang-c/nodes/NPostIncDec.cpp
  22. +1 −1 libdcpu-ci-lang-c/nodes/NPostIncDec.h
  23. +1 −1 libdcpu-ci-lang-c/nodes/NPreIncDec.cpp
  24. +1 −1 libdcpu-ci-lang-c/nodes/NPreIncDec.h
  25. +3 −3 libdcpu-ci-lang-c/nodes/NString.cpp
  26. +1 −1 libdcpu-ci-lang-c/nodes/NString.h
  27. +4 −4 libdcpu-ci-lang-c/nodes/NStructureResolutionOperator.cpp
  28. +1 −1 libdcpu-ci-lang-c/nodes/NStructureResolutionOperator.h
  29. +2 −2 libdcpu-ci-lang-c/nodes/NType.cpp
  30. +4 −3 libdcpu-ci-lang-c/nodes/NType.h
  31. +3 −5 libdcpu-ci-lang-c/nodes/NUnaryOperator.cpp
  32. +1 −1 libdcpu-ci-lang-c/nodes/NUnaryOperator.h
  33. +2 −1 libdcpu-ci-lang/nodes/IType.h
View
@@ -362,7 +362,7 @@ void aout_write(FILE* out, bool relocatable, bool intermediate)
break;
}
}
- /*else if (current_inner->type == AOUT_TYPE_NORMAL && (bstring)current_outer->expr->data != NULL)
+ /*else if (current_inner->type == AOUT_TYPE_NORMAL)
{
// We're adjusting a label reference in the code
// to it's actual value.
@@ -393,6 +393,15 @@ void aout_write(FILE* out, bool relocatable, bool intermediate)
expr_delete(current_outer->expr);
current_outer->expr = NULL;
did_find = true;
+
+ // We also need to add this entry to the adjustment
+ // table for the linker since it also needs to adjust
+ // internal label jumps in files when it concatenates
+ // all of the object code together.
+ linker_temp = lprov_create(NULL, out_index);
+ linker_temp->next = linker_adjustment;
+ linker_adjustment = linker_temp;
+ fprintf(stderr, "LINK ADJUST 0x%04X\n", out_index);
}
}
}
@@ -27,11 +27,11 @@ AsmBlock* NAddressOfOperator::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of an address-of operator.");
}
-IType& NAddressOfOperator::getExpressionType(AsmGenerator& context)
+IType* NAddressOfOperator::getExpressionType(AsmGenerator& context)
{
// The address of operator has the type of it's expression as a pointer.
- IType& c = this->expr.getExpressionType(context);
- NType t = NType((NType&)c);
- t.pointerCount += 1;
+ IType* i = this->expr.getExpressionType(context);
+ NType* t = new NType(*((NType*)i));
+ t->pointerCount += 1;
return t;
}
@@ -23,7 +23,7 @@ class NAddressOfOperator : public NExpression
NAddressOfOperator(NExpression& expr) : expr(expr), NExpression("addressof") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -118,7 +118,7 @@ AsmBlock* NAssignment::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of an assignment.");
}
-IType& NAssignment::getExpressionType(AsmGenerator& context)
+IType* NAssignment::getExpressionType(AsmGenerator& context)
{
// An assignment has the type of it's LHS.
return this->lhs.getExpressionType(context);
@@ -26,7 +26,7 @@ class NAssignment : public NExpression
lhs(lhs), op(op), rhs(rhs), NExpression("assignment") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -188,14 +188,17 @@ AsmBlock* NBinaryOperator::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of an binary operator.");
}
-IType& NBinaryOperator::getExpressionType(AsmGenerator& context)
+IType* NBinaryOperator::getExpressionType(AsmGenerator& context)
{
// A binary operator has the type of each of it's expressions.
- NType lType = (NType&)this->lhs.getExpressionType(context);
- NType rType = (NType&)this->rhs.getExpressionType(context);
-
- if (lType != rType)
- throw new CompilerException("Unable to implicitly cast from " + lType.name + " to " + rType.name + ".");
+ IType* lType = this->lhs.getExpressionType(context);
+ IType* rType = this->rhs.getExpressionType(context);
+
+ // FIXME: We need a proper type system allowing for implicit
+ // casts between reasonable values (currently this won't
+ // even permit you to cast between int and int16_t).
+ //if (lType != rType)
+ // throw new CompilerException("Unable to implicitly cast from " + lType.name + " to " + rType.name + ".");
return lType;
}
@@ -26,7 +26,7 @@ class NBinaryOperator : public NExpression
lhs(lhs), op(op), rhs(rhs), NExpression("binaryop") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -38,8 +38,8 @@ AsmBlock* NBlock::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of a block node.");
}
-IType& NBlock::getExpressionType(AsmGenerator& context)
+IType* NBlock::getExpressionType(AsmGenerator& context)
{
// A block of statements has no type.
- return NType::VoidType;
+ return new NType(NType::VoidType);
}
@@ -24,7 +24,7 @@ class NBlock : public NExpression
NBlock() : NExpression("block") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -34,8 +34,8 @@ AsmBlock* NCharacter::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to a character node.");
}
-IType& NCharacter::getExpressionType(AsmGenerator& context)
+IType* NCharacter::getExpressionType(AsmGenerator& context)
{
// A character has the type char.
- return NInteger::CharType;
+ return new NType(NInteger::CharType);
}
@@ -23,7 +23,7 @@ class NCharacter : public NExpression
NCharacter(std::string value) : value(value), NExpression("character") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -42,13 +42,14 @@ AsmBlock* NDereferenceOperator::reference(AsmGenerator& context)
return this->expr.compile(context);
}
-IType& NDereferenceOperator::getExpressionType(AsmGenerator& context)
+IType* NDereferenceOperator::getExpressionType(AsmGenerator& context)
{
// An dereference operator has the "unpointered" type of it's expression.
- NType t = NType((NType&)this->expr.getExpressionType(context));
+ IType* i = this->expr.getExpressionType(context);
+ NType* t = new NType(*((NType*)i));
- if (t.pointerCount > 0)
- t.pointerCount -= 1;
+ if (t->pointerCount > 0)
+ t->pointerCount -= 1;
else
throw new CompilerException("Attempting to dereference non-pointer type during type resolution.");
@@ -23,7 +23,7 @@ class NDereferenceOperator : public NExpression
NDereferenceOperator(NExpression& expr) : expr(expr), NExpression("dereference") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -24,7 +24,7 @@ class NExpression : public Node
protected:
NExpression(std::string type) : Node("expression-" + type) { }
public:
- virtual IType& getExpressionType(AsmGenerator& context) = 0;
+ virtual IType* getExpressionType(AsmGenerator& context) = 0;
};
#endif
@@ -57,7 +57,7 @@ AsmBlock* NIdentifier::reference(AsmGenerator& context)
return block;
}
-IType& NIdentifier::getExpressionType(AsmGenerator& context)
+IType* NIdentifier::getExpressionType(AsmGenerator& context)
{
// Search the current context for the variable with
// this name and return it's type.
@@ -66,5 +66,5 @@ IType& NIdentifier::getExpressionType(AsmGenerator& context)
if (type == NULL)
throw new CompilerException("Unable to resolve variable '" + this->name + "' when determining type information (does the variable exist?).");
else
- return *type;
+ return type;
}
@@ -24,7 +24,7 @@ class NIdentifier : public NExpression
NIdentifier(const std::string& name, std::string type) : name(name), NExpression("identifier-" + type) { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -60,8 +60,8 @@ AsmBlock* NInteger::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of a numeric literal.");
}
-IType& NInteger::getExpressionType(AsmGenerator& context)
+IType* NInteger::getExpressionType(AsmGenerator& context)
{
// All literals are currently unsigned 16-bit integers.
- return NInteger::UInt16Type;
+ return new NType(NInteger::UInt16Type);
}
@@ -26,7 +26,7 @@ class NInteger : public NExpression
NInteger(long long value) : value(value), NExpression("integer") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
// Specific types for integers.
static NType& Int8Type;
@@ -46,6 +46,9 @@ AsmBlock* NMethodCall::compile(AsmGenerator& context)
isDirect = false;
}
+ // FIXME: Again, without implicit type casting this breaks quite a few
+ // things, so it's disabled for now.
+ /*
// check if the called function matches the signature of this method call
// typedef std::vector<NVariableDeclaration*> VariableList; funcsig->arguments
// typedef std::vector<NExpression*> ExpressionList for this->arguments
@@ -59,16 +62,17 @@ AsmBlock* NMethodCall::compile(AsmGenerator& context)
// now check types of all the arguments
for (unsigned int i = 0; i < funcsig->arguments.size(); i++)
{
- NType callerType = (NType&) this->arguments[i]->getExpressionType(context);
- NType calleeType = (NType&) funcsig->arguments[i]->type;
- if (callerType.name != calleeType.name)
+ NType* callerType = (NType*) this->arguments[i]->getExpressionType(context);
+ NType& calleeType = funcsig->arguments[i]->type;
+ if (callerType->name != calleeType.name)
{
throw new CompilerException("There is no function with the name "
+ this->id.name + " and signature " + this->calculateSignature(context) + "\n"
+ "Candidates are:\t" + this->id.name + " with signature " + funcsig->getSignature());
}
}
-
+ */
+
// Get the stack table of this method.
StackFrame* frame = context.generateStackFrameIncomplete(funcsig);
@@ -176,15 +180,15 @@ AsmBlock* NMethodCall::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of a method call.");
}
-IType& NMethodCall::getExpressionType(AsmGenerator& context)
+IType* NMethodCall::getExpressionType(AsmGenerator& context)
{
// An method call has the type of the method's return type.
NFunctionDeclaration* funcdecl = (NFunctionDeclaration*)context.getFunction(this->id.name);
if (funcdecl == NULL)
throw new CompilerException("Called function was not found '" + this->id.name + "'.");
- return (NType&)funcdecl->type;
+ return new NType(funcdecl->type);
}
@@ -193,18 +197,15 @@ IType& NMethodCall::getExpressionType(AsmGenerator& context)
/* NFunctionSignature::calculateSignature() */
std::string NMethodCall::calculateSignature(AsmGenerator& context)
{
-
-
-
std::string sig = "(";
for (ExpressionList::const_iterator i = this->arguments.begin(); i != this->arguments.end(); i++)
{
if (i != this->arguments.begin())
{
sig = sig + ",";
}
- NType type = (NType&)(*i)->getExpressionType(context);
- sig = sig + type.name;
+ NType* type = (NType*)((*i)->getExpressionType(context));
+ sig = sig + type->name;
}
sig = sig + ")";
return sig;
@@ -30,7 +30,7 @@ class NMethodCall : public NExpression
NMethodCall(const NIdentifier& id) : id(id), NExpression("methodcall") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
virtual std::string calculateSignature(AsmGenerator& context);
};
@@ -58,7 +58,7 @@ AsmBlock* NPostIncDec::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of an Post-Increment.");
}
-IType& NPostIncDec::getExpressionType(AsmGenerator& context)
+IType* NPostIncDec::getExpressionType(AsmGenerator& context)
{
// An assignment has the type of it's LHS.
return this->expr.getExpressionType(context);
@@ -25,7 +25,7 @@ class NPostIncDec : public NExpression
expr(expr), op(op), NExpression("postincdec") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -61,7 +61,7 @@ AsmBlock* NPreIncDec::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of an Pre-Increment.");
}
-IType& NPreIncDec::getExpressionType(AsmGenerator& context)
+IType* NPreIncDec::getExpressionType(AsmGenerator& context)
{
// An assignment has the type of it's LHS.
return this->expr.getExpressionType(context);
@@ -25,7 +25,7 @@ class NPreIncDec : public NExpression
expr(expr), op(op), NExpression("preincdec") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -43,10 +43,10 @@ AsmBlock* NString::reference(AsmGenerator& context)
throw new CompilerException("Unable to get reference to the result of a string literal.");
}
-IType& NString::getExpressionType(AsmGenerator& context)
+IType* NString::getExpressionType(AsmGenerator& context)
{
// A string has the type char*.
- NType t = NType(NInteger::CharType);
- t.pointerCount += 1;
+ NType* t = new NType(NInteger::CharType);
+ t->pointerCount += 1;
return t;
}
@@ -23,7 +23,7 @@ class NString : public NExpression
NString(std::string value) : value(value), NExpression("string") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
@@ -52,14 +52,14 @@ AsmBlock* NStructureResolutionOperator::reference(AsmGenerator& context)
throw new CompilerException("Unable to use AST node " + this->lhs.cType + " as part of the structure resolution operator; it is not a suitable left-value.");
// Ensure the LHS expression is actually a structure type.
- NType& t = (NType&)this->lhs.getExpressionType(context);
+ NType* t = (NType*)this->lhs.getExpressionType(context);
- if (!t.isStruct)
+ if (!t->isStruct)
throw new CompilerException("Unable to use AST node " + this->lhs.cType + " as part of the structure resolution operator; the resulting type is not a structure.");
// We need to work out at what offset is the RHS identifier
// in the structure.
- uint16_t pos = t.getStructFieldPosition(context, this->rhs.name);
+ uint16_t pos = t->getStructFieldPosition(context, this->rhs.name);
// Evaluate the LHS, placing a reference to it's position on the stack.
AsmBlock* lhs = this->lhs.reference(context);
@@ -73,7 +73,7 @@ AsmBlock* NStructureResolutionOperator::reference(AsmGenerator& context)
return block;
}
-IType& NStructureResolutionOperator::getExpressionType(AsmGenerator& context)
+IType* NStructureResolutionOperator::getExpressionType(AsmGenerator& context)
{
// A structure resolution operator has the type of it's field.
throw new CompilerException("Unable to resolve type for structure resolution operator.");
@@ -28,7 +28,7 @@ class NStructureResolutionOperator : public NExpression
lhs(lhs), rhs(rhs), isDereference(isDereference), NExpression("field") { }
virtual AsmBlock* compile(AsmGenerator& context);
virtual AsmBlock* reference(AsmGenerator& context);
- virtual IType& getExpressionType(AsmGenerator& context);
+ virtual IType* getExpressionType(AsmGenerator& context);
};
#endif
Oops, something went wrong.

0 comments on commit 4764891

Please sign in to comment.