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,
64 enum : AsmExprTargetType
94 void printWarning(
const char* linePtr,
const char* message);
96 void printError(
const char* linePtr,
const char* message);
103 cxbyte signess = WS_BOTH);
112 const char* linePtr,
const char* lineEnd, std::vector<cxbyte>& output) = 0;
115 cxbyte* sectionData,
size_t offset, AsmExprTargetType targetType,
116 cxuint sectionId, uint64_t value) = 0;
121 Flags regFlags = 0) = 0;
124 Flags& regFlags)
const = 0;
129 cxuint& regEnd,
const AsmRegVar*& regVar) = 0;
131 virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType) = 0;
134 const char* end, cxuint& type) = 0;
152 uint16_t curArchMask;
159 void assemble(
const CString& mnemonic,
const char* mnemPlace,
const char* linePtr,
160 const char* lineEnd, std::vector<cxbyte>& output);
162 cxbyte* sectionData,
size_t offset, AsmExprTargetType targetType,
163 cxuint sectionId, uint64_t value);
252 cxuint onceDefined:1;
269 refCount(1), sectionId(
ASMSECT_ABS), info(0), other(0), hasValue(false),
270 onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
271 regRange(false), value(0), size(0), expression(nullptr)
275 refCount(1), sectionId(
ASMSECT_ABS), info(0), other(0), hasValue(false),
276 onceDefined(_onceDefined), resolving(false), base(_base),
277 snapshot(false), regRange(false), value(0), size(0), expression(expr)
280 explicit AsmSymbol(cxuint _sectionId, uint64_t _value,
bool _onceDefined =
false) :
281 refCount(1), sectionId(_sectionId), info(0), other(0), hasValue(true),
282 onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
283 regRange(false), value(_value), size(0), expression(nullptr)
290 { occurrencesInExprs.push_back({expr, argIndex, opIndex}); }
292 void removeOccurrenceInExpr(
AsmExpression* expr,
size_t argIndex,
size_t opIndex);
294 void clearOccurrencesInExpr();
299 {
return hasValue || expression!=
nullptr; }
324 : type(_type), sectionId(_sectionId), offset(_offset)
366 class TempSymbolSnapshotMap;
371 bool relativeSymOccurs;
374 std::unique_ptr<LineCol[]> messagePositions;
375 std::unique_ptr<AsmExprArg[]> args;
380 pos.
lineNo = messagePositions[msgPosIndex].lineNo;
381 pos.
colNo = messagePositions[msgPosIndex].colNo;
386 TempSymbolSnapshotMap* snapshotMap,
const AsmSymbolEntry& symEntry,
387 AsmSymbolEntry*& outSymEntry,
const AsmSourcePos* topParentSourcePos);
390 void setParams(
size_t symOccursNum,
bool relativeSymOccurs,
391 size_t _opsNum,
const AsmExprOp* ops,
size_t opPosNum,
const LineCol* opPos,
392 size_t argsNum,
const AsmExprArg* args,
bool baseExpr =
false);
396 size_t opsNum,
size_t opPosNum,
size_t argsNum,
bool baseExpr =
false);
399 size_t opsNum,
const AsmExprOp* ops,
size_t opPosNum,
401 bool baseExpr =
false);
407 {
return ops.empty(); }
414 { target = _target; }
424 {
return evaluate(assembler, 0, ops.size(), value, sectionId); }
435 bool evaluate(
Assembler& assembler,
size_t opStart,
size_t opEnd,
436 uint64_t& value, cxuint& sectionId)
const;
447 bool makeBase =
false,
bool dontResolveSymbolsLater =
false);
458 bool makeBase =
false,
bool dontResolveSymbolsLater =
false);
462 {
return (AsmExprOp::FIRST_ARG <= op && op <= AsmExprOp::LAST_ARG); }
474 {
return symOccursNum; }
477 {
return relativeSymOccurs; }
480 {
return --symOccursNum!=0; }
490 {
return args.get(); }
493 {
return sourcePos; }
495 size_t toTop(
size_t opIndex)
const;
498 static bool makeSymbolSnapshot(
Assembler& assembler,
const AsmSymbolEntry& symEntry,
499 AsmSymbolEntry*& outSymEntry,
const AsmSourcePos* parentExprSourcePos);
504 if (base)
delete expression;
505 clearOccurrencesInExpr();
520 uint64_t value, cxuint sectionId)
523 args[occurrence.
argIndex].relValue.value = value;
524 args[occurrence.
argIndex].relValue.sectionId = sectionId;
526 relativeSymOccurs =
true;
529 typedef cxbyte AsmRegField;
538 GCNFIELD_SMRD_SOFFSET,
547 GCNFIELD_VINTRP_VSRC0,
548 GCNFIELD_VINTRP_VDST,
565 GCNFIELD_DPPSDWA_SRC0,
568 GCNFIELD_SMEM_OFFSET,
588 uint16_t rstart, rend;
607 std::unordered_map<CString, AsmRegVar>
regVars;
616 { regVarUsages.push_back(varUsage); }
620 {
return ((flags&ASMSECT_WRITEABLE) != 0) ? content.size() : size; }
654 typedef std::pair<CString, uint64_t>
DefSym;
656 typedef std::unordered_map<CString, RefPtr<const AsmMacro> >
MacroMap;
671 friend struct AsmParseUtils;
672 friend struct AsmPseudoOps;
673 friend struct AsmGalliumPseudoOps;
674 friend struct AsmAmdPseudoOps;
675 friend struct AsmAmdCL2PseudoOps;
676 friend struct AsmROCmPseudoOps;
677 friend struct GCNAsmUtils;
682 uint32_t driverVersion;
685 bool resolvingRelocs;
687 std::vector<DefSym> defSyms;
688 std::vector<CString> includeDirs;
689 std::vector<AsmSection> sections;
690 AsmSymbolMap symbolMap;
691 std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
692 std::vector<AsmRelocation> relocations;
695 std::vector<AsmKernel> kernels;
702 cxuint inclusionLevel;
703 cxuint macroSubstLevel;
704 cxuint repetitionLevel;
705 bool lineAlreadyRead;
711 cxuint filenameIndex;
712 std::stack<AsmInputFilter*> asmInputFilters;
715 std::ostream& messageStream;
716 std::ostream& printStream;
720 std::stack<AsmClause> clauses;
722 cxuint currentKernel;
723 cxuint& currentSection;
724 uint64_t& currentOutPos;
735 {
return getSourcePos(linePtr-line); }
740 void printWarning(
const char* linePtr,
const char* message)
742 void printError(
const char* linePtr,
const char* message)
743 {
printError(getSourcePos(linePtr), message); }
748 {
printError(getSourcePos(lineCol), message); }
750 LineCol translatePos(
const char* linePtr)
const 751 {
return currentInputFilter->
translatePos(linePtr-line); }
752 LineCol translatePos(
size_t pos)
const 755 bool parseLiteral(uint64_t& value,
const char*& linePtr);
756 bool parseString(std::string& outString,
const char*& linePtr);
758 enum class ParseState
768 ParseState parseSymbol(
const char*& linePtr, AsmSymbolEntry*& entry,
769 bool localLabel =
true,
bool dontCreateSymbol =
false);
770 bool skipSymbol(
const char*& linePtr);
772 bool setSymbol(AsmSymbolEntry& symEntry, uint64_t value, cxuint sectionId);
774 bool assignSymbol(
const CString& symbolName,
const char* symbolPlace,
775 const char* linePtr,
bool reassign =
true,
bool baseExpr =
false);
777 bool assignOutputCounter(
const char* symbolPlace, uint64_t value, cxuint sectionId,
778 cxbyte fillValue = 0);
780 void parsePseudoOps(
const CString& firstName,
const char* stmtPlace,
781 const char* linePtr);
784 bool skipClauses(
bool exitm =
false);
786 bool putRepetitionContent(
AsmRepeat& repeat);
788 void initializeOutputFormat();
790 bool pushClause(
const char*
string,
AsmClauseType clauseType)
793 return pushClause(
string, clauseType,
true, included);
795 bool pushClause(
const char*
string,
AsmClauseType clauseType,
796 bool satisfied,
bool& included);
798 bool popClause(
const char*
string,
AsmClauseType clauseType);
801 bool includeFile(
const char* pseudoOpPlace,
const std::string& filename);
803 ParseState makeMacroSubstitution(
const char*
string);
805 bool parseMacroArgValue(
const char*& linePtr, std::string& outStr);
807 void putData(
size_t size,
const cxbyte* data)
809 AsmSection& section = sections[currentSection];
811 currentOutPos += size;
814 cxbyte* reserveData(
size_t size, cxbyte fillValue = 0);
816 void goToMain(
const char* pseudoOpPlace);
817 void goToKernel(
const char* pseudoOpPlace,
const char* kernelName);
818 void goToSection(
const char* pseudoOpPlace,
const char* sectionName, uint64_t align=0);
819 void goToSection(
const char* pseudoOpPlace,
const char* sectionName,
821 void goToSection(
const char* pseudoOpPlace, cxuint sectionId, uint64_t align=0);
824 cxbyte signess = WS_BOTH);
826 bool checkReservedName(
const CString& name);
828 bool isAddressableSection()
const 831 (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
833 bool isWriteableSection()
const 836 (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
838 bool isResolvableSection()
const 843 bool isResolvableSection(cxuint sectionId)
const 866 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
880 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
888 void writeBinary(
const char* filename)
const;
890 void writeBinary(std::ostream& outStream)
const;
896 {
return driverVersion; }
899 { this->driverVersion = driverVersion; }
903 {
return deviceType; }
906 { this->deviceType = deviceType; }
912 { format = binFormat; }
918 { _64bit = this64Bit; }
924 { this->flags = flags; }
927 {
return includeDirs; }
929 void addIncludeDir(
const CString& includeDir);
932 {
return symbolMap; }
938 {
return kernelMap; }
944 bool isAbsoluteSymbol(
const AsmSymbol& symbol)
const;
947 void addInitialDefSym(
const CString& symName, uint64_t value);
951 {
return formatHandler; }
955 {
return assembler.printWarning(linePtr, message); }
958 {
return assembler.printError(linePtr, message); }
962 {
return assembler.printWarningForRange(bits, value, pos, signess); }
965 {
return assembler.printWarning(sourcePos, message); }
968 {
return assembler.printError(sourcePos, message); }
AsmRepeatInputFilter or AsmIRPInputFilter.
bool is64Bit() const
get bitness
Definition: Assembler.h:914
void set64Bit(bool this64Bit)
set bitness
Definition: Assembler.h:917
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:650
AsmExprTargetType type
type of target
Definition: Assembler.h:310
enable all warnings for assembler
Definition: Assembler.h:48
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: Assembler.h:363
AsmSymbolEntry * symbol
symbol
Definition: Assembler.h:356
AsmSymbol(bool _onceDefined=false)
empty constructor
Definition: Assembler.h:268
kernel entry structure
Definition: Assembler.h:634
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
Flags getFlags() const
get flags
Definition: Assembler.h:920
AsmExpression * expression
expression of symbol (if not resolved)
Definition: Assembler.h:260
bool operator==(const AsmExprSymbolOccurrence &b) const
comparison operator
Definition: Assembler.h:238
assembler repeat
Definition: AsmSource.h:238
cxuint kernelId
kernel id (optional)
Definition: Assembler.h:599
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:658
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:902
virtual bool parseRegisterType(const char *&linePtr, const char *end, cxuint &type)=0
parse register type for '.reg' pseudo-op
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:934
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:313
target for assembler expression
Definition: Assembler.h:308
void setDriverVersion(uint32_t driverVersion)
set AMD driver version
Definition: Assembler.h:898
reference pointer based on Glibmm refptr
Definition: Utilities.h:761
~AsmSymbol()
destructor
Definition: Assembler.h:502
AsmClauseType
type of clause
Definition: Assembler.h:624
Assembler & assembler
assembler
Definition: Assembler.h:91
AsmExprOp
assembler expression operator
Definition: Assembler.h:179
assembler section
Definition: Assembler.h:596
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:654
AsmExprTarget()
empty constructor
Definition: Assembler.h:320
handles raw code format
Definition: AsmFormats.h:180
static AsmExprTarget symbolTarget(AsmSymbolEntry *entry)
make symbol target for expression
Definition: Assembler.h:328
Definition: Assembler.h:578
cxbyte info
ELF symbol info.
Definition: Assembler.h:249
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:957
const AsmExprTarget & getTarget() const
get targer of expression
Definition: Assembler.h:470
signed (arithmetic) shift right
const AsmRegVar * regVar
register alignment
Definition: Assembler.h:592
static bool isUnaryOp(AsmExprOp op)
return true if is unary op
Definition: Assembler.h:464
AsmSourcePos sourcePos
source position of definition
Definition: Assembler.h:637
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:954
size_t getSymOccursNum() const
get number of symbol occurrences in expression
Definition: Assembler.h:473
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:143
assembler symbol occurrence in expression
Definition: Assembler.h:231
Flags regFlags
define what extra register must be included
Definition: Assembler.h:145
cxuint sectionId
section id
Definition: Assembler.h:248
void addOccurrenceInExpr(AsmExpression *expr, size_t argIndex, size_t opIndex)
adds occurrence in expression
Definition: Assembler.h:289
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:280
handles ROCM binary format
Definition: AsmFormats.h:437
cxuint RelocType
relocation type
Definition: Commons.h:32
void setTarget(AsmExprTarget _target)
set target of expression
Definition: Assembler.h:413
virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType)=0
return true if expresion of target fit to value with specified bits
Definition: Assembler.h:584
line and column
Definition: AsmSource.h:44
bool isEmpty() const
return true if expression is empty
Definition: Assembler.h:406
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: Assembler.h:303
cxuint sectionId
sectionId
Definition: Assembler.h:515
LineNo lineNo
line number of top-most source
Definition: AsmSource.h:156
size_t offset
offset of relocation
Definition: Assembler.h:353
void setFlags(Flags flags)
set flags
Definition: Assembler.h:923
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:644
AsmRegField regField
place in instruction
Definition: Assembler.h:587
uint64_t value
value of symbol
Definition: Assembler.h:257
AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
constructor to create custom target
Definition: Assembler.h:323
assembler relocation
Definition: Assembler.h:350
std::unordered_map< CString, AsmRegVar > regVars
register variables
Definition: Assembler.h:607
size_t opIndex
operator index
Definition: Assembler.h:235
const char * name
name of kernel
Definition: Assembler.h:636
AsmSectionType
assembler section type
Definition: AsmFormats.h:45
absolute section id
Definition: AsmFormats.h:78
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:926
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:144
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:646
main namespace
Definition: AsmFormats.h:41
AsmSymbolEntry * symbol
if symbol
Definition: Assembler.h:511
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:519
static bool isArg(AsmExprOp op)
return true if is argument op
Definition: Assembler.h:461
AsmClauseType type
type of clause
Definition: Assembler.h:643
bool unrefSymOccursNum()
unreference symbol occurrences in expression (used internally)
Definition: Assembler.h:479
virtual bool parseRegisterRange(const char *&linePtr, cxuint ®Start, cxuint ®End, const AsmRegVar *®Var)=0
parse register range
only for running tests
Definition: Assembler.h:52
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:937
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:908
static bool isBinaryOp(AsmExprOp op)
return true if is binary op
Definition: Assembler.h:467
uint64_t size
size of symbol
Definition: Assembler.h:258
size_t getSize() const
get section's size
Definition: Assembler.h:619
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:931
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: Assembler.h:62
uint64_t size
section size
Definition: Assembler.h:603
std::vector< AsmExprSymbolOccurrence > occurrencesInExprs
Definition: Assembler.h:265
std::vector< cxbyte > content
content of section
Definition: Assembler.h:604
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: Assembler.h:305
Flags flags
section flags
Definition: Assembler.h:601
uint64_t value
value
Definition: Assembler.h:512
cxuint refCount
reference counter (for internal use only)
Definition: Assembler.h:247
GPUDeviceType
type of GPU device
Definition: GPUId.h:38
size_t offset
offset of destination
Definition: Assembler.h:316
size_t argIndex
argument index
Definition: Assembler.h:234
cxuint sectionId
section id where relocation is present
Definition: Assembler.h:352
cxuint sectionId
section id of destination
Definition: Assembler.h:315
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:905
utilities for other libraries and programs
const Array< AsmExprOp > & getOps() const
get operators list
Definition: Assembler.h:486
RelocType type
relocation type
Definition: Assembler.h:354
target is 32-bit word
Definition: Assembler.h:69
uint64_t alignment
section alignment
Definition: Assembler.h:602
static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
make n-bit word target for expression
Definition: Assembler.h:338
GCN arch assembler.
Definition: Assembler.h:138
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
virtual bool checkMnemonic(const CString &mnemonic) const =0
check if name is mnemonic
AsmSectionType type
type of section
Definition: Assembler.h:600
register pool numbers
Definition: Assembler.h:142
assembler's clause (if,else,macro,rept)
Definition: Assembler.h:641
AsmSymbol(AsmExpression *expr, bool _onceDefined=false, bool _base=false)
constructor with expression
Definition: Assembler.h:274
virtual void fillAlignment(size_t size, cxbyte *output)=0
fill alignment when value is not given
LineNo lineNo
line number
Definition: AsmSource.h:46
cxbyte other
ELF symbol other.
Definition: Assembler.h:250
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:895
target is 64-bit word
Definition: Assembler.h:70
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:950
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:960
target is byte
Definition: Assembler.h:67
handles GalliumCompute format
Definition: AsmFormats.h:354
const char * name
section name
Definition: Assembler.h:598
an assembler sources handling
cxuint relSectionId
section for which relocation is defined
Definition: Assembler.h:357
assembler symbol structure
Definition: Assembler.h:245
virtual const cxuint * getAllocatedRegisters(size_t ®TypesNum, Flags ®Flags) const =0
get allocated register numbers after assemblying
bool isDefined() const
return true if symbol defined (have value or expression)
Definition: Assembler.h:298
const AsmExprArg * getArgs() const
get argument list
Definition: Assembler.h:489
std::vector< AsmVarUsage > regVarUsages
reg-var usage in section
Definition: Assembler.h:609
handles AMD Catalyst format
Definition: AsmFormats.h:206
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:272
size_t hasRelativeSymOccurs() const
get number of symbol occurrences in expression
Definition: Assembler.h:476
std::unordered_map< CString, RefPtr< const AsmMacro > > MacroMap
macro map type
Definition: Assembler.h:656
section is unresolvable
Definition: AsmFormats.h:89
ISA assembler class.
Definition: Assembler.h:88
uint64_t addend
addend
Definition: Assembler.h:359
AsmExpression * expression
target expression pointer
Definition: Assembler.h:233
bool evaluate(Assembler &assembler, uint64_t &value, cxuint §ionId) const
try to evaluate expression
Definition: Assembler.h:423
assembler expression argument
Definition: Assembler.h:509
simple C-string container
Definition: CString.h:38
all flags
Definition: Assembler.h:53
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:911
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:645
target is symbol
Definition: Assembler.h:66
target is 16-bit word
Definition: Assembler.h:68
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:940
const AsmSourcePos & getSourcePos() const
get source position
Definition: Assembler.h:492
assembler source position
Definition: AsmSource.h:152