mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-10-14 13:24:57 +00:00
llvm-mc: Diagnose misuse (mix) of defined symbols and labels.
- For example, we diagnose errors on: -- a: a = 10 -- - For now we reject code like: -- .long a a = 10 -- which "as" accepts (on Darwin). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74476 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
9146d0e89c
commit
dce0f3c556
@ -31,6 +31,8 @@ namespace llvm {
|
|||||||
StringMap<MCSymbol*> Symbols;
|
StringMap<MCSymbol*> Symbols;
|
||||||
|
|
||||||
/// SymbolValues - Bindings of symbols to values.
|
/// SymbolValues - Bindings of symbols to values.
|
||||||
|
//
|
||||||
|
// FIXME: Is there a good reason to not just put this in the MCSymbol?
|
||||||
DenseMap<MCSymbol*, MCValue> SymbolValues;
|
DenseMap<MCSymbol*, MCValue> SymbolValues;
|
||||||
|
|
||||||
/// Allocator - Allocator object used for creating machine code objects.
|
/// Allocator - Allocator object used for creating machine code objects.
|
||||||
|
@ -17,14 +17,18 @@ namespace llvm {
|
|||||||
MCSection *Section;
|
MCSection *Section;
|
||||||
std::string Name;
|
std::string Name;
|
||||||
unsigned IsTemporary : 1;
|
unsigned IsTemporary : 1;
|
||||||
|
unsigned IsExternal : 1;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MCSymbol(const char *_Name, bool _IsTemporary)
|
MCSymbol(const char *_Name, bool _IsTemporary)
|
||||||
: Section(0), Name(_Name), IsTemporary(_IsTemporary) {}
|
: Section(0), Name(_Name), IsTemporary(_IsTemporary), IsExternal(false) {}
|
||||||
|
|
||||||
MCSection *getSection() const { return Section; }
|
MCSection *getSection() const { return Section; }
|
||||||
void setSection(MCSection *Value) { Section = Value; }
|
void setSection(MCSection *Value) { Section = Value; }
|
||||||
|
|
||||||
|
bool isExternal() const { return IsExternal; }
|
||||||
|
void setExternal(bool Value) { IsExternal = Value; }
|
||||||
|
|
||||||
const std::string &getName() const { return Name; }
|
const std::string &getName() const { return Name; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
#include "llvm/MC/MCContext.h"
|
#include "llvm/MC/MCContext.h"
|
||||||
#include "llvm/MC/MCInst.h"
|
#include "llvm/MC/MCInst.h"
|
||||||
#include "llvm/MC/MCStreamer.h"
|
#include "llvm/MC/MCStreamer.h"
|
||||||
|
#include "llvm/MC/MCSymbol.h"
|
||||||
#include "llvm/Support/SourceMgr.h"
|
#include "llvm/Support/SourceMgr.h"
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
@ -82,12 +83,19 @@ bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
|
|||||||
return true;
|
return true;
|
||||||
Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
|
Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
|
||||||
return false;
|
return false;
|
||||||
case asmtok::Identifier:
|
case asmtok::Identifier: {
|
||||||
// This is a label, this should be parsed as part of an expression, to
|
// This is a label, this should be parsed as part of an expression, to
|
||||||
// handle things like LFOO+4.
|
// handle things like LFOO+4.
|
||||||
Res = new AsmSymbolRefExpr(Ctx.GetOrCreateSymbol(Lexer.getCurStrVal()));
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
|
||||||
|
|
||||||
|
// If this is use of an undefined symbol then mark it external.
|
||||||
|
if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
|
||||||
|
Sym->setExternal(true);
|
||||||
|
|
||||||
|
Res = new AsmSymbolRefExpr(Sym);
|
||||||
Lexer.Lex(); // Eat identifier.
|
Lexer.Lex(); // Eat identifier.
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
case asmtok::IntVal:
|
case asmtok::IntVal:
|
||||||
Res = new AsmConstantExpr(Lexer.getCurIntVal());
|
Res = new AsmConstantExpr(Lexer.getCurIntVal());
|
||||||
Lexer.Lex(); // Eat identifier.
|
Lexer.Lex(); // Eat identifier.
|
||||||
@ -270,16 +278,28 @@ bool AsmParser::ParseStatement() {
|
|||||||
|
|
||||||
// Consume the identifier, see what is after it.
|
// Consume the identifier, see what is after it.
|
||||||
switch (Lexer.Lex()) {
|
switch (Lexer.Lex()) {
|
||||||
case asmtok::Colon:
|
case asmtok::Colon: {
|
||||||
// identifier ':' -> Label.
|
// identifier ':' -> Label.
|
||||||
Lexer.Lex();
|
Lexer.Lex();
|
||||||
|
|
||||||
|
// Diagnose attempt to use a variable as a label.
|
||||||
|
//
|
||||||
|
// FIXME: Diagnostics. Note the location of the definition as a label.
|
||||||
|
// FIXME: This doesn't diagnose assignment to a symbol which has been
|
||||||
|
// implicitly marked as external.
|
||||||
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
|
||||||
|
if (Sym->getSection())
|
||||||
|
return Error(IDLoc, "invalid symbol redefinition");
|
||||||
|
if (Ctx.GetSymbolValue(Sym))
|
||||||
|
return Error(IDLoc, "symbol already used as assembler variable");
|
||||||
|
|
||||||
// Since we saw a label, create a symbol and emit it.
|
// Since we saw a label, create a symbol and emit it.
|
||||||
// FIXME: If the label starts with L it is an assembler temporary label.
|
// FIXME: If the label starts with L it is an assembler temporary label.
|
||||||
// Why does the client of this api need to know this?
|
// Why does the client of this api need to know this?
|
||||||
Out.EmitLabel(Ctx.GetOrCreateSymbol(IDVal));
|
Out.EmitLabel(Sym);
|
||||||
|
|
||||||
return ParseStatement();
|
return ParseStatement();
|
||||||
|
}
|
||||||
|
|
||||||
case asmtok::Equal:
|
case asmtok::Equal:
|
||||||
// identifier '=' ... -> assignment statement
|
// identifier '=' ... -> assignment statement
|
||||||
@ -440,6 +460,9 @@ bool AsmParser::ParseStatement() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
|
bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
|
||||||
|
// FIXME: Use better location, we should use proper tokens.
|
||||||
|
SMLoc EqualLoc = Lexer.getLoc();
|
||||||
|
|
||||||
int64_t Value;
|
int64_t Value;
|
||||||
if (ParseAbsoluteExpression(Value))
|
if (ParseAbsoluteExpression(Value))
|
||||||
return true;
|
return true;
|
||||||
@ -450,9 +473,20 @@ bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
|
|||||||
// Eat the end of statement marker.
|
// Eat the end of statement marker.
|
||||||
Lexer.Lex();
|
Lexer.Lex();
|
||||||
|
|
||||||
// Get the symbol for this name.
|
// Diagnose assignment to a label.
|
||||||
|
//
|
||||||
|
// FIXME: Diagnostics. Note the location of the definition as a label.
|
||||||
|
// FIXME: This doesn't diagnose assignment to a symbol which has been
|
||||||
|
// implicitly marked as external.
|
||||||
// FIXME: Handle '.'.
|
// FIXME: Handle '.'.
|
||||||
|
// FIXME: Diagnose assignment to protected identifier (e.g., register name).
|
||||||
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
|
MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
|
||||||
|
if (Sym->getSection())
|
||||||
|
return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
|
||||||
|
if (Sym->isExternal())
|
||||||
|
return Error(EqualLoc, "invalid assignment to external symbol");
|
||||||
|
|
||||||
|
// Do the assignment.
|
||||||
Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
|
Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user