23 #ifndef __CLRX_ASSEMBLER_H__ 24 #define __CLRX_ASSEMBLER_H__ 36 #include <unordered_set> 37 #include <unordered_map> 98 void skipBytesInInstrStruct();
145 uint16_t rend)
const = 0;
148 cxbyte rwFlags)
const = 0;
181 void printWarning(
const char* linePtr,
const char* message);
183 void printError(
const char* linePtr,
const char* message);
185 void printWarning(
const AsmSourcePos& sourcePos,
const char* message);
187 void printError(
const AsmSourcePos& sourcePos,
const char* message);
190 cxbyte signess = WS_BOTH);
201 virtual void assemble(
const CString& mnemonic,
const char* mnemPlace,
202 const char* linePtr,
const char* lineEnd, std::vector<cxbyte>& output,
207 cxuint sectionId, uint64_t value) = 0;
209 virtual bool checkMnemonic(
const CString& mnemonic)
const = 0;
211 virtual void setAllocatedRegisters(
const cxuint* regs =
nullptr,
212 Flags regFlags = 0) = 0;
214 virtual const cxuint* getAllocatedRegisters(
size_t& regTypesNum,
215 Flags& regFlags)
const = 0;
217 virtual void getMaxRegistersNum(
size_t& regTypesNum,
cxuint* maxRegs)
const = 0;
219 virtual void getRegisterRanges(
size_t& regTypesNum,
cxuint* regRanges)
const = 0;
221 virtual void fillAlignment(
size_t size,
cxbyte* output) = 0;
223 virtual bool parseRegisterRange(
const char*& linePtr,
cxuint& regStart,
228 virtual bool parseRegisterType(
const char*& linePtr,
229 const char* end,
cxuint& type) = 0;
231 virtual size_t getInstructionSize(
size_t codeSize,
const cxbyte* code)
const = 0;
245 friend struct GCNAsmUtils;
250 uint16_t curArchMask;
254 void resetInstrRVUs()
259 void setCurrentRVU(
cxbyte idx)
260 { currentRVUIndex = idx; }
263 { instrRVUs[currentRVUIndex] = rvu; }
268 if (rvu.regField != ASMFIELD_NONE)
279 void assemble(
const CString& mnemonic,
const char* mnemPlace,
const char* linePtr,
280 const char* lineEnd, std::vector<cxbyte>& output,
284 cxuint sectionId, uint64_t value);
285 bool checkMnemonic(
const CString& mnemonic)
const;
286 void setAllocatedRegisters(
const cxuint* regs,
Flags regFlags);
287 const cxuint* getAllocatedRegisters(
size_t& regTypesNum,
Flags& regFlags)
const;
288 void getMaxRegistersNum(
size_t& regTypesNum,
cxuint* maxRegs)
const;
289 void getRegisterRanges(
size_t& regTypesNum,
cxuint* regRanges)
const;
290 void fillAlignment(
size_t size,
cxbyte* output);
291 bool parseRegisterRange(
const char*& linePtr,
cxuint& regStart,
cxuint& regEnd,
294 bool parseRegisterType(
const char*& linePtr,
const char* end,
cxuint& type);
295 size_t getInstructionSize(
size_t codeSize,
const cxbyte* code)
const;
316 SSAInfo(
size_t _bssaId = SIZE_MAX,
size_t _ssaIdF = SIZE_MAX,
317 size_t _ssaId = SIZE_MAX,
size_t _ssaIdL = SIZE_MAX,
318 size_t _ssaIdChange = 0,
bool _readBeforeWrite =
false)
319 : ssaIdBefore(_bssaId), ssaIdFirst(_ssaIdF), ssaId(_ssaId),
320 ssaIdLast(_ssaIdL), ssaIdChange(_ssaIdChange),
321 readBeforeWrite(_readBeforeWrite)
332 std::unordered_map<AsmSingleVReg, SSAInfo> ssaInfoMap;
337 typedef std::pair<size_t, size_t> SSAReplace;
338 typedef std::unordered_map<AsmSingleVReg, std::vector<SSAReplace> > SSAReplacesMap;
341 typedef std::unordered_map<AsmSingleVReg, std::vector<size_t> > VarIndexMap;
345 std::vector<size_t> prevVidxes;
346 std::vector<size_t> nextVidxes;
350 std::vector<size_t> prevVidxes;
351 std::vector<size_t> nextVidxes;
355 std::vector<CodeBlock> codeBlocks;
356 SSAReplacesMap ssaReplacesMap;
359 VarIndexMap vregIndexMaps[MAX_REGTYPES_NUM];
360 InterGraph interGraphs[MAX_REGTYPES_NUM];
362 std::unordered_map<size_t, LinearDep> linearDepMaps[MAX_REGTYPES_NUM];
363 std::unordered_map<size_t, EqualToDep> equalToDepMaps[MAX_REGTYPES_NUM];
364 std::unordered_map<size_t, size_t> equalSetMaps[MAX_REGTYPES_NUM];
365 std::vector<std::vector<size_t> > equalSetLists[MAX_REGTYPES_NUM];
369 void createCodeStructure(
const std::vector<AsmCodeFlowEntry>& codeFlow,
370 size_t codeSize,
const cxbyte* code);
372 void applySSAReplaces();
374 void colorInterferenceGraph();
376 void allocateRegisters(
cxuint sectionId);
378 const std::vector<CodeBlock>& getCodeBlocks()
const 379 {
return codeBlocks; }
380 const SSAReplacesMap& getSSAReplacesMap()
const 381 {
return ssaReplacesMap; }
408 typedef std::pair<CString, uint64_t>
DefSym;
425 friend struct AsmParseUtils;
426 friend struct AsmPseudoOps;
427 friend struct AsmGalliumPseudoOps;
428 friend struct AsmAmdPseudoOps;
429 friend struct AsmAmdCL2PseudoOps;
430 friend struct AsmROCmPseudoOps;
431 friend struct GCNAsmUtils;
436 uint32_t driverVersion;
437 uint32_t llvmVersion;
439 bool newROCmBinFormat;
441 bool resolvingRelocs;
442 bool doNotRemoveFromSymbolClones;
444 std::vector<DefSym> defSyms;
445 std::vector<CString> includeDirs;
446 std::vector<AsmSection> sections;
447 std::vector<Array<cxuint> > relSpacesSections;
448 std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
449 std::unordered_set<AsmSymbolEntry*> symbolClones;
450 std::vector<AsmExpression*> unevalExpressions;
451 std::vector<AsmRelocation> relocations;
454 std::stack<AsmScope*> scopeStack;
455 std::vector<AsmScope*> abandonedScopes;
458 std::vector<AsmKernel> kernels;
471 bool lineAlreadyRead;
476 bool sectionDiffsPrepared;
479 std::stack<AsmInputFilter*> asmInputFilters;
482 std::ostream& messageStream;
483 std::ostream& printStream;
487 std::stack<AsmClause> clauses;
491 uint64_t& currentOutPos;
493 bool withSectionDiffs()
const 505 {
return getSourcePos(linePtr-line); }
507 void printWarning(
const AsmSourcePos& pos,
const char* message);
508 void printError(
const AsmSourcePos& pos,
const char* message);
510 void printWarning(
const char* linePtr,
const char* message)
511 { printWarning(getSourcePos(linePtr), message); }
512 void printError(
const char* linePtr,
const char* message)
513 { printError(getSourcePos(linePtr), message); }
515 void printWarning(
LineCol lineCol,
const char* message)
516 { printWarning(getSourcePos(lineCol), message); }
517 void printError(
LineCol lineCol,
const char* message)
518 { printError(getSourcePos(lineCol), message); }
520 LineCol translatePos(
const char* linePtr)
const 521 {
return currentInputFilter->
translatePos(linePtr-line); }
522 LineCol translatePos(
size_t pos)
const 525 bool parseLiteral(uint64_t& value,
const char*& linePtr);
526 bool parseLiteralNoError(uint64_t& value,
const char*& linePtr);
527 bool parseString(std::string& outString,
const char*& linePtr);
529 enum class ParseState
539 ParseState parseSymbol(
const char*& linePtr,
AsmSymbolEntry*& entry,
540 bool localLabel =
true,
bool dontCreateSymbol =
false);
541 bool skipSymbol(
const char*& linePtr);
545 bool assignSymbol(
const CString& symbolName,
const char* symbolPlace,
546 const char* linePtr,
bool reassign =
true,
bool baseExpr =
false);
548 bool assignOutputCounter(
const char* symbolPlace, uint64_t value,
cxuint sectionId,
551 void parsePseudoOps(
const CString& firstName,
const char* stmtPlace,
552 const char* linePtr);
555 bool skipClauses(
bool exitm =
false);
557 bool putRepetitionContent(
AsmRepeat& repeat);
559 void initializeOutputFormat();
561 bool pushClause(
const char*
string,
AsmClauseType clauseType)
564 return pushClause(
string, clauseType,
true, included);
566 bool pushClause(
const char*
string,
AsmClauseType clauseType,
567 bool satisfied,
bool& included);
569 bool popClause(
const char*
string,
AsmClauseType clauseType);
573 std::unordered_set<AsmScope*>& scopeSet);
575 AsmScope* getRecurScope(
const CString& scopePlace,
bool ignoreLast =
false,
576 const char** lastStep =
nullptr);
580 std::unordered_set<AsmScope*>& scopeSet);
583 CString& sameSymName,
bool insertMode =
false);
585 std::pair<AsmSymbolEntry*, bool> insertSymbolInScope(
const CString& symName,
590 std::unordered_set<AsmScope*>& scopeSet);
593 CString& sameRvName,
bool insertMode =
false);
595 std::pair<AsmRegVarEntry*, bool> insertRegVarInScope(
const CString& rvName,
601 bool pushScope(
const CString& scopeName);
605 bool includeFile(
const char* pseudoOpPlace,
const std::string& filename);
607 ParseState makeMacroSubstitution(
const char*
string);
609 bool parseMacroArgValue(
const char*& linePtr, std::string& outStr);
611 void putData(
size_t size,
const cxbyte* data)
613 AsmSection& section = sections[currentSection];
615 currentOutPos += size;
620 void goToMain(
const char* pseudoOpPlace);
621 void goToKernel(
const char* pseudoOpPlace,
const char* kernelName);
622 void goToSection(
const char* pseudoOpPlace,
const char* sectionName, uint64_t align=0);
623 void goToSection(
const char* pseudoOpPlace,
const char* sectionName,
625 void goToSection(
const char* pseudoOpPlace,
cxuint sectionId, uint64_t align=0);
628 cxbyte signess = WS_BOTH);
630 bool isAddressableSection()
const 633 (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
635 bool isWriteableSection()
const 638 (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
640 bool isResolvableSection()
const 645 bool isResolvableSection(
cxuint sectionId)
const 652 void handleRegionsOnKernels(
const std::vector<cxuint>& newKernels,
653 const std::vector<cxuint>& oldKernels,
cxuint codeSection);
656 void tryToResolveSymbols(
AsmScope* scope);
657 void printUnresolvedSymbols(
AsmScope* scope);
682 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
696 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
704 void writeBinary(
const char* filename)
const;
706 void writeBinary(std::ostream& outStream)
const;
712 {
return driverVersion; }
715 { this->driverVersion = driverVersion; }
719 {
return llvmVersion; }
722 { this->llvmVersion = llvmVersion; }
726 {
return deviceType; }
729 { this->deviceType = deviceType; }
735 { format = binFormat; }
741 { _64bit = this64Bit; }
744 {
return newROCmBinFormat; }
747 { newROCmBinFormat = newFmt; }
753 { this->flags = flags; }
756 {
return alternateMacro; }
759 {
return macroCase; }
762 {
return oldModParam; }
765 {
return buggyFPLit; }
768 {
return includeDirs; }
770 void addIncludeDir(
const CString& includeDir);
778 const std::vector<Array<cxuint> >& getRelSpacesSections()
const 779 {
return relSpacesSections; }
782 {
return kernelMap; }
791 {
return insertRegVarInScope(name, var).second; }
797 {
return globalScope; }
800 bool isAbsoluteSymbol(
const AsmSymbol& symbol)
const;
803 void addInitialDefSym(
const CString& symName, uint64_t value);
807 {
return formatHandler; }
811 { assembler.printWarning(linePtr, message); }
814 { assembler.printError(linePtr, message); }
818 { assembler.printWarningForRange(bits, value, pos, signess); }
821 { assembler.printWarning(sourcePos, message); }
824 { assembler.printError(sourcePos, message); }
827 { assembler.sections[sectionId].addCodeFlowEntry(entry); }
void pushUseRegUsage(const AsmRegVarUsage &rvu)
push regvar or register from usereg pseudo-op
AsmRepeatInputFilter or AsmIRPInputFilter.
bool useRegMode
true if in usereg mode
Definition: Assembler.h:96
bool is64Bit() const
get bitness (true if 64-bit)
Definition: Assembler.h:737
void set64Bit(bool this64Bit)
set bitness (true if 64-bit)
Definition: Assembler.h:740
AsmRegVarMap regVarMap
regvar map
Definition: AsmDefs.h:674
common definitions for assembler and disassembler
Definition: Assembler.h:301
main class of assembler
Definition: Assembler.h:404
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: AsmDefs.h:301
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
size_t readOffset
read offset
Definition: Assembler.h:86
Flags getFlags() const
get flags
Definition: Assembler.h:749
std::unordered_map< CString, AsmRegVar > AsmRegVarMap
regvar map
Definition: AsmDefs.h:590
assembler repeat
Definition: AsmSource.h:239
virtual ~ISAUsageHandler()
destructor
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:410
size_t ssaIdChange
number of SSA id changes
Definition: Assembler.h:312
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:725
AsmRegVarMap::value_type AsmRegVarEntry
regvar entry
Definition: AsmDefs.h:592
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:775
ColNo colNo
column number, for macro substitution and IRP points to column preprocessed line
Definition: AsmSource.h:50
force add symbols to binary
Definition: Assembler.h:51
void setDriverVersion(uint32_t driverVersion)
set AMD driver version
Definition: Assembler.h:714
reference pointer based on Glibmm refptr
Definition: Utilities.h:786
AsmClauseType
type of clause
Definition: Assembler.h:385
Assembler & assembler
assembler
Definition: Assembler.h:178
const AsmRegVarMap & getRegVarMap() const
get regvar map
Definition: Assembler.h:787
ReadPos getReadPos() const
get reading position
Definition: Assembler.h:123
assembler section
Definition: AsmDefs.h:711
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:408
bool isBuggyFPLit() const
get true if buggyFPLit enabled
Definition: Assembler.h:764
size_t regUsagesPos
position in reg usage
Definition: Assembler.h:88
handles raw code format
Definition: AsmFormats.h:208
assembler scope for symbol, macros, regvars
Definition: AsmDefs.h:670
bool isNewROCmBinFormat() const
is new ROCm binary format
Definition: Assembler.h:743
Regvar info structure.
Definition: AsmDefs.h:569
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:813
AsmRegVarUsage nextUsage()
get next usage
ISAUsageHandler(const std::vector< cxbyte > &content)
constructor
size_t lastOffset
last offset
Definition: Assembler.h:85
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:810
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:240
virtual ISAUsageHandler * copy() const =0
copy this usage handler
an array class
Definition: Containers.h:38
size_t regUsagesPos
position in reg usage
Definition: Assembler.h:73
std::vector< NextBlock > nexts
nexts blocks, if empty then direct next block
Definition: Assembler.h:327
Flags regFlags
define what extra register must be included
Definition: Assembler.h:242
disable case-insensitive naming (default)
Definition: Assembler.h:55
void pushUsage(const AsmRegVarUsage &rvu)
push regvar or register usage
void rewind()
rewind to start for reading
handles ROCM binary format
Definition: AsmFormats.h:497
size_t ssaId
original SSA id
Definition: Assembler.h:310
all flags
Definition: Assembler.h:58
size_t regVarUsagesPos
position in regvar usage
Definition: Assembler.h:90
line and column
Definition: AsmSource.h:45
size_t instrStructPos
position in instr struct
Definition: Assembler.h:87
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: AsmDefs.h:221
void setFlags(Flags flags)
set flags
Definition: Assembler.h:752
absolute section id
Definition: AsmFormats.h:85
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:398
uint16_t pushedArgs
pushed args
Definition: Assembler.h:91
void setReadPos(const ReadPos rpos)
set reading position
Definition: Assembler.h:129
size_t regVarUsagesPos
position in regVarUsage
Definition: Assembler.h:75
if failed now, no later trial
AsmSectionType
assembler section type
Definition: AsmFormats.h:47
unsigned char cxbyte
unsigned byte
Definition: Config.h:215
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:767
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:241
std::unordered_map< CString, RefPtr< const AsmMacro > > AsmMacroMap
assembler macro map
Definition: AsmDefs.h:662
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:400
size_t ssaIdLast
last SSA id in last
Definition: Assembler.h:311
main namespace
Definition: AsmDefs.h:38
bool hasNext() const
has next regvar usage
Definition: Assembler.h:117
void setNewROCmBinFormat(bool newFmt)
set new ROCm binary format
Definition: Assembler.h:746
cxbyte argPos
argument position
Definition: Assembler.h:92
bool isAltMacro() const
get true if altMacro enabled
Definition: Assembler.h:755
bool isMacroCase() const
get true if macroCase enabled
Definition: Assembler.h:758
AsmClauseType type
type of clause
Definition: Assembler.h:397
only for running tests
Definition: Assembler.h:57
Definition: Assembler.h:298
unsigned int cxuint
unsigned int
Definition: Config.h:223
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:781
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:731
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:772
std::vector< cxbyte > instrStruct
structure of register usage
Definition: Assembler.h:80
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: AsmDefs.h:59
std::vector< cxbyte > content
content of section
Definition: AsmDefs.h:721
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: AsmDefs.h:223
virtual cxbyte getRwFlags(AsmRegField regField, uint16_t rstart, uint16_t rend) const =0
get RW flags (used by assembler)
Definition: Assembler.h:342
GCN (register and regvar) Usage handler.
Definition: Assembler.h:155
cxbyte argFlags
???
Definition: Assembler.h:93
GPUDeviceType
type of GPU device
Definition: GPUId.h:51
bool useRegMode
true if in usereg mode
Definition: Assembler.h:77
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:728
utilities for other libraries and programs
bool isNext
is next
Definition: Assembler.h:95
bool readBeforeWrite
have read before write
Definition: Assembler.h:315
GCN arch assembler.
Definition: Assembler.h:235
enable resolving symbols if ASM_TESTRUN enabled
Definition: Assembler.h:56
size_t ssaIdBefore
SSA id before first SSA in block.
Definition: Assembler.h:308
AsmSymbolMap symbolMap
symbol map
Definition: AsmDefs.h:673
register pool numbers
Definition: Assembler.h:239
size_t regUsages2Pos
position in regUsage2
Definition: Assembler.h:74
uint32_t getLLVMVersion() const
get LLVM version
Definition: Assembler.h:718
assembler's clause (if,else,macro,rept)
Definition: Assembler.h:395
regvar usage in code
Definition: AsmDefs.h:604
Definition: Assembler.h:306
const std::vector< cxbyte > & content
code content
Definition: Assembler.h:84
cxbyte defaultInstrSize
default instruction size
Definition: Assembler.h:94
void putSpace(size_t offset)
put space to offset
buggy handling of fpliterals (including fp constants)
Definition: Assembler.h:53
LineNo lineNo
line number
Definition: AsmSource.h:47
bool addRegVar(const CString &name, const AsmRegVar &var)
add regvar
Definition: Assembler.h:790
stgructure that hold read position to store later
Definition: Assembler.h:69
code flow entry
Definition: AsmDefs.h:654
void flush()
flush last pending register usages
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:711
size_t instrStructPos
position instrStructs
Definition: Assembler.h:72
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:806
std::vector< AsmRegUsage2Int > regUsages2
register usage (by .usereg)
Definition: Assembler.h:82
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:816
handles GalliumCompute format
Definition: AsmFormats.h:412
const AsmScope & getGlobalScope() const
get global scope
Definition: Assembler.h:796
an assembler sources handling
Definition: Assembler.h:348
size_t readOffset
read offset
Definition: Assembler.h:71
void setLLVMVersion(uint32_t llvmVersion)
set LLVM version
Definition: Assembler.h:721
uint16_t pushedArgs
pushed argds number
Definition: Assembler.h:76
assembler symbol structure
Definition: AsmDefs.h:158
virtual void getUsageDependencies(cxuint rvusNum, const AsmRegVarUsage *rvus, cxbyte *linearDeps, cxbyte *equalToDeps) const =0
get usage dependencies around single instruction
bool isOldModParam() const
get true if oldModParam enabled (old modifier parametrization)
Definition: Assembler.h:761
cxbyte AsmRegField
type of register field
Definition: AsmDefs.h:515
enable all warnings for assembler
Definition: Assembler.h:50
ISA (register and regvar) Usage handler.
Definition: Assembler.h:65
handles AMD Catalyst format
Definition: AsmFormats.h:234
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:323
Definition: Assembler.h:324
virtual std::pair< uint16_t, uint16_t > getRegPair(AsmRegField regField, cxbyte rwFlags) const =0
get reg pair (used by assembler)
AsmRegField regField
place in instruction
Definition: AsmDefs.h:610
ISA assembler class.
Definition: Assembler.h:175
enable altmacro mode
Definition: Assembler.h:52
section is unresolvable
Definition: AsmFormats.h:96
simple C-string container
Definition: CString.h:38
an assembler for Radeon GPU's
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:734
std::vector< AsmRegVarUsageInt > regVarUsages
regvar usage
Definition: Assembler.h:83
size_t regUsages2Pos
position in reg usage 2
Definition: Assembler.h:89
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:399
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:784
assembler source position
Definition: AsmSource.h:153
std::vector< AsmRegUsageInt > regUsages
register usage
Definition: Assembler.h:81