23 #ifndef __CLRX_ASSEMBLER_H__
24 #define __CLRX_ASSEMBLER_H__
26 #include <CLRX/Config.h>
35 #include <unordered_set>
36 #include <unordered_map>
49 ASM_FORCE_ADD_SYMBOLS = 2,
63 enum : AsmExprTargetType
91 void printWarning(
const char* linePtr,
const char* message);
93 void printError(
const char* linePtr,
const char* message);
100 cxbyte signess = WS_BOTH);
109 const char* linePtr,
const char* lineEnd, std::vector<cxbyte>& output) = 0;
112 cxbyte* sectionData,
size_t offset, AsmExprTargetType targetType,
113 cxuint sectionId, uint64_t value) = 0;
118 Flags regFlags = 0) = 0;
121 Flags& regFlags)
const = 0;
126 cxuint& regStart, cxuint& regEnd) = 0;
128 virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType) = 0;
146 uint16_t curArchMask;
153 void assemble(
const CString& mnemonic,
const char* mnemPlace,
const char* linePtr,
154 const char* lineEnd, std::vector<cxbyte>& output);
156 cxbyte* sectionData,
size_t offset, AsmExprTargetType targetType,
157 cxuint sectionId, uint64_t value);
216 struct AsmExprTarget;
267 explicit AsmSymbol(cxuint _sectionId, uint64_t _value,
bool _onceDefined =
false) :
353 class TempSymbolSnapshotMap;
358 bool relativeSymOccurs;
361 std::unique_ptr<LineCol[]> messagePositions;
362 std::unique_ptr<AsmExprArg[]> args;
367 pos.
lineNo = messagePositions[msgPosIndex].lineNo;
368 pos.
colNo = messagePositions[msgPosIndex].colNo;
372 static bool makeSymbolSnapshot(
Assembler& assembler,
373 TempSymbolSnapshotMap* snapshotMap,
const AsmSymbolEntry& symEntry,
374 AsmSymbolEntry*& outSymEntry,
const AsmSourcePos* topParentSourcePos);
377 void setParams(
size_t symOccursNum,
bool relativeSymOccurs,
378 size_t _opsNum,
const AsmExprOp* ops,
size_t opPosNum,
const LineCol* opPos,
379 size_t argsNum,
const AsmExprArg* args,
bool baseExpr =
false);
383 size_t opsNum,
size_t opPosNum,
size_t argsNum,
bool baseExpr =
false);
386 size_t opsNum,
const AsmExprOp* ops,
size_t opPosNum,
388 bool baseExpr =
false);
394 {
return ops.empty(); }
401 { target = _target; }
411 {
return evaluate(assembler, 0, ops.size(), value, sectionId); }
423 uint64_t& value, cxuint& sectionId)
const;
434 bool makeBase =
false,
bool dontResolveSymbolsLater =
false);
445 bool makeBase =
false,
bool dontResolveSymbolsLater =
false);
449 {
return (AsmExprOp::FIRST_ARG <= op && op <= AsmExprOp::LAST_ARG); }
461 {
return symOccursNum; }
464 {
return relativeSymOccurs; }
467 {
return --symOccursNum!=0; }
477 {
return args.get(); }
480 {
return sourcePos; }
482 size_t toTop(
size_t opIndex)
const;
485 static bool makeSymbolSnapshot(
Assembler& assembler,
const AsmSymbolEntry& symEntry,
486 AsmSymbolEntry*& outSymEntry,
const AsmSourcePos* parentExprSourcePos);
507 uint64_t value, cxuint sectionId)
510 args[occurrence.
argIndex].relValue.value = value;
511 args[occurrence.
argIndex].relValue.sectionId = sectionId;
513 relativeSymOccurs =
true;
563 typedef std::pair<CString, uint64_t>
DefSym;
565 typedef std::unordered_map<CString, RefPtr<const AsmMacro> >
MacroMap;
579 friend struct AsmParseUtils;
580 friend struct AsmPseudoOps;
581 friend struct AsmGalliumPseudoOps;
582 friend struct AsmAmdPseudoOps;
583 friend struct AsmAmdCL2PseudoOps;
584 friend struct GCNAsmUtils;
589 uint32_t driverVersion;
592 bool resolvingRelocs;
594 std::vector<DefSym> defSyms;
595 std::vector<CString> includeDirs;
596 std::vector<AsmSection> sections;
597 AsmSymbolMap symbolMap;
598 std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
599 std::vector<AsmRelocation> relocations;
602 std::vector<AsmKernel> kernels;
608 cxuint inclusionLevel;
609 cxuint macroSubstLevel;
610 cxuint repetitionLevel;
611 bool lineAlreadyRead;
617 cxuint filenameIndex;
618 std::stack<AsmInputFilter*> asmInputFilters;
621 std::ostream& messageStream;
622 std::ostream& printStream;
626 std::stack<AsmClause> clauses;
628 cxuint currentKernel;
629 cxuint& currentSection;
630 uint64_t& currentOutPos;
638 AsmSourcePos getSourcePos(
size_t pos)
const
640 AsmSourcePos getSourcePos(
const char* linePtr)
const
641 {
return getSourcePos(linePtr-line); }
643 void printWarning(
const AsmSourcePos& pos,
const char* message);
644 void printError(
const AsmSourcePos& pos,
const char* message);
646 void printWarning(
const char* linePtr,
const char* message)
647 { printWarning(getSourcePos(linePtr), message); }
648 void printError(
const char* linePtr,
const char* message)
649 { printError(getSourcePos(linePtr), message); }
651 void printWarning(LineCol lineCol,
const char* message)
652 { printWarning(getSourcePos(lineCol), message); }
653 void printError(LineCol lineCol,
const char* message)
654 { printError(getSourcePos(lineCol), message); }
656 LineCol translatePos(
const char* linePtr)
const
657 {
return currentInputFilter->
translatePos(linePtr-line); }
658 LineCol translatePos(
size_t pos)
const
661 bool parseLiteral(uint64_t& value,
const char*& linePtr);
662 bool parseString(std::string& outString,
const char*& linePtr);
664 enum class ParseState
674 ParseState parseSymbol(
const char*& linePtr, AsmSymbolEntry*& entry,
675 bool localLabel =
true,
bool dontCreateSymbol =
false);
676 bool skipSymbol(
const char*& linePtr);
678 bool setSymbol(AsmSymbolEntry& symEntry, uint64_t value, cxuint sectionId);
680 bool assignSymbol(
const CString& symbolName,
const char* symbolPlace,
681 const char* linePtr,
bool reassign =
true,
bool baseExpr =
false);
683 bool assignOutputCounter(
const char* symbolPlace, uint64_t value, cxuint sectionId,
684 cxbyte fillValue = 0);
686 void parsePseudoOps(
const CString& firstName,
const char* stmtPlace,
687 const char* linePtr);
690 bool skipClauses(
bool exitm =
false);
691 bool putMacroContent(RefPtr<AsmMacro> macro);
692 bool putRepetitionContent(AsmRepeat& repeat);
694 void initializeOutputFormat();
696 bool pushClause(
const char*
string,
AsmClauseType clauseType)
699 return pushClause(
string, clauseType,
true, included);
701 bool pushClause(
const char*
string,
AsmClauseType clauseType,
702 bool satisfied,
bool& included);
704 bool popClause(
const char*
string,
AsmClauseType clauseType);
707 bool includeFile(
const char* pseudoOpPlace,
const std::string& filename);
709 ParseState makeMacroSubstitution(
const char*
string);
711 bool parseMacroArgValue(
const char*& linePtr, std::string& outStr);
713 void putData(
size_t size,
const cxbyte* data)
715 AsmSection& section = sections[currentSection];
716 section.content.insert(section.content.end(), data, data+size);
717 currentOutPos += size;
720 cxbyte* reserveData(
size_t size, cxbyte fillValue = 0);
722 void goToMain(
const char* pseudoOpPlace);
723 void goToKernel(
const char* pseudoOpPlace,
const char* kernelName);
724 void goToSection(
const char* pseudoOpPlace,
const char* sectionName, uint64_t align=0);
725 void goToSection(
const char* pseudoOpPlace,
const char* sectionName,
727 void goToSection(
const char* pseudoOpPlace, cxuint sectionId, uint64_t align=0);
729 void printWarningForRange(cxuint bits, uint64_t value,
const AsmSourcePos& pos,
730 cxbyte signess = WS_BOTH);
732 bool checkReservedName(
const CString& name);
734 bool isAddressableSection()
const
737 (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
739 bool isWriteableSection()
const
742 (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
744 bool isResolvableSection()
const
749 bool isResolvableSection(cxuint sectionId)
const
769 explicit Assembler(
const CString& filename, std::istream& input,
Flags flags = 0,
772 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
783 explicit Assembler(
const Array<CString>& filenames,
Flags flags = 0,
786 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
802 {
return driverVersion; }
805 { this->driverVersion = driverVersion; }
809 {
return deviceType; }
812 { this->deviceType = deviceType; }
818 { format = binFormat; }
824 { _64bit = this64Bit; }
830 { this->flags = flags; }
833 {
return includeDirs; }
838 {
return symbolMap; }
844 {
return kernelMap; }
857 {
return formatHandler; }
861 {
return assembler.printWarning(linePtr, message); }
864 {
return assembler.printError(linePtr, message); }
868 {
return assembler.printWarningForRange(bits, value, pos, signess); }
871 {
return assembler.printWarning(sourcePos, message); }
874 {
return assembler.printError(sourcePos, message); }
AsmRepeatInputFilter or AsmIRPInputFilter.
void undefine()
make symbol as undefined
bool is64Bit() const
get bitness
Definition: Assembler.h:820
void set64Bit(bool this64Bit)
set bitness
Definition: Assembler.h:823
virtual void assemble(const CString &mnemonic, const char *mnemPlace, const char *linePtr, const char *lineEnd, std::vector< cxbyte > &output)=0
assemble single line
common definitions for assembler and disassembler
main class of assembler
Definition: Assembler.h:559
AsmExprTargetType type
type of target
Definition: Assembler.h:297
enable all warnings for assembler
Definition: Assembler.h:48
virtual bool parseRegisterRange(const char *&linePtr, cxuint ®Start, cxuint ®End)=0
parse register range
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: Assembler.h:350
AsmSymbolEntry * symbol
symbol
Definition: Assembler.h:343
AsmSymbol(bool _onceDefined=false)
empty constructor
Definition: Assembler.h:255
kernel entry structure
Definition: Assembler.h:543
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
void assemble(const CString &mnemonic, const char *mnemPlace, const char *linePtr, const char *lineEnd, std::vector< cxbyte > &output)
assemble single line
Flags getFlags() const
get flags
Definition: Assembler.h:826
AsmExpression * expression
expression of symbol (if not resolved)
Definition: Assembler.h:249
bool operator==(const AsmExprSymbolOccurrence &b) const
comparison operator
Definition: Assembler.h:230
cxuint kernelId
kernel id (optional)
Definition: Assembler.h:520
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:567
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:808
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:840
ISAAssembler(Assembler &assembler)
constructor
ColNo colNo
column number, for macro substitution and IRP points to column preprocessed line
Definition: AsmSource.h:49
AsmSymbolEntry * symbol
symbol entry (if ASMXTGT_SYMBOL)
Definition: Assembler.h:300
cxuint regRange
if symbol is register range
Definition: Assembler.h:246
target for assembler expression
Definition: Assembler.h:295
void setDriverVersion(uint32_t driverVersion)
set AMD driver version
Definition: Assembler.h:804
~AsmSymbol()
destructor
Definition: Assembler.h:489
AsmClauseType
type of clause
Definition: Assembler.h:533
Assembler & assembler
assembler
Definition: Assembler.h:88
AsmExprOp
assembler expression operator
Definition: Assembler.h:171
assembler section
Definition: Assembler.h:517
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:563
AsmExprTarget()
empty constructor
Definition: Assembler.h:307
handles raw code format
Definition: AsmFormats.h:175
static AsmExprTarget symbolTarget(AsmSymbolEntry *entry)
make symbol target for expression
Definition: Assembler.h:315
cxbyte info
ELF symbol info.
Definition: Assembler.h:239
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:863
const AsmExprTarget & getTarget() const
get targer of expression
Definition: Assembler.h:457
signed (arithmetic) shift right
static bool isUnaryOp(AsmExprOp op)
return true if is unary op
Definition: Assembler.h:451
AsmSourcePos sourcePos
source position of definition
Definition: Assembler.h:546
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:860
size_t getSymOccursNum() const
get number of symbol occurrences in expression
Definition: Assembler.h:460
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:137
assembler symbol occurrence in expression
Definition: Assembler.h:223
Flags regFlags
define what extra register must be included
Definition: Assembler.h:139
cxuint sectionId
section id
Definition: Assembler.h:238
void addOccurrenceInExpr(AsmExpression *expr, size_t argIndex, size_t opIndex)
adds occurrence in expression
Definition: Assembler.h:276
virtual void setAllocatedRegisters(const cxuint *regs=nullptr, Flags regFlags=0)=0
set allocated registers (if regs is null then reset them)
ColNo colNo
column number
Definition: AsmSource.h:157
AsmSymbol(cxuint _sectionId, uint64_t _value, bool _onceDefined=false)
constructor with value and section id
Definition: Assembler.h:267
bool readLine()
helper for testing
cxuint resolving
helper
Definition: Assembler.h:243
cxuint RelocType
relocation type
Definition: Commons.h:32
const cxuint * getAllocatedRegisters(size_t ®TypesNum, Flags ®Flags) const
get allocated register numbers after assemblying
cxuint onceDefined
symbol can be only once defined (likes labels)
Definition: Assembler.h:242
void setTarget(AsmExprTarget _target)
set target of expression
Definition: Assembler.h:400
virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType)=0
return true if expresion of target fit to value with specified bits
bool isAbsoluteSymbol(const AsmSymbol &symbol) const
returns true if symbol contains absolute value
virtual bool checkMnemonic(const CString &mnemonic) const =0
check if name is mnemonic
line and column
Definition: AsmSource.h:44
bool isEmpty() const
return true if expression is empty
Definition: Assembler.h:393
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: Assembler.h:290
cxuint sectionId
sectionId
Definition: Assembler.h:502
void clearOccurrencesInExpr()
clear list of occurrences in expression
LineNo lineNo
line number of top-most source
Definition: AsmSource.h:156
cxuint hasValue
symbol is defined
Definition: Assembler.h:241
size_t offset
offset of relocation
Definition: Assembler.h:340
bool resolveCode(const AsmSourcePos &sourcePos, cxuint targetSectionId, cxbyte *sectionData, size_t offset, AsmExprTargetType targetType, cxuint sectionId, uint64_t value)
resolve code with location, target and value
void setFlags(Flags flags)
set flags
Definition: Assembler.h:829
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:553
static AsmExpression * parse(Assembler &assembler, size_t &linePos, bool makeBase=false, bool dontResolveSymbolsLater=false)
parse expression. By default, also gets values of symbol or creates them
uint64_t value
value of symbol
Definition: Assembler.h:247
AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
constructor to create custom target
Definition: Assembler.h:310
virtual const cxuint * getAllocatedRegisters(size_t ®TypesNum, Flags ®Flags) const =0
get allocated register numbers after assemblying
bool checkMnemonic(const CString &mnemonic) const
check if name is mnemonic
assembler relocation
Definition: Assembler.h:337
size_t opIndex
operator index
Definition: Assembler.h:227
const char * name
name of kernel
Definition: Assembler.h:545
AsmSectionType
assembler section type
Definition: AsmFormats.h:44
absolute section id
Definition: AsmFormats.h:73
bool assemble()
main routine to assemble code
Assembler(const CString &filename, std::istream &input, Flags flags=0, BinaryFormat format=BinaryFormat::AMD, GPUDeviceType deviceType=GPUDeviceType::CAPE_VERDE, std::ostream &msgStream=std::cerr, std::ostream &printStream=std::cout)
constructor with filename and input stream
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:832
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:138
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:555
AsmSymbolEntry * symbol
if symbol
Definition: Assembler.h:498
void addInitialDefSym(const CString &symName, uint64_t value)
add initiali defsyms
absolute symbol without defined value
void substituteOccurrence(AsmExprSymbolOccurrence occurrence, uint64_t value, cxuint sectionId=ASMSECT_ABS)
substitute occurrence in expression by value
Definition: Assembler.h:506
void removeOccurrenceInExpr(AsmExpression *expr, size_t argIndex, size_t opIndex)
remove occurrence in expression
cxuint base
with base expression
Definition: Assembler.h:244
static bool isArg(AsmExprOp op)
return true if is argument op
Definition: Assembler.h:448
AsmClauseType type
type of clause
Definition: Assembler.h:552
bool unrefSymOccursNum()
unreference symbol occurrences in expression (used internally)
Definition: Assembler.h:466
~AsmExpression()
destructor
only for running tests
Definition: Assembler.h:51
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:843
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:814
static bool isBinaryOp(AsmExprOp op)
return true if is binary op
Definition: Assembler.h:454
bool parseRegisterRange(const char *&linePtr, cxuint ®Start, cxuint ®End)
parse register range
uint64_t size
size of symbol
Definition: Assembler.h:248
size_t getSize() const
get section's size
Definition: Assembler.h:528
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:837
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: Assembler.h:61
uint64_t size
section size
Definition: Assembler.h:524
std::vector< AsmExprSymbolOccurrence > occurrencesInExprs
Definition: Assembler.h:252
std::vector< cxbyte > content
content of section
Definition: Assembler.h:525
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: Assembler.h:292
Flags flags
section flags
Definition: Assembler.h:522
void addIncludeDir(const CString &includeDir)
adds include directory
uint64_t value
value
Definition: Assembler.h:499
cxuint refCount
reference counter (for internal use only)
Definition: Assembler.h:237
GPUDeviceType
type of GPU device
Definition: GPUId.h:38
void writeBinary(const char *filename) const
write binary to file
void setAllocatedRegisters(const cxuint *regs, Flags regFlags)
set allocated registers (if regs is null then reset them)
size_t offset
offset of destination
Definition: Assembler.h:303
size_t argIndex
argument index
Definition: Assembler.h:226
cxuint sectionId
section id where relocation is present
Definition: Assembler.h:339
cxuint sectionId
section id of destination
Definition: Assembler.h:302
void fillAlignment(size_t size, cxbyte *output)
fill alignment when value is not given
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:811
utilities for other libraries and programs
const Array< AsmExprOp > & getOps() const
get operators list
Definition: Assembler.h:473
RelocType type
relocation type
Definition: Assembler.h:341
target is 32-bit word
Definition: Assembler.h:68
uint64_t alignment
section alignment
Definition: Assembler.h:523
static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
make n-bit word target for expression
Definition: Assembler.h:325
AsmExpression * createForSnapshot(const AsmSourcePos *exprSourcePos) const
helper to create symbol snapshot. Creates initial expression for symbol snapshot
GCN arch assembler.
Definition: Assembler.h:132
bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType)
return true if expresion of target fit to value with specified bits
virtual bool resolveCode(const AsmSourcePos &sourcePos, cxuint targetSectionId, cxbyte *sectionData, size_t offset, AsmExprTargetType targetType, cxuint sectionId, uint64_t value)=0
resolve code with location, target and value
virtual ~ISAAssembler()
destructor
AsmSectionType type
type of section
Definition: Assembler.h:521
register pool numbers
Definition: Assembler.h:136
assembler's clause (if,else,macro,rept)
Definition: Assembler.h:550
AsmSymbol(AsmExpression *expr, bool _onceDefined=false, bool _base=false)
constructor with expression
Definition: Assembler.h:261
virtual void fillAlignment(size_t size, cxbyte *output)=0
fill alignment when value is not given
LineNo lineNo
line number
Definition: AsmSource.h:46
struct CLRX::AsmExprArg::@13 relValue
relative value (with section)
cxbyte other
ELF symbol other.
Definition: Assembler.h:240
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:801
target is 64-bit word
Definition: Assembler.h:69
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:856
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:866
target is byte
Definition: Assembler.h:66
handles GalliumCompute format
Definition: AsmFormats.h:349
const char * name
section name
Definition: Assembler.h:519
an assembler sources handling
cxuint relSectionId
section for which relocation is defined
Definition: Assembler.h:344
assembler symbol structure
Definition: Assembler.h:235
bool isDefined() const
return true if symbol defined (have value or expression)
Definition: Assembler.h:285
const AsmExprArg * getArgs() const
get argument list
Definition: Assembler.h:476
handles AMD Catalyst format
Definition: AsmFormats.h:201
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:267
~GCNAssembler()
destructor
cxuint snapshot
if symbol is snapshot
Definition: Assembler.h:245
size_t hasRelativeSymOccurs() const
get number of symbol occurrences in expression
Definition: Assembler.h:463
std::unordered_map< CString, RefPtr< const AsmMacro > > MacroMap
macro map type
Definition: Assembler.h:565
section is unresolvable
Definition: AsmFormats.h:84
ISA assembler class.
Definition: Assembler.h:85
uint64_t addend
addend
Definition: Assembler.h:346
GCNAssembler(Assembler &assembler)
constructor
AsmExpression * expression
target expression pointer
Definition: Assembler.h:225
bool evaluate(Assembler &assembler, uint64_t &value, cxuint §ionId) const
try to evaluate expression
Definition: Assembler.h:410
assembler expression argument
Definition: Assembler.h:496
simple C-string container
Definition: CString.h:38
all flags
Definition: Assembler.h:52
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:817
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:554
target is symbol
Definition: Assembler.h:65
target is 16-bit word
Definition: Assembler.h:67
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:846
const AsmSourcePos & getSourcePos() const
get source position
Definition: Assembler.h:479
assembler source position
Definition: AsmSource.h:152