CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
Assembler.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2018 Mateusz Szpakowski
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
23 #ifndef __CLRX_ASSEMBLER_H__
24 #define __CLRX_ASSEMBLER_H__
25 
26 #include <CLRX/Config.h>
27 #include <cstdint>
28 #include <string>
29 #include <istream>
30 #include <ostream>
31 #include <iostream>
32 #include <vector>
33 #include <utility>
34 #include <stack>
35 #include <list>
36 #include <unordered_set>
37 #include <unordered_map>
38 #include <CLRX/utils/Utilities.h>
39 #include <CLRX/amdasm/Commons.h>
40 #include <CLRX/amdasm/AsmSource.h>
41 #include <CLRX/amdasm/AsmFormats.h>
42 #include <CLRX/amdasm/AsmDefs.h>
43 
45 namespace CLRX
46 {
47 
48 enum: Flags
49 {
54  ASM_MACRONOCASE = 16,
56  ASM_TESTRESOLVE = (1U<<30),
57  ASM_TESTRUN = (1U<<31),
58  ASM_ALL = FLAGS_ALL&~(ASM_TESTRUN|ASM_TESTRESOLVE|ASM_BUGGYFPLIT|ASM_MACRONOCASE|
60 };
61 
62 struct AsmRegVar;
63 
66 {
67 public:
69  struct ReadPos
70  {
71  size_t readOffset;
72  size_t instrStructPos;
73  size_t regUsagesPos;
74  size_t regUsages2Pos;
75  size_t regVarUsagesPos;
76  uint16_t pushedArgs;
77  bool useRegMode;
78  };
79 protected:
80  std::vector<cxbyte> instrStruct;
81  std::vector<AsmRegUsageInt> regUsages;
82  std::vector<AsmRegUsage2Int> regUsages2;
83  std::vector<AsmRegVarUsageInt> regVarUsages;
84  const std::vector<cxbyte>& content;
85  size_t lastOffset;
86  size_t readOffset;
87  size_t instrStructPos;
88  size_t regUsagesPos;
89  size_t regUsages2Pos;
90  size_t regVarUsagesPos;
91  uint16_t pushedArgs;
95  bool isNext;
96  bool useRegMode;
97 
98  void skipBytesInInstrStruct();
100  void putSpace(size_t offset);
101 
103  explicit ISAUsageHandler(const std::vector<cxbyte>& content);
104 public:
106  virtual ~ISAUsageHandler();
108  virtual ISAUsageHandler* copy() const = 0;
109 
111  void pushUsage(const AsmRegVarUsage& rvu);
113  void rewind();
115  void flush();
117  bool hasNext() const
118  { return isNext; }
121 
124  {
126  regVarUsagesPos, pushedArgs, useRegMode };
127  }
129  void setReadPos(const ReadPos rpos)
130  {
131  readOffset = rpos.readOffset;
132  instrStructPos = rpos.instrStructPos;
133  regUsagesPos = rpos.regUsagesPos;
134  regUsages2Pos = rpos.regUsages2Pos;
135  regVarUsagesPos = rpos.regVarUsagesPos;
136  pushedArgs = rpos.pushedArgs;
137  useRegMode = rpos.useRegMode;
138  }
139 
141  void pushUseRegUsage(const AsmRegVarUsage& rvu);
142 
144  virtual cxbyte getRwFlags(AsmRegField regField, uint16_t rstart,
145  uint16_t rend) const = 0;
147  virtual std::pair<uint16_t,uint16_t> getRegPair(AsmRegField regField,
148  cxbyte rwFlags) const = 0;
150  virtual void getUsageDependencies(cxuint rvusNum, const AsmRegVarUsage* rvus,
151  cxbyte* linearDeps, cxbyte* equalToDeps) const = 0;
152 };
153 
156 {
157 private:
158  uint16_t archMask;
159 public:
161  GCNUsageHandler(const std::vector<cxbyte>& content, uint16_t archMask);
163  ~GCNUsageHandler();
164 
166  ISAUsageHandler* copy() const;
167 
168  cxbyte getRwFlags(AsmRegField regFied, uint16_t rstart, uint16_t rend) const;
169  std::pair<uint16_t,uint16_t> getRegPair(AsmRegField regField, cxbyte rwFlags) const;
170  void getUsageDependencies(cxuint rvusNum, const AsmRegVarUsage* rvus,
171  cxbyte* linearDeps, cxbyte* equalToDeps) const;
172 };
173 
176 {
177 protected:
179 
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);
189  void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos& pos,
190  cxbyte signess = WS_BOTH);
191  void addCodeFlowEntry(cxuint sectionId, const AsmCodeFlowEntry& entry);
193  explicit ISAAssembler(Assembler& assembler);
194 public:
196  virtual ~ISAAssembler();
198  virtual ISAUsageHandler* createUsageHandler(std::vector<cxbyte>& content) const = 0;
199 
201  virtual void assemble(const CString& mnemonic, const char* mnemPlace,
202  const char* linePtr, const char* lineEnd, std::vector<cxbyte>& output,
203  ISAUsageHandler* usageHandler) = 0;
205  virtual bool resolveCode(const AsmSourcePos& sourcePos, cxuint targetSectionId,
206  cxbyte* sectionData, size_t offset, AsmExprTargetType targetType,
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,
224  cxuint& regEnd, const AsmRegVar*& regVar) = 0;
226  virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType) = 0;
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;
232 };
233 
236 {
237 public:
239  struct Regs {
243  };
244 private:
245  friend struct GCNAsmUtils; // INTERNAL LOGIC
246  union {
247  Regs regs;
248  cxuint regTable[2];
249  };
250  uint16_t curArchMask;
251  cxbyte currentRVUIndex;
252  AsmRegVarUsage instrRVUs[6];
253 
254  void resetInstrRVUs()
255  {
256  for (AsmRegVarUsage& rvu: instrRVUs)
257  rvu.regField = ASMFIELD_NONE;
258  }
259  void setCurrentRVU(cxbyte idx)
260  { currentRVUIndex = idx; }
261 
262  void setRegVarUsage(const AsmRegVarUsage& rvu)
263  { instrRVUs[currentRVUIndex] = rvu; }
264 
265  void flushInstrRVUs(ISAUsageHandler* usageHandler)
266  {
267  for (const AsmRegVarUsage& rvu: instrRVUs)
268  if (rvu.regField != ASMFIELD_NONE)
269  usageHandler->pushUsage(rvu);
270  }
271 public:
273  explicit GCNAssembler(Assembler& assembler);
275  ~GCNAssembler();
276 
277  ISAUsageHandler* createUsageHandler(std::vector<cxbyte>& content) const;
278 
279  void assemble(const CString& mnemonic, const char* mnemPlace, const char* linePtr,
280  const char* lineEnd, std::vector<cxbyte>& output,
281  ISAUsageHandler* usageHandler);
282  bool resolveCode(const AsmSourcePos& sourcePos, cxuint targetSectionId,
283  cxbyte* sectionData, size_t offset, AsmExprTargetType targetType,
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,
292  const AsmRegVar*& regVar);
293  bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType);
294  bool parseRegisterType(const char*& linePtr, const char* end, cxuint& type);
295  size_t getInstructionSize(size_t codeSize, const cxbyte* code) const;
296 };
297 
299 {
300 public:
301  struct NextBlock
302  {
303  size_t block;
304  bool isCall;
305  };
306  struct SSAInfo
307  {
308  size_t ssaIdBefore;
309  size_t ssaIdFirst; // SSA id at first change
310  size_t ssaId;
311  size_t ssaIdLast;
312  size_t ssaIdChange;
313  size_t firstPos;
314  size_t lastPos;
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)
322  { }
323  };
324  struct CodeBlock
325  {
326  size_t start, end; // place in code
327  std::vector<NextBlock> nexts;
328  bool haveCalls;
329  bool haveReturn;
330  bool haveEnd;
331  // key - regvar, value - SSA info for this regvar
332  std::unordered_map<AsmSingleVReg, SSAInfo> ssaInfoMap;
333  ISAUsageHandler::ReadPos usagePos;
334  };
335 
336  // first - orig ssaid, second - dest ssaid
337  typedef std::pair<size_t, size_t> SSAReplace;
338  typedef std::unordered_map<AsmSingleVReg, std::vector<SSAReplace> > SSAReplacesMap;
339  // interference graph type
341  typedef std::unordered_map<AsmSingleVReg, std::vector<size_t> > VarIndexMap;
342  struct LinearDep
343  {
344  cxbyte align;
345  std::vector<size_t> prevVidxes;
346  std::vector<size_t> nextVidxes;
347  };
348  struct EqualToDep
349  {
350  std::vector<size_t> prevVidxes;
351  std::vector<size_t> nextVidxes;
352  };
353 private:
354  Assembler& assembler;
355  std::vector<CodeBlock> codeBlocks;
356  SSAReplacesMap ssaReplacesMap;
357  size_t regTypesNum;
358 
359  VarIndexMap vregIndexMaps[MAX_REGTYPES_NUM]; // indices to igraph for 2 reg types
360  InterGraph interGraphs[MAX_REGTYPES_NUM]; // for 2 register
361  Array<cxuint> graphColorMaps[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];
366 public:
367  AsmRegAllocator(Assembler& assembler);
368 
369  void createCodeStructure(const std::vector<AsmCodeFlowEntry>& codeFlow,
370  size_t codeSize, const cxbyte* code);
371  void createSSAData(ISAUsageHandler& usageHandler);
372  void applySSAReplaces();
373  void createInterferenceGraph(ISAUsageHandler& usageHandler);
374  void colorInterferenceGraph();
375 
376  void allocateRegisters(cxuint sectionId);
377 
378  const std::vector<CodeBlock>& getCodeBlocks() const
379  { return codeBlocks; }
380  const SSAReplacesMap& getSSAReplacesMap() const
381  { return ssaReplacesMap; }
382 };
383 
385 enum class AsmClauseType
386 {
387  IF,
388  ELSEIF,
389  ELSE,
390  REPEAT,
391  MACRO
392 };
393 
395 struct AsmClause
396 {
401 };
402 
405 {
406 public:
408  typedef std::pair<CString, uint64_t> DefSym;
410  typedef std::unordered_map<CString, cxuint> KernelMap;
411 private:
412  friend class AsmStreamInputFilter;
413  friend class AsmMacroInputFilter;
414  friend class AsmForInputFilter;
415  friend class AsmExpression;
416  friend class AsmFormatHandler;
417  friend class AsmRawCodeHandler;
418  friend class AsmAmdHandler;
419  friend class AsmAmdCL2Handler;
420  friend class AsmGalliumHandler;
421  friend class AsmROCmHandler;
422  friend class ISAAssembler;
423  friend class AsmRegAllocator;
424 
425  friend struct AsmParseUtils; // INTERNAL LOGIC
426  friend struct AsmPseudoOps; // INTERNAL LOGIC
427  friend struct AsmGalliumPseudoOps; // INTERNAL LOGIC
428  friend struct AsmAmdPseudoOps; // INTERNAL LOGIC
429  friend struct AsmAmdCL2PseudoOps; // INTERNAL LOGIC
430  friend struct AsmROCmPseudoOps; // INTERNAL LOGIC
431  friend struct GCNAsmUtils; // INTERNAL LOGIC
432 
433  Array<CString> filenames;
434  BinaryFormat format;
435  GPUDeviceType deviceType;
436  uint32_t driverVersion;
437  uint32_t llvmVersion; // GalliumCompute
438  bool _64bit;
439  bool newROCmBinFormat;
440  bool good;
441  bool resolvingRelocs;
442  bool doNotRemoveFromSymbolClones;
443  ISAAssembler* isaAssembler;
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;
452  AsmScope globalScope;
453  AsmMacroMap macroMap;
454  std::stack<AsmScope*> scopeStack;
455  std::vector<AsmScope*> abandonedScopes;
456  AsmScope* currentScope;
457  KernelMap kernelMap;
458  std::vector<AsmKernel> kernels;
460  Flags flags;
461  uint64_t macroCount;
462  uint64_t localCount; // macro's local count
463  bool alternateMacro;
464  bool buggyFPLit;
465  bool macroCase;
466  bool oldModParam;
467 
468  cxuint inclusionLevel;
469  cxuint macroSubstLevel;
470  cxuint repetitionLevel;
471  bool lineAlreadyRead; // if line already read
472 
473  size_t lineSize;
474  const char* line;
475  bool endOfAssembly;
476  bool sectionDiffsPrepared;
477 
478  cxuint filenameIndex;
479  std::stack<AsmInputFilter*> asmInputFilters;
480  AsmInputFilter* currentInputFilter;
481 
482  std::ostream& messageStream;
483  std::ostream& printStream;
484 
485  AsmFormatHandler* formatHandler;
486 
487  std::stack<AsmClause> clauses;
488 
489  cxuint currentKernel;
490  cxuint& currentSection;
491  uint64_t& currentOutPos;
492 
493  bool withSectionDiffs() const
494  { return formatHandler!=nullptr && formatHandler->isSectionDiffsResolvable(); }
495 
496  AsmSourcePos getSourcePos(LineCol lineCol) const
497  {
498  return { currentInputFilter->getMacroSubst(), currentInputFilter->getSource(),
499  lineCol.lineNo, lineCol.colNo };
500  }
501 
502  AsmSourcePos getSourcePos(size_t pos) const
503  { return currentInputFilter->getSourcePos(pos); }
504  AsmSourcePos getSourcePos(const char* linePtr) const
505  { return getSourcePos(linePtr-line); }
506 
507  void printWarning(const AsmSourcePos& pos, const char* message);
508  void printError(const AsmSourcePos& pos, const char* message);
509 
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); }
514 
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); }
519 
520  LineCol translatePos(const char* linePtr) const
521  { return currentInputFilter->translatePos(linePtr-line); }
522  LineCol translatePos(size_t pos) const
523  { return currentInputFilter->translatePos(pos); }
524 
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);
528 
529  enum class ParseState
530  {
531  FAILED = 0,
532  PARSED,
533  MISSING // missing element
534  };
535 
539  ParseState parseSymbol(const char*& linePtr, AsmSymbolEntry*& entry,
540  bool localLabel = true, bool dontCreateSymbol = false);
541  bool skipSymbol(const char*& linePtr);
542 
543  bool setSymbol(AsmSymbolEntry& symEntry, uint64_t value, cxuint sectionId);
544 
545  bool assignSymbol(const CString& symbolName, const char* symbolPlace,
546  const char* linePtr, bool reassign = true, bool baseExpr = false);
547 
548  bool assignOutputCounter(const char* symbolPlace, uint64_t value, cxuint sectionId,
549  cxbyte fillValue = 0);
550 
551  void parsePseudoOps(const CString& firstName, const char* stmtPlace,
552  const char* linePtr);
553 
555  bool skipClauses(bool exitm = false);
556  bool putMacroContent(RefPtr<AsmMacro> macro);
557  bool putRepetitionContent(AsmRepeat& repeat);
558 
559  void initializeOutputFormat();
560 
561  bool pushClause(const char* string, AsmClauseType clauseType)
562  {
563  bool included; // to ignore
564  return pushClause(string, clauseType, true, included);
565  }
566  bool pushClause(const char* string, AsmClauseType clauseType,
567  bool satisfied, bool& included);
568  // return false when failed (for example no clauses)
569  bool popClause(const char* string, AsmClauseType clauseType);
570 
571  // recursive function to find scope in scope
572  AsmScope* findScopeInScope(AsmScope* scope, const CString& scopeName,
573  std::unordered_set<AsmScope*>& scopeSet);
574  // find scope by identifier
575  AsmScope* getRecurScope(const CString& scopePlace, bool ignoreLast = false,
576  const char** lastStep = nullptr);
577  // find symbol in scopes
578  // internal recursive function to find symbol in scope
579  AsmSymbolEntry* findSymbolInScopeInt(AsmScope* scope, const CString& symName,
580  std::unordered_set<AsmScope*>& scopeSet);
581  // scope - return scope from scoped name
582  AsmSymbolEntry* findSymbolInScope(const CString& symName, AsmScope*& scope,
583  CString& sameSymName, bool insertMode = false);
584  // similar to map::insert, but returns pointer
585  std::pair<AsmSymbolEntry*, bool> insertSymbolInScope(const CString& symName,
586  const AsmSymbol& symbol);
587 
588  // internal recursive function to find symbol in scope
589  AsmRegVarEntry* findRegVarInScopeInt(AsmScope* scope, const CString& rvName,
590  std::unordered_set<AsmScope*>& scopeSet);
591  // scope - return scope from scoped name
592  AsmRegVarEntry* findRegVarInScope(const CString& rvName, AsmScope*& scope,
593  CString& sameRvName, bool insertMode = false);
594  // similar to map::insert, but returns pointer
595  std::pair<AsmRegVarEntry*, bool> insertRegVarInScope(const CString& rvName,
596  const AsmRegVar& regVar);
597 
598  // create scope
599  bool getScope(AsmScope* parent, const CString& scopeName, AsmScope*& scope);
600  // push new scope level
601  bool pushScope(const CString& scopeName);
602  bool popScope();
603 
605  bool includeFile(const char* pseudoOpPlace, const std::string& filename);
606 
607  ParseState makeMacroSubstitution(const char* string);
608 
609  bool parseMacroArgValue(const char*& linePtr, std::string& outStr);
610 
611  void putData(size_t size, const cxbyte* data)
612  {
613  AsmSection& section = sections[currentSection];
614  section.content.insert(section.content.end(), data, data+size);
615  currentOutPos += size;
616  }
617 
618  cxbyte* reserveData(size_t size, cxbyte fillValue = 0);
619 
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,
624  AsmSectionType type, Flags flags, uint64_t align=0);
625  void goToSection(const char* pseudoOpPlace, cxuint sectionId, uint64_t align=0);
626 
627  void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos& pos,
628  cxbyte signess = WS_BOTH);
629 
630  bool isAddressableSection() const
631  {
632  return currentSection==ASMSECT_ABS ||
633  (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
634  }
635  bool isWriteableSection() const
636  {
637  return currentSection!=ASMSECT_ABS &&
638  (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
639  }
640  bool isResolvableSection() const
641  {
642  return currentSection==ASMSECT_ABS ||
643  (sections[currentSection].flags & ASMSECT_UNRESOLVABLE) == 0;
644  }
645  bool isResolvableSection(cxuint sectionId) const
646  {
647  return sectionId==ASMSECT_ABS ||
648  (sections[sectionId].flags & ASMSECT_UNRESOLVABLE) == 0;
649  }
650 
651  // oldKernels and newKernels must be sorted
652  void handleRegionsOnKernels(const std::vector<cxuint>& newKernels,
653  const std::vector<cxuint>& oldKernels, cxuint codeSection);
654 
655  void tryToResolveSymbol(AsmSymbolEntry& symEntry);
656  void tryToResolveSymbols(AsmScope* scope);
657  void printUnresolvedSymbols(AsmScope* scope);
658 
659  bool resolveExprTarget(const AsmExpression* expr, uint64_t value, cxuint sectionId);
660 
661  void cloneSymEntryIfNeeded(AsmSymbolEntry& symEntry);
662 
663  void undefineSymbol(AsmSymbolEntry& symEntry);
664 
665 protected:
667  bool readLine();
668 public:
670 
679  explicit Assembler(const CString& filename, std::istream& input, Flags flags = 0,
682  std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
683 
685 
693  explicit Assembler(const Array<CString>& filenames, Flags flags = 0,
696  std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
698  ~Assembler();
699 
701  bool assemble();
702 
704  void writeBinary(const char* filename) const;
706  void writeBinary(std::ostream& outStream) const;
708  void writeBinary(Array<cxbyte>& array) const;
709 
711  uint32_t getDriverVersion() const
712  { return driverVersion; }
714  void setDriverVersion(uint32_t driverVersion)
715  { this->driverVersion = driverVersion; }
716 
718  uint32_t getLLVMVersion() const
719  { return llvmVersion; }
721  void setLLVMVersion(uint32_t llvmVersion)
722  { this->llvmVersion = llvmVersion; }
723 
726  { return deviceType; }
728  void setDeviceType(const GPUDeviceType deviceType)
729  { this->deviceType = deviceType; }
732  { return format; }
735  { format = binFormat; }
737  bool is64Bit() const
738  { return _64bit; }
740  void set64Bit(bool this64Bit)
741  { _64bit = this64Bit; }
743  bool isNewROCmBinFormat() const
744  { return newROCmBinFormat; }
746  void setNewROCmBinFormat(bool newFmt)
747  { newROCmBinFormat = newFmt; }
749  Flags getFlags() const
750  { return flags; }
752  void setFlags(Flags flags)
753  { this->flags = flags; }
755  bool isAltMacro() const
756  { return alternateMacro; }
758  bool isMacroCase() const
759  { return macroCase; }
761  bool isOldModParam() const
762  { return oldModParam; }
764  bool isBuggyFPLit() const
765  { return buggyFPLit; }
767  const std::vector<CString>& getIncludeDirs() const
768  { return includeDirs; }
770  void addIncludeDir(const CString& includeDir);
772  const AsmSymbolMap& getSymbolMap() const
773  { return globalScope.symbolMap; }
775  const std::vector<AsmSection>& getSections() const
776  { return sections; }
777  // get first sections for rel spaces
778  const std::vector<Array<cxuint> >& getRelSpacesSections() const
779  { return relSpacesSections; }
781  const KernelMap& getKernelMap() const
782  { return kernelMap; }
784  const std::vector<AsmKernel>& getKernels() const
785  { return kernels; }
787  const AsmRegVarMap& getRegVarMap() const
788  { return globalScope.regVarMap; }
790  bool addRegVar(const CString& name, const AsmRegVar& var)
791  { return insertRegVarInScope(name, var).second; }
793  bool getRegVar(const CString& name, const AsmRegVar*& regVar);
794 
796  const AsmScope& getGlobalScope() const
797  { return globalScope; }
798 
800  bool isAbsoluteSymbol(const AsmSymbol& symbol) const;
801 
803  void addInitialDefSym(const CString& symName, uint64_t value);
804 
807  { return formatHandler; }
808 };
809 
810 inline void ISAAssembler::printWarning(const char* linePtr, const char* message)
811 { assembler.printWarning(linePtr, message); }
812 
813 inline void ISAAssembler::printError(const char* linePtr, const char* message)
814 { assembler.printError(linePtr, message); }
815 
816 inline void ISAAssembler::printWarningForRange(cxuint bits, uint64_t value,
817  const AsmSourcePos& pos, cxbyte signess)
818 { assembler.printWarningForRange(bits, value, pos, signess); }
819 
820 inline void ISAAssembler::printWarning(const AsmSourcePos& sourcePos, const char* message)
821 { assembler.printWarning(sourcePos, message); }
822 
823 inline void ISAAssembler::printError(const AsmSourcePos& sourcePos, const char* message)
824 { assembler.printError(sourcePos, message); }
825 
826 inline void ISAAssembler::addCodeFlowEntry(cxuint sectionId, const AsmCodeFlowEntry& entry)
827 { assembler.sections[sectionId].addCodeFlowEntry(entry); }
828 
829 };
830 
831 #endif
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
AMD CATALYST format.
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
assembler macro input filter (for macro filtering)
Definition: AsmSource.h:450
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 input layout filter
Definition: AsmSource.h:416
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
bool isSectionDiffsResolvable() const
return true if format handler can resolve differences between sections
Definition: AsmFormats.h:154
disable case-insensitive naming (default)
Definition: Assembler.h:55
void pushUsage(const AsmRegVarUsage &rvu)
push regvar or register usage
Configuration header.
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
assembler &#39;for&#39; pseudo-op input filter
Definition: AsmSource.h:502
an assembler formats
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
assdembler format handler
Definition: AsmFormats.h:121
RefPtr< const AsmSource > getSource() const
get current source after reading line
Definition: AsmSource.h:395
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
LineCol translatePos(size_t position) const
translate position to line number and column number
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
macro substitution
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&#39;s clause (if,else,macro,rept)
Definition: Assembler.h:395
regvar usage in code
Definition: AsmDefs.h:604
AsmSourcePos getSourcePos(size_t position) const
get source position after reading line
Definition: AsmSource.h:402
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
assembler input filter for reading lines
Definition: AsmSource.h:353
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
RefPtr< const AsmMacroSubst > getMacroSubst() const
get current macro substitution after reading line
Definition: AsmSource.h:398
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&#39;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