23 #ifndef __CLRX_ASSEMBLER_H__ 24 #define __CLRX_ASSEMBLER_H__ 36 #include <unordered_set> 37 #include <unordered_map> 51 ASM_FORCE_ADD_SYMBOLS = 2,
97 void skipBytesInInstrStruct();
144 uint16_t rend)
const = 0;
147 cxbyte rwFlags)
const = 0;
180 void printWarning(
const char* linePtr,
const char* message);
182 void printError(
const char* linePtr,
const char* message);
184 void printWarning(
const AsmSourcePos& sourcePos,
const char* message);
186 void printError(
const AsmSourcePos& sourcePos,
const char* message);
189 cxbyte signess = WS_BOTH);
200 virtual void assemble(
const CString& mnemonic,
const char* mnemPlace,
201 const char* linePtr,
const char* lineEnd, std::vector<cxbyte>& output,
206 cxuint sectionId, uint64_t value) = 0;
208 virtual bool checkMnemonic(
const CString& mnemonic)
const = 0;
210 virtual void setAllocatedRegisters(
const cxuint* regs =
nullptr,
211 Flags regFlags = 0) = 0;
213 virtual const cxuint* getAllocatedRegisters(
size_t& regTypesNum,
214 Flags& regFlags)
const = 0;
216 virtual void getMaxRegistersNum(
size_t& regTypesNum,
cxuint* maxRegs)
const = 0;
218 virtual void getRegisterRanges(
size_t& regTypesNum,
cxuint* regRanges)
const = 0;
220 virtual void fillAlignment(
size_t size,
cxbyte* output) = 0;
222 virtual bool parseRegisterRange(
const char*& linePtr,
cxuint& regStart,
227 virtual bool parseRegisterType(
const char*& linePtr,
228 const char* end,
cxuint& type) = 0;
230 virtual size_t getInstructionSize(
size_t codeSize,
const cxbyte* code)
const = 0;
244 friend struct GCNAsmUtils;
249 uint16_t curArchMask;
253 void resetInstrRVUs()
258 void setCurrentRVU(
cxbyte idx)
259 { currentRVUIndex = idx; }
262 { instrRVUs[currentRVUIndex] = rvu; }
267 if (rvu.regField != ASMFIELD_NONE)
278 void assemble(
const CString& mnemonic,
const char* mnemPlace,
const char* linePtr,
279 const char* lineEnd, std::vector<cxbyte>& output,
283 cxuint sectionId, uint64_t value);
284 bool checkMnemonic(
const CString& mnemonic)
const;
285 void setAllocatedRegisters(
const cxuint* regs,
Flags regFlags);
286 const cxuint* getAllocatedRegisters(
size_t& regTypesNum,
Flags& regFlags)
const;
287 void getMaxRegistersNum(
size_t& regTypesNum,
cxuint* maxRegs)
const;
288 void getRegisterRanges(
size_t& regTypesNum,
cxuint* regRanges)
const;
289 void fillAlignment(
size_t size,
cxbyte* output);
290 bool parseRegisterRange(
const char*& linePtr,
cxuint& regStart,
cxuint& regEnd,
293 bool parseRegisterType(
const char*& linePtr,
const char* end,
cxuint& type);
294 size_t getInstructionSize(
size_t codeSize,
const cxbyte* code)
const;
315 SSAInfo(
size_t _bssaId = SIZE_MAX,
size_t _ssaIdF = SIZE_MAX,
316 size_t _ssaId = SIZE_MAX,
size_t _ssaIdL = SIZE_MAX,
317 size_t _ssaIdChange = 0,
bool _readBeforeWrite =
false)
318 : ssaIdBefore(_bssaId), ssaIdFirst(_ssaIdF), ssaId(_ssaId),
319 ssaIdLast(_ssaIdL), ssaIdChange(_ssaIdChange),
320 readBeforeWrite(_readBeforeWrite)
331 std::unordered_map<AsmSingleVReg, SSAInfo> ssaInfoMap;
336 typedef std::pair<size_t, size_t> SSAReplace;
337 typedef std::unordered_map<AsmSingleVReg, std::vector<SSAReplace> > SSAReplacesMap;
340 typedef std::unordered_map<AsmSingleVReg, std::vector<size_t> > VarIndexMap;
344 std::vector<size_t> prevVidxes;
345 std::vector<size_t> nextVidxes;
349 std::vector<size_t> prevVidxes;
350 std::vector<size_t> nextVidxes;
354 std::vector<CodeBlock> codeBlocks;
355 SSAReplacesMap ssaReplacesMap;
358 VarIndexMap vregIndexMaps[MAX_REGTYPES_NUM];
359 InterGraph interGraphs[MAX_REGTYPES_NUM];
361 std::unordered_map<size_t, LinearDep> linearDepMaps[MAX_REGTYPES_NUM];
362 std::unordered_map<size_t, EqualToDep> equalToDepMaps[MAX_REGTYPES_NUM];
363 std::unordered_map<size_t, size_t> equalSetMaps[MAX_REGTYPES_NUM];
364 std::vector<std::vector<size_t> > equalSetLists[MAX_REGTYPES_NUM];
368 void createCodeStructure(
const std::vector<AsmCodeFlowEntry>& codeFlow,
369 size_t codeSize,
const cxbyte* code);
371 void applySSAReplaces();
373 void colorInterferenceGraph();
375 void allocateRegisters(
cxuint sectionId);
377 const std::vector<CodeBlock>& getCodeBlocks()
const 378 {
return codeBlocks; }
379 const SSAReplacesMap& getSSAReplacesMap()
const 380 {
return ssaReplacesMap; }
407 typedef std::pair<CString, uint64_t>
DefSym;
423 friend struct AsmParseUtils;
424 friend struct AsmPseudoOps;
425 friend struct AsmGalliumPseudoOps;
426 friend struct AsmAmdPseudoOps;
427 friend struct AsmAmdCL2PseudoOps;
428 friend struct AsmROCmPseudoOps;
429 friend struct GCNAsmUtils;
434 uint32_t driverVersion;
435 uint32_t llvmVersion;
438 bool resolvingRelocs;
440 std::vector<DefSym> defSyms;
441 std::vector<CString> includeDirs;
442 std::vector<AsmSection> sections;
443 std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
444 std::vector<AsmRelocation> relocations;
447 std::stack<AsmScope*> scopeStack;
448 std::vector<AsmScope*> abandonedScopes;
451 std::vector<AsmKernel> kernels;
464 bool lineAlreadyRead;
471 std::stack<AsmInputFilter*> asmInputFilters;
474 std::ostream& messageStream;
475 std::ostream& printStream;
479 std::stack<AsmClause> clauses;
483 uint64_t& currentOutPos;
494 {
return getSourcePos(linePtr-line); }
496 void printWarning(
const AsmSourcePos& pos,
const char* message);
497 void printError(
const AsmSourcePos& pos,
const char* message);
499 void printWarning(
const char* linePtr,
const char* message)
500 { printWarning(getSourcePos(linePtr), message); }
501 void printError(
const char* linePtr,
const char* message)
502 { printError(getSourcePos(linePtr), message); }
504 void printWarning(
LineCol lineCol,
const char* message)
505 { printWarning(getSourcePos(lineCol), message); }
506 void printError(
LineCol lineCol,
const char* message)
507 { printError(getSourcePos(lineCol), message); }
509 LineCol translatePos(
const char* linePtr)
const 510 {
return currentInputFilter->
translatePos(linePtr-line); }
511 LineCol translatePos(
size_t pos)
const 514 bool parseLiteral(uint64_t& value,
const char*& linePtr);
515 bool parseString(std::string& outString,
const char*& linePtr);
517 enum class ParseState
527 ParseState parseSymbol(
const char*& linePtr,
AsmSymbolEntry*& entry,
528 bool localLabel =
true,
bool dontCreateSymbol =
false);
529 bool skipSymbol(
const char*& linePtr);
533 bool assignSymbol(
const CString& symbolName,
const char* symbolPlace,
534 const char* linePtr,
bool reassign =
true,
bool baseExpr =
false);
536 bool assignOutputCounter(
const char* symbolPlace, uint64_t value,
cxuint sectionId,
539 void parsePseudoOps(
const CString& firstName,
const char* stmtPlace,
540 const char* linePtr);
543 bool skipClauses(
bool exitm =
false);
545 bool putRepetitionContent(
AsmRepeat& repeat);
547 void initializeOutputFormat();
549 bool pushClause(
const char*
string,
AsmClauseType clauseType)
552 return pushClause(
string, clauseType,
true, included);
554 bool pushClause(
const char*
string,
AsmClauseType clauseType,
555 bool satisfied,
bool& included);
557 bool popClause(
const char*
string,
AsmClauseType clauseType);
561 std::unordered_set<AsmScope*>& scopeSet);
563 AsmScope* getRecurScope(
const CString& scopePlace,
bool ignoreLast =
false,
564 const char** lastStep =
nullptr);
568 std::unordered_set<AsmScope*>& scopeSet);
571 CString& sameSymName,
bool insertMode =
false);
573 std::pair<AsmSymbolEntry*, bool> insertSymbolInScope(
const CString& symName,
578 std::unordered_set<AsmScope*>& scopeSet);
581 CString& sameRvName,
bool insertMode =
false);
583 std::pair<AsmRegVarEntry*, bool> insertRegVarInScope(
const CString& rvName,
589 bool pushScope(
const CString& scopeName);
593 bool includeFile(
const char* pseudoOpPlace,
const std::string& filename);
595 ParseState makeMacroSubstitution(
const char*
string);
597 bool parseMacroArgValue(
const char*& linePtr, std::string& outStr);
599 void putData(
size_t size,
const cxbyte* data)
601 AsmSection& section = sections[currentSection];
603 currentOutPos += size;
608 void goToMain(
const char* pseudoOpPlace);
609 void goToKernel(
const char* pseudoOpPlace,
const char* kernelName);
610 void goToSection(
const char* pseudoOpPlace,
const char* sectionName, uint64_t align=0);
611 void goToSection(
const char* pseudoOpPlace,
const char* sectionName,
613 void goToSection(
const char* pseudoOpPlace,
cxuint sectionId, uint64_t align=0);
616 cxbyte signess = WS_BOTH);
618 bool isAddressableSection()
const 621 (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
623 bool isWriteableSection()
const 626 (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
628 bool isResolvableSection()
const 633 bool isResolvableSection(
cxuint sectionId)
const 640 void handleRegionsOnKernels(
const std::vector<cxuint>& newKernels,
641 const std::vector<cxuint>& oldKernels,
cxuint codeSection);
643 void tryToResolveSymbols(
AsmScope* scope);
644 void printUnresolvedSymbols(
AsmScope* scope);
663 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
677 std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
685 void writeBinary(
const char* filename)
const;
687 void writeBinary(std::ostream& outStream)
const;
693 {
return driverVersion; }
696 { this->driverVersion = driverVersion; }
700 {
return llvmVersion; }
703 { this->llvmVersion = llvmVersion; }
707 {
return deviceType; }
710 { this->deviceType = deviceType; }
716 { format = binFormat; }
722 { _64bit = this64Bit; }
728 { this->flags = flags; }
730 bool isOldModParam()
const 731 {
return oldModParam; }
734 {
return includeDirs; }
736 void addIncludeDir(
const CString& includeDir);
745 {
return kernelMap; }
754 {
return insertRegVarInScope(name, var).second; }
760 {
return globalScope; }
763 bool isAbsoluteSymbol(
const AsmSymbol& symbol)
const;
766 void addInitialDefSym(
const CString& symName, uint64_t value);
770 {
return formatHandler; }
774 { assembler.printWarning(linePtr, message); }
777 { assembler.printError(linePtr, message); }
781 { assembler.printWarningForRange(bits, value, pos, signess); }
784 { assembler.printWarning(sourcePos, message); }
787 { assembler.printError(sourcePos, message); }
790 { 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:95
bool is64Bit() const
get bitness
Definition: Assembler.h:718
void set64Bit(bool this64Bit)
set bitness
Definition: Assembler.h:721
AsmRegVarMap regVarMap
regvar map
Definition: AsmDefs.h:615
common definitions for assembler and disassembler
Definition: Assembler.h:300
main class of assembler
Definition: Assembler.h:403
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: AsmDefs.h:289
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
size_t readOffset
read offset
Definition: Assembler.h:85
Flags getFlags() const
get flags
Definition: Assembler.h:724
std::unordered_map< CString, AsmRegVar > AsmRegVarMap
regvar map
Definition: AsmDefs.h:531
assembler repeat
Definition: AsmSource.h:238
virtual ~ISAUsageHandler()
destructor
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:409
size_t ssaIdChange
number of SSA id changes
Definition: Assembler.h:311
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:706
AsmRegVarMap::value_type AsmRegVarEntry
regvar entry
Definition: AsmDefs.h:533
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:741
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:695
reference pointer based on Glibmm refptr
Definition: Utilities.h:774
AsmClauseType
type of clause
Definition: Assembler.h:384
Assembler & assembler
assembler
Definition: Assembler.h:177
const AsmRegVarMap & getRegVarMap() const
get regvar map
Definition: Assembler.h:750
ReadPos getReadPos() const
get reading position
Definition: Assembler.h:122
assembler section
Definition: AsmDefs.h:652
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:407
size_t regUsagesPos
position in reg usage
Definition: Assembler.h:87
handles raw code format
Definition: AsmFormats.h:183
assembler scope for symbol, macros, regvars
Definition: AsmDefs.h:611
Regvar info structure.
Definition: AsmDefs.h:510
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:776
AsmRegVarUsage nextUsage()
get next usage
ISAUsageHandler(const std::vector< cxbyte > &content)
constructor
size_t lastOffset
last offset
Definition: Assembler.h:84
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:773
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:239
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:72
std::vector< NextBlock > nexts
nexts blocks, if empty then direct next block
Definition: Assembler.h:326
Flags regFlags
define what extra register must be included
Definition: Assembler.h:241
void pushUsage(const AsmRegVarUsage &rvu)
push regvar or register usage
void rewind()
rewind to start for reading
handles ROCM binary format
Definition: AsmFormats.h:469
size_t ssaId
original SSA id
Definition: Assembler.h:309
all flags
Definition: Assembler.h:57
size_t regVarUsagesPos
position in regvar usage
Definition: Assembler.h:89
line and column
Definition: AsmSource.h:44
size_t instrStructPos
position in instr struct
Definition: Assembler.h:86
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: AsmDefs.h:217
void setFlags(Flags flags)
set flags
Definition: Assembler.h:727
absolute section id
Definition: AsmFormats.h:81
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:397
uint16_t pushedArgs
pushed args
Definition: Assembler.h:90
void setReadPos(const ReadPos rpos)
set reading position
Definition: Assembler.h:128
size_t regVarUsagesPos
position in regVarUsage
Definition: Assembler.h:74
AsmSectionType
assembler section type
Definition: AsmFormats.h:46
unsigned char cxbyte
unsigned byte
Definition: Config.h:215
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:733
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:240
std::unordered_map< CString, RefPtr< const AsmMacro > > AsmMacroMap
assembler macro map
Definition: AsmDefs.h:603
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:399
size_t ssaIdLast
last SSA id in last
Definition: Assembler.h:310
main namespace
Definition: AsmDefs.h:38
bool hasNext() const
has next regvar usage
Definition: Assembler.h:116
cxbyte argPos
argument position
Definition: Assembler.h:91
AsmClauseType type
type of clause
Definition: Assembler.h:396
only for running tests
Definition: Assembler.h:56
Definition: Assembler.h:297
unsigned int cxuint
unsigned int
Definition: Config.h:223
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:744
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:712
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:738
std::vector< cxbyte > instrStruct
structure of register usage
Definition: Assembler.h:79
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: AsmDefs.h:59
std::vector< cxbyte > content
content of section
Definition: AsmDefs.h:660
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: AsmDefs.h:219
virtual cxbyte getRwFlags(AsmRegField regField, uint16_t rstart, uint16_t rend) const =0
get RW flags (used by assembler)
Definition: Assembler.h:341
GCN (register and regvar) Usage handler.
Definition: Assembler.h:154
cxbyte argFlags
???
Definition: Assembler.h:92
GPUDeviceType
type of GPU device
Definition: GPUId.h:51
bool useRegMode
true if in usereg mode
Definition: Assembler.h:76
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:709
utilities for other libraries and programs
bool isNext
is next
Definition: Assembler.h:94
bool readBeforeWrite
have read before write
Definition: Assembler.h:314
GCN arch assembler.
Definition: Assembler.h:234
size_t ssaIdBefore
SSA id before first SSA in block.
Definition: Assembler.h:307
AsmSymbolMap symbolMap
symbol map
Definition: AsmDefs.h:614
register pool numbers
Definition: Assembler.h:238
size_t regUsages2Pos
position in regUsage2
Definition: Assembler.h:73
uint32_t getLLVMVersion() const
get LLVM version
Definition: Assembler.h:699
assembler's clause (if,else,macro,rept)
Definition: Assembler.h:394
regvar usage in code
Definition: AsmDefs.h:545
Definition: Assembler.h:305
const std::vector< cxbyte > & content
code content
Definition: Assembler.h:83
cxbyte defaultInstrSize
default instruction size
Definition: Assembler.h:93
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:753
stgructure that hold read position to store later
Definition: Assembler.h:68
code flow entry
Definition: AsmDefs.h:595
void flush()
flush last pending register usages
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:692
size_t instrStructPos
position instrStructs
Definition: Assembler.h:71
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:769
std::vector< AsmRegUsage2Int > regUsages2
register usage (by .usereg)
Definition: Assembler.h:81
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:779
handles GalliumCompute format
Definition: AsmFormats.h:387
const AsmScope & getGlobalScope() const
get global scope
Definition: Assembler.h:759
an assembler sources handling
Definition: Assembler.h:347
size_t readOffset
read offset
Definition: Assembler.h:70
void setLLVMVersion(uint32_t llvmVersion)
set LLVM version
Definition: Assembler.h:702
uint16_t pushedArgs
pused argds number
Definition: Assembler.h:75
assembler symbol structure
Definition: AsmDefs.h:159
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:457
enable all warnings for assembler
Definition: Assembler.h:50
ISA (register and regvar) Usage handler.
Definition: Assembler.h:64
handles AMD Catalyst format
Definition: AsmFormats.h:209
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:298
Definition: Assembler.h:323
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:551
ISA assembler class.
Definition: Assembler.h:174
section is unresolvable
Definition: AsmFormats.h:92
simple C-string container
Definition: CString.h:38
an assembler for Radeon GPU's
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:715
std::vector< AsmRegVarUsageInt > regVarUsages
regvar usage
Definition: Assembler.h:82
size_t regUsages2Pos
position in reg usage 2
Definition: Assembler.h:88
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:398
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:747
assembler source position
Definition: AsmSource.h:152
std::vector< AsmRegUsageInt > regUsages
register usage
Definition: Assembler.h:80