23 #ifndef __CLRX_ASSEMBLER_H__ 24 #define __CLRX_ASSEMBLER_H__ 26 #include <CLRX/Config.h> 36 #include <unordered_set> 37 #include <unordered_map> 51 ASM_FORCE_ADD_SYMBOLS = 2,
95 void skipBytesInInstrStruct();
142 uint16_t rend)
const = 0;
145 cxbyte rwFlags)
const = 0;
148 cxbyte* linearDeps, cxbyte* equalToDeps)
const = 0;
168 cxbyte* linearDeps, cxbyte* equalToDeps)
const;
178 void printWarning(
const char* linePtr,
const char* message);
180 void printError(
const char* linePtr,
const char* message);
182 void printWarning(
const AsmSourcePos& sourcePos,
const char* message);
184 void printError(
const AsmSourcePos& sourcePos,
const char* message);
186 void printWarningForRange(cxuint bits, uint64_t value,
const AsmSourcePos& pos,
187 cxbyte signess = WS_BOTH);
198 virtual void assemble(
const CString& mnemonic,
const char* mnemPlace,
199 const char* linePtr,
const char* lineEnd, std::vector<cxbyte>& output,
202 virtual bool resolveCode(
const AsmSourcePos& sourcePos, cxuint targetSectionId,
204 cxuint sectionId, uint64_t value) = 0;
206 virtual bool checkMnemonic(
const CString& mnemonic)
const = 0;
208 virtual void setAllocatedRegisters(
const cxuint* regs =
nullptr,
209 Flags regFlags = 0) = 0;
211 virtual const cxuint* getAllocatedRegisters(
size_t& regTypesNum,
212 Flags& regFlags)
const = 0;
214 virtual void getMaxRegistersNum(
size_t& regTypesNum, cxuint* maxRegs)
const = 0;
216 virtual void getRegisterRanges(
size_t& regTypesNum, cxuint* regRanges)
const = 0;
218 virtual void fillAlignment(
size_t size, cxbyte* output) = 0;
220 virtual bool parseRegisterRange(
const char*& linePtr, cxuint& regStart,
221 cxuint& regEnd,
const AsmRegVar*& regVar) = 0;
225 virtual bool parseRegisterType(
const char*& linePtr,
226 const char* end, cxuint& type) = 0;
228 virtual size_t getInstructionSize(
size_t codeSize,
const cxbyte* code)
const = 0;
242 friend struct GCNAsmUtils;
247 uint16_t curArchMask;
248 cxbyte currentRVUIndex;
251 void resetInstrRVUs()
256 void setCurrentRVU(cxbyte idx)
257 { currentRVUIndex = idx; }
260 { instrRVUs[currentRVUIndex] = rvu; }
265 if (rvu.regField != ASMFIELD_NONE)
276 void assemble(
const CString& mnemonic,
const char* mnemPlace,
const char* linePtr,
277 const char* lineEnd, std::vector<cxbyte>& output,
279 bool resolveCode(
const AsmSourcePos& sourcePos, cxuint targetSectionId,
281 cxuint sectionId, uint64_t value);
282 bool checkMnemonic(
const CString& mnemonic)
const;
283 void setAllocatedRegisters(
const cxuint* regs,
Flags regFlags);
284 const cxuint* getAllocatedRegisters(
size_t& regTypesNum,
Flags& regFlags)
const;
285 void getMaxRegistersNum(
size_t& regTypesNum, cxuint* maxRegs)
const;
286 void getRegisterRanges(
size_t& regTypesNum, cxuint* regRanges)
const;
287 void fillAlignment(
size_t size, cxbyte* output);
288 bool parseRegisterRange(
const char*& linePtr, cxuint& regStart, cxuint& regEnd,
291 bool parseRegisterType(
const char*& linePtr,
const char* end, cxuint& type);
292 size_t getInstructionSize(
size_t codeSize,
const cxbyte* code)
const;
313 SSAInfo(
size_t _bssaId = SIZE_MAX,
size_t _ssaIdF = SIZE_MAX,
314 size_t _ssaId = SIZE_MAX,
size_t _ssaIdL = SIZE_MAX,
315 size_t _ssaIdChange = 0,
bool _readBeforeWrite =
false)
316 : ssaIdBefore(_bssaId), ssaIdFirst(_ssaIdF), ssaId(_ssaId),
317 ssaIdLast(_ssaIdL), ssaIdChange(_ssaIdChange),
318 readBeforeWrite(_readBeforeWrite)
329 std::unordered_map<AsmSingleVReg, SSAInfo> ssaInfoMap;
334 typedef std::pair<size_t, size_t> SSAReplace;
335 typedef std::unordered_map<AsmSingleVReg, std::vector<SSAReplace> > SSAReplacesMap;
338 typedef std::unordered_map<AsmSingleVReg, std::vector<size_t> > VarIndexMap;
342 std::vector<size_t> prevVidxes;
343 std::vector<size_t> nextVidxes;
347 std::vector<size_t> prevVidxes;
348 std::vector<size_t> nextVidxes;
352 std::vector<CodeBlock> codeBlocks;
353 SSAReplacesMap ssaReplacesMap;
356 VarIndexMap vregIndexMaps[MAX_REGTYPES_NUM];
357 InterGraph interGraphs[MAX_REGTYPES_NUM];
359 std::unordered_map<size_t, LinearDep> linearDepMaps[MAX_REGTYPES_NUM];
360 std::unordered_map<size_t, EqualToDep> equalToDepMaps[MAX_REGTYPES_NUM];
361 std::unordered_map<size_t, size_t> equalSetMaps[MAX_REGTYPES_NUM];
362 std::vector<std::vector<size_t> > equalSetLists[MAX_REGTYPES_NUM];
366 void createCodeStructure(
const std::vector<AsmCodeFlowEntry>& codeFlow,
367 size_t codeSize,
const cxbyte* code);
369 void applySSAReplaces();
371 void colorInterferenceGraph();
373 void allocateRegisters(cxuint sectionId);
375 const std::vector<CodeBlock>& getCodeBlocks()
const 376 {
return codeBlocks; }
377 const SSAReplacesMap& getSSAReplacesMap()
const 378 {
return ssaReplacesMap; }
405 typedef std::pair<CString, uint64_t>
DefSym;
421 friend struct AsmParseUtils;
422 friend struct AsmPseudoOps;
423 friend struct AsmGalliumPseudoOps;
424 friend struct AsmAmdPseudoOps;
425 friend struct AsmAmdCL2PseudoOps;
426 friend struct AsmROCmPseudoOps;
427 friend struct GCNAsmUtils;
432 uint32_t driverVersion;
433 uint32_t llvmVersion;
436 bool resolvingRelocs;
438 std::vector<DefSym> defSyms;
439 std::vector<CString> includeDirs;
440 std::vector<AsmSection> sections;
441 std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
442 std::vector<AsmRelocation> relocations;
445 std::stack<AsmScope*> scopeStack;
446 std::vector<AsmScope*> abandonedScopes;
449 std::vector<AsmKernel> kernels;
458 cxuint inclusionLevel;
459 cxuint macroSubstLevel;
460 cxuint repetitionLevel;
461 bool lineAlreadyRead;
467 cxuint filenameIndex;
468 std::stack<AsmInputFilter*> asmInputFilters;
471 std::ostream& messageStream;
472 std::ostream& printStream;
476 std::stack<AsmClause> clauses;
478 cxuint currentKernel;
479 cxuint& currentSection;
480 uint64_t& currentOutPos;
491 {
return getSourcePos(linePtr-line); }
493 void printWarning(
const AsmSourcePos& pos,
const char* message);
494 void printError(
const AsmSourcePos& pos,
const char* message);
496 void printWarning(
const char* linePtr,
const char* message)
497 { printWarning(getSourcePos(linePtr), message); }
498 void printError(
const char* linePtr,
const char* message)
499 { printError(getSourcePos(linePtr), message); }
501 void printWarning(
LineCol lineCol,
const char* message)
502 { printWarning(getSourcePos(lineCol), message); }
503 void printError(
LineCol lineCol,
const char* message)
504 { printError(getSourcePos(lineCol), message); }
506 LineCol translatePos(
const char* linePtr)
const 507 {
return currentInputFilter->
translatePos(linePtr-line); }
508 LineCol translatePos(
size_t pos)
const 511 bool parseLiteral(uint64_t& value,
const char*& linePtr);
512 bool parseString(std::string& outString,
const char*& linePtr);
514 enum class ParseState
524 ParseState parseSymbol(
const char*& linePtr,
AsmSymbolEntry*& entry,
525 bool localLabel =
true,
bool dontCreateSymbol =
false);
526 bool skipSymbol(
const char*& linePtr);
528 bool setSymbol(
AsmSymbolEntry& symEntry, uint64_t value, cxuint sectionId);
530 bool assignSymbol(
const CString& symbolName,
const char* symbolPlace,
531 const char* linePtr,
bool reassign =
true,
bool baseExpr =
false);
533 bool assignOutputCounter(
const char* symbolPlace, uint64_t value, cxuint sectionId,
534 cxbyte fillValue = 0);
536 void parsePseudoOps(
const CString& firstName,
const char* stmtPlace,
537 const char* linePtr);
540 bool skipClauses(
bool exitm =
false);
542 bool putRepetitionContent(
AsmRepeat& repeat);
544 void initializeOutputFormat();
546 bool pushClause(
const char*
string,
AsmClauseType clauseType)
549 return pushClause(
string, clauseType,
true, included);
551 bool pushClause(
const char*
string,
AsmClauseType clauseType,
552 bool satisfied,
bool& included);
554 bool popClause(
const char*
string,
AsmClauseType clauseType);
558 std::unordered_set<AsmScope*>& scopeSet);
560 AsmScope* getRecurScope(
const CString& scopePlace,
bool ignoreLast =
false,
561 const char** lastStep =
nullptr);
565 std::unordered_set<AsmScope*>& scopeSet);
568 CString& sameSymName,
bool insertMode =
false);
570 std::pair<AsmSymbolEntry*, bool> insertSymbolInScope(
const CString& symName,
575 std::unordered_set<AsmScope*>& scopeSet);
578 CString& sameRvName,
bool insertMode =
false);
580 std::pair<AsmRegVarEntry*, bool> insertRegVarInScope(
const CString& rvName,
586 bool pushScope(
const CString& scopeName);
590 bool includeFile(
const char* pseudoOpPlace,
const std::string& filename);
592 ParseState makeMacroSubstitution(
const char*
string);
594 bool parseMacroArgValue(
const char*& linePtr, std::string& outStr);
596 void putData(
size_t size,
const cxbyte* data)
598 AsmSection& section = sections[currentSection];
600 currentOutPos += size;
603 cxbyte* reserveData(
size_t size, cxbyte fillValue = 0);
605 void goToMain(
const char* pseudoOpPlace);
606 void goToKernel(
const char* pseudoOpPlace,
const char* kernelName);
607 void goToSection(
const char* pseudoOpPlace,
const char* sectionName, uint64_t align=0);
608 void goToSection(
const char* pseudoOpPlace,
const char* sectionName,
610 void goToSection(
const char* pseudoOpPlace, cxuint sectionId, uint64_t align=0);
612 void printWarningForRange(cxuint bits, uint64_t value,
const AsmSourcePos& pos,
613 cxbyte signess = WS_BOTH);
615 bool isAddressableSection()
const 618 (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
620 bool isWriteableSection()
const 623 (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
625 bool isResolvableSection()
const 630 bool isResolvableSection(cxuint sectionId)
const 637 void handleRegionsOnKernels(
const std::vector<cxuint>& newKernels,
638 const std::vector<cxuint>& oldKernels, cxuint codeSection);
640 void tryToResolveSymbols(
AsmScope* scope);
641 void printUnresolvedSymbols(
AsmScope* scope);
660 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
674 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
682 void writeBinary(
const char* filename)
const;
684 void writeBinary(std::ostream& outStream)
const;
690 {
return driverVersion; }
693 { this->driverVersion = driverVersion; }
697 {
return llvmVersion; }
700 { this->llvmVersion = llvmVersion; }
704 {
return deviceType; }
707 { this->deviceType = deviceType; }
713 { format = binFormat; }
719 { _64bit = this64Bit; }
725 { this->flags = flags; }
728 {
return includeDirs; }
730 void addIncludeDir(
const CString& includeDir);
739 {
return kernelMap; }
748 {
return insertRegVarInScope(name, var).second; }
754 {
return globalScope; }
757 bool isAbsoluteSymbol(
const AsmSymbol& symbol)
const;
760 void addInitialDefSym(
const CString& symName, uint64_t value);
764 {
return formatHandler; }
768 { assembler.printWarning(linePtr, message); }
771 { assembler.printError(linePtr, message); }
775 { assembler.printWarningForRange(bits, value, pos, signess); }
778 { assembler.printWarning(sourcePos, message); }
781 { assembler.printError(sourcePos, message); }
783 inline void ISAAssembler::addCodeFlowEntry(cxuint sectionId,
const AsmCodeFlowEntry& entry)
784 { 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:93
bool is64Bit() const
get bitness
Definition: Assembler.h:715
void set64Bit(bool this64Bit)
set bitness
Definition: Assembler.h:718
AsmRegVarMap regVarMap
regvar map
Definition: AsmDefs.h:603
common definitions for assembler and disassembler
Definition: Assembler.h:298
main class of assembler
Definition: Assembler.h:401
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: AsmDefs.h:277
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
size_t readOffset
read offset
Definition: Assembler.h:83
Flags getFlags() const
get flags
Definition: Assembler.h:721
std::unordered_map< CString, AsmRegVar > AsmRegVarMap
regvar map
Definition: AsmDefs.h:519
assembler repeat
Definition: AsmSource.h:238
virtual ~ISAUsageHandler()
destructor
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:407
size_t ssaIdChange
number of SSA id changes
Definition: Assembler.h:309
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:703
AsmRegVarMap::value_type AsmRegVarEntry
regvar entry
Definition: AsmDefs.h:521
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:735
ColNo colNo
column number, for macro substitution and IRP points to column preprocessed line
Definition: AsmSource.h:49
void setDriverVersion(uint32_t driverVersion)
set AMD driver version
Definition: Assembler.h:692
reference pointer based on Glibmm refptr
Definition: Utilities.h:774
AsmClauseType
type of clause
Definition: Assembler.h:382
Assembler & assembler
assembler
Definition: Assembler.h:175
const AsmRegVarMap & getRegVarMap() const
get regvar map
Definition: Assembler.h:744
ReadPos getReadPos() const
get reading position
Definition: Assembler.h:120
assembler section
Definition: AsmDefs.h:640
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:405
size_t regUsagesPos
position in reg usage
Definition: Assembler.h:85
handles raw code format
Definition: AsmFormats.h:181
assembler scope for symbol, macros, regvars
Definition: AsmDefs.h:599
Regvar info structure.
Definition: AsmDefs.h:498
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:770
AsmRegVarUsage nextUsage()
get next usage
ISAUsageHandler(const std::vector< cxbyte > &content)
constructor
size_t lastOffset
last offset
Definition: Assembler.h:82
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:767
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:237
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:70
std::vector< NextBlock > nexts
nexts blocks, if empty then direct next block
Definition: Assembler.h:324
Flags regFlags
define what extra register must be included
Definition: Assembler.h:239
void pushUsage(const AsmRegVarUsage &rvu)
push regvar or register usage
void rewind()
rewind to start for reading
handles ROCM binary format
Definition: AsmFormats.h:439
size_t ssaId
original SSA id
Definition: Assembler.h:307
all flags
Definition: Assembler.h:56
size_t regVarUsagesPos
position in regvar usage
Definition: Assembler.h:87
line and column
Definition: AsmSource.h:44
size_t instrStructPos
position in instr struct
Definition: Assembler.h:84
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: AsmDefs.h:205
void setFlags(Flags flags)
set flags
Definition: Assembler.h:724
absolute section id
Definition: AsmFormats.h:79
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:395
uint16_t pushedArgs
pushed args
Definition: Assembler.h:88
void setReadPos(const ReadPos rpos)
set reading position
Definition: Assembler.h:126
size_t regVarUsagesPos
position in regVarUsage
Definition: Assembler.h:72
AsmSectionType
assembler section type
Definition: AsmFormats.h:46
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:727
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:238
std::unordered_map< CString, RefPtr< const AsmMacro > > AsmMacroMap
assembler macro map
Definition: AsmDefs.h:591
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:397
size_t ssaIdLast
last SSA id in last
Definition: Assembler.h:308
main namespace
Definition: AsmDefs.h:38
bool hasNext() const
has next regvar usage
Definition: Assembler.h:114
cxbyte argPos
argument position
Definition: Assembler.h:89
AsmClauseType type
type of clause
Definition: Assembler.h:394
only for running tests
Definition: Assembler.h:55
Definition: Assembler.h:295
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:738
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:709
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:732
std::vector< cxbyte > instrStruct
structure of register usage
Definition: Assembler.h:77
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: AsmDefs.h:47
std::vector< cxbyte > content
content of section
Definition: AsmDefs.h:648
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: AsmDefs.h:207
virtual cxbyte getRwFlags(AsmRegField regField, uint16_t rstart, uint16_t rend) const =0
get RW flags (used by assembler)
Definition: Assembler.h:339
GCN (register and regvar) Usage handler.
Definition: Assembler.h:152
cxbyte argFlags
???
Definition: Assembler.h:90
GPUDeviceType
type of GPU device
Definition: GPUId.h:38
bool useRegMode
true if in usereg mode
Definition: Assembler.h:74
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:706
utilities for other libraries and programs
bool isNext
is next
Definition: Assembler.h:92
bool readBeforeWrite
have read before write
Definition: Assembler.h:312
GCN arch assembler.
Definition: Assembler.h:232
size_t ssaIdBefore
SSA id before first SSA in block.
Definition: Assembler.h:305
AsmSymbolMap symbolMap
symbol map
Definition: AsmDefs.h:602
register pool numbers
Definition: Assembler.h:236
size_t regUsages2Pos
position in regUsage2
Definition: Assembler.h:71
uint32_t getLLVMVersion() const
get LLVM version
Definition: Assembler.h:696
assembler's clause (if,else,macro,rept)
Definition: Assembler.h:392
regvar usage in code
Definition: AsmDefs.h:533
Definition: Assembler.h:303
const std::vector< cxbyte > & content
code content
Definition: Assembler.h:81
cxbyte defaultInstrSize
default instruction size
Definition: Assembler.h:91
void putSpace(size_t offset)
put space to offset
LineNo lineNo
line number
Definition: AsmSource.h:46
bool addRegVar(const CString &name, const AsmRegVar &var)
add regvar
Definition: Assembler.h:747
stgructure that hold read position to store later
Definition: Assembler.h:66
code flow entry
Definition: AsmDefs.h:583
void flush()
flush last pending register usages
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:689
size_t instrStructPos
position instrStructs
Definition: Assembler.h:69
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:763
std::vector< AsmRegUsage2Int > regUsages2
register usage (by .usereg)
Definition: Assembler.h:79
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:773
handles GalliumCompute format
Definition: AsmFormats.h:355
const AsmScope & getGlobalScope() const
get global scope
Definition: Assembler.h:753
an assembler sources handling
Definition: Assembler.h:345
size_t readOffset
read offset
Definition: Assembler.h:68
void setLLVMVersion(uint32_t llvmVersion)
set LLVM version
Definition: Assembler.h:699
uint16_t pushedArgs
pused argds number
Definition: Assembler.h:73
assembler symbol structure
Definition: AsmDefs.h:147
virtual void getUsageDependencies(cxuint rvusNum, const AsmRegVarUsage *rvus, cxbyte *linearDeps, cxbyte *equalToDeps) const =0
get usage dependencies around single instruction
cxbyte AsmRegField
type of register field
Definition: AsmDefs.h:445
enable all warnings for assembler
Definition: Assembler.h:50
ISA (register and regvar) Usage handler.
Definition: Assembler.h:62
handles AMD Catalyst format
Definition: AsmFormats.h:207
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:273
Definition: Assembler.h:321
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:539
ISA assembler class.
Definition: Assembler.h:172
section is unresolvable
Definition: AsmFormats.h:90
simple C-string container
Definition: CString.h:38
an assembler for Radeon GPU's
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:712
std::vector< AsmRegVarUsageInt > regVarUsages
regvar usage
Definition: Assembler.h:80
size_t regUsages2Pos
position in reg usage 2
Definition: Assembler.h:86
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:396
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:741
assembler source position
Definition: AsmSource.h:152
std::vector< AsmRegUsageInt > regUsages
register usage
Definition: Assembler.h:78