CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Assembler.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2016 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 <unordered_set>
36 #include <unordered_map>
37 #include <CLRX/utils/Utilities.h>
38 #include <CLRX/amdasm/Commons.h>
39 #include <CLRX/amdasm/AsmSource.h>
40 #include <CLRX/amdasm/AsmFormats.h>
41 
43 namespace CLRX
44 {
45 
46 enum: Flags
47 {
49  ASM_FORCE_ADD_SYMBOLS = 2,
50  ASM_ALTMACRO = 4,
51  ASM_TESTRUN = (1U<<31),
52  ASM_ALL = FLAGS_ALL&~ASM_TESTRUN
53 };
54 
55 enum: cxbyte {
56  WS_UNSIGNED = 0, // only unsigned
57  WS_BOTH = 1, // both signed and unsigned range checking
58 };
59 
61 typedef cxbyte AsmExprTargetType;
62 
63 enum : AsmExprTargetType
64 {
70 
71  GCNTGT_LITIMM = 16,
72  GCNTGT_SOPKSIMM16,
73  GCNTGT_SOPJMP,
74  GCNTGT_SMRDOFFSET,
75  GCNTGT_DSOFFSET16,
76  GCNTGT_DSOFFSET8_0,
77  GCNTGT_DSOFFSET8_1,
78  GCNTGT_MXBUFOFFSET,
79  GCNTGT_SMEMOFFSET,
80  GCNTGT_SOPCIMM8,
81  GCNTGT_SMEMIMM
82 };
83 
86 {
87 protected:
89 
91  void printWarning(const char* linePtr, const char* message);
93  void printError(const char* linePtr, const char* message);
95  void printWarning(const AsmSourcePos& sourcePos, const char* message);
97  void printError(const AsmSourcePos& sourcePos, const char* message);
99  void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos& pos,
100  cxbyte signess = WS_BOTH);
102  explicit ISAAssembler(Assembler& assembler);
103 public:
105  virtual ~ISAAssembler();
106 
108  virtual void assemble(const CString& mnemonic, const char* mnemPlace,
109  const char* linePtr, const char* lineEnd, std::vector<cxbyte>& output) = 0;
111  virtual bool resolveCode(const AsmSourcePos& sourcePos, cxuint targetSectionId,
112  cxbyte* sectionData, size_t offset, AsmExprTargetType targetType,
113  cxuint sectionId, uint64_t value) = 0;
115  virtual bool checkMnemonic(const CString& mnemonic) const = 0;
117  virtual void setAllocatedRegisters(const cxuint* regs = nullptr,
118  Flags regFlags = 0) = 0;
120  virtual const cxuint* getAllocatedRegisters(size_t& regTypesNum,
121  Flags& regFlags) const = 0;
123  virtual void fillAlignment(size_t size, cxbyte* output) = 0;
125  virtual bool parseRegisterRange(const char*& linePtr,
126  cxuint& regStart, cxuint& regEnd) = 0;
128  virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType) = 0;
129 };
130 
133 {
134 public:
136  struct Regs {
137  cxuint sgprsNum;
138  cxuint vgprsNum;
140  };
141 private:
142  union {
143  Regs regs;
144  cxuint regTable[2];
145  };
146  uint16_t curArchMask;
147 public:
149  explicit GCNAssembler(Assembler& assembler);
151  ~GCNAssembler();
152 
153  void assemble(const CString& mnemonic, const char* mnemPlace, const char* linePtr,
154  const char* lineEnd, std::vector<cxbyte>& output);
155  bool resolveCode(const AsmSourcePos& sourcePos, cxuint targetSectionId,
156  cxbyte* sectionData, size_t offset, AsmExprTargetType targetType,
157  cxuint sectionId, uint64_t value);
158  bool checkMnemonic(const CString& mnemonic) const;
159  void setAllocatedRegisters(const cxuint* regs, Flags regFlags);
160  const cxuint* getAllocatedRegisters(size_t& regTypesNum, Flags& regFlags) const;
161  void fillAlignment(size_t size, cxbyte* output);
162  bool parseRegisterRange(const char*& linePtr, cxuint& regStart, cxuint& regEnd);
163  bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType);
164 };
165 
166 /*
167  * assembler expressions
168  */
169 
171 enum class AsmExprOp : cxbyte
172 {
173  ARG_VALUE = 0,
174  ARG_SYMBOL = 1,
175  NEGATE = 2,
176  BIT_NOT,
177  LOGICAL_NOT,
178  PLUS,
179  ADDITION,
180  SUBTRACT,
181  MULTIPLY,
182  DIVISION,
184  MODULO,
185  SIGNED_MODULO,
186  BIT_AND,
187  BIT_OR, //< bitwise OR
188  BIT_XOR,
189  BIT_ORNOT,
190  SHIFT_LEFT,
191  SHIFT_RIGHT,
193  LOGICAL_AND,
194  LOGICAL_OR,
195  EQUAL,
196  NOT_EQUAL,
197  LESS,
198  LESS_EQ,
199  GREATER,
200  GREATER_EQ,
201  BELOW,
202  BELOW_EQ,
203  ABOVE,
204  ABOVE_EQ,
205  CHOICE,
206  CHOICE_START,
207  FIRST_ARG = ARG_VALUE,
208  LAST_ARG = ARG_SYMBOL,
209  FIRST_UNARY = NEGATE,
210  LAST_UNARY = PLUS,
213  NONE = 0xff
214 };
215 
216 struct AsmExprTarget;
217 
218 union AsmExprArg;
219 
220 class AsmExpression;
221 
224 {
226  size_t argIndex;
227  size_t opIndex;
228 
230  bool operator==(const AsmExprSymbolOccurrence& b) const
231  { return expression==b.expression && opIndex==b.opIndex && argIndex==b.argIndex; }
232 };
233 
235 struct AsmSymbol
236 {
237  cxuint refCount;
238  cxuint sectionId;
239  cxbyte info;
240  cxbyte other;
241  cxuint hasValue:1;
242  cxuint onceDefined:1;
243  cxuint resolving:1;
244  cxuint base:1;
245  cxuint snapshot:1;
246  cxuint regRange:1;
247  uint64_t value;
248  uint64_t size;
250 
252  std::vector<AsmExprSymbolOccurrence> occurrencesInExprs;
253 
255  explicit AsmSymbol(bool _onceDefined = false) :
256  refCount(1), sectionId(ASMSECT_ABS), info(0), other(0), hasValue(false),
257  onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
258  regRange(false), value(0), size(0), expression(nullptr)
259  { }
261  explicit AsmSymbol(AsmExpression* expr, bool _onceDefined = false, bool _base = false) :
262  refCount(1), sectionId(ASMSECT_ABS), info(0), other(0), hasValue(false),
263  onceDefined(_onceDefined), resolving(false), base(_base),
264  snapshot(false), regRange(false), value(0), size(0), expression(expr)
265  { }
267  explicit AsmSymbol(cxuint _sectionId, uint64_t _value, bool _onceDefined = false) :
268  refCount(1), sectionId(_sectionId), info(0), other(0), hasValue(true),
269  onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
270  regRange(false), value(_value), size(0), expression(nullptr)
271  { }
273  ~AsmSymbol();
274 
276  void addOccurrenceInExpr(AsmExpression* expr, size_t argIndex, size_t opIndex)
277  { occurrencesInExprs.push_back({expr, argIndex, opIndex}); }
279  void removeOccurrenceInExpr(AsmExpression* expr, size_t argIndex, size_t opIndex);
281  void clearOccurrencesInExpr();
283  void undefine();
285  bool isDefined() const
286  { return hasValue || expression!=nullptr; }
287 };
288 
290 typedef std::unordered_map<CString, AsmSymbol> AsmSymbolMap;
292 typedef AsmSymbolMap::value_type AsmSymbolEntry;
293 
296 {
297  AsmExprTargetType type;
298  union
299  {
300  AsmSymbolEntry* symbol;
301  struct {
302  cxuint sectionId;
303  size_t offset;
304  };
305  };
308 
310  AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
311  : type(_type), sectionId(_sectionId), offset(_offset)
312  { }
313 
315  static AsmExprTarget symbolTarget(AsmSymbolEntry* entry)
316  {
317  AsmExprTarget target;
318  target.type = ASMXTGT_SYMBOL;
319  target.symbol = entry;
320  return target;
321  }
322 
324  template<typename T>
325  static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
326  {
327  AsmExprTarget target;
328  target.type = (sizeof(T)==1) ? ASMXTGT_DATA8 : (sizeof(T)==2) ? ASMXTGT_DATA16 :
329  (sizeof(T)==4) ? ASMXTGT_DATA32 : ASMXTGT_DATA64;
330  target.sectionId = sectionId;
331  target.offset = offset;
332  return target;
333  }
334 };
335 
338 {
339  cxuint sectionId;
340  size_t offset;
342  union {
343  AsmSymbolEntry* symbol;
344  cxuint relSectionId;
345  };
346  uint64_t addend;
347 };
348 
351 {
352 private:
353  class TempSymbolSnapshotMap;
354 
355  AsmExprTarget target;
356  AsmSourcePos sourcePos;
357  size_t symOccursNum;
358  bool relativeSymOccurs;
359  bool baseExpr;
360  Array<AsmExprOp> ops;
361  std::unique_ptr<LineCol[]> messagePositions;
362  std::unique_ptr<AsmExprArg[]> args;
363 
364  AsmSourcePos getSourcePos(size_t msgPosIndex) const
365  {
366  AsmSourcePos pos = sourcePos;
367  pos.lineNo = messagePositions[msgPosIndex].lineNo;
368  pos.colNo = messagePositions[msgPosIndex].colNo;
369  return pos;
370  }
371 
372  static bool makeSymbolSnapshot(Assembler& assembler,
373  TempSymbolSnapshotMap* snapshotMap, const AsmSymbolEntry& symEntry,
374  AsmSymbolEntry*& outSymEntry, const AsmSourcePos* topParentSourcePos);
375 
376  AsmExpression();
377  void setParams(size_t symOccursNum, bool relativeSymOccurs,
378  size_t _opsNum, const AsmExprOp* ops, size_t opPosNum, const LineCol* opPos,
379  size_t argsNum, const AsmExprArg* args, bool baseExpr = false);
380 public:
382  AsmExpression(const AsmSourcePos& pos, size_t symOccursNum, bool relativeSymOccurs,
383  size_t opsNum, size_t opPosNum, size_t argsNum, bool baseExpr = false);
385  AsmExpression(const AsmSourcePos& pos, size_t symOccursNum, bool relativeSymOccurs,
386  size_t opsNum, const AsmExprOp* ops, size_t opPosNum,
387  const LineCol* opPos, size_t argsNum, const AsmExprArg* args,
388  bool baseExpr = false);
390  ~AsmExpression();
391 
393  bool isEmpty() const
394  { return ops.empty(); }
395 
397  AsmExpression* createForSnapshot(const AsmSourcePos* exprSourcePos) const;
398 
400  void setTarget(AsmExprTarget _target)
401  { target = _target; }
402 
404 
410  bool evaluate(Assembler& assembler, uint64_t& value, cxuint& sectionId) const
411  { return evaluate(assembler, 0, ops.size(), value, sectionId); }
412 
414 
422  bool evaluate(Assembler& assembler, size_t opStart, size_t opEnd,
423  uint64_t& value, cxuint& sectionId) const;
424 
426 
433  static AsmExpression* parse(Assembler& assembler, size_t& linePos,
434  bool makeBase = false, bool dontResolveSymbolsLater = false);
435 
437 
444  static AsmExpression* parse(Assembler& assembler, const char*& linePtr,
445  bool makeBase = false, bool dontResolveSymbolsLater = false);
446 
448  static bool isArg(AsmExprOp op)
449  { return (AsmExprOp::FIRST_ARG <= op && op <= AsmExprOp::LAST_ARG); }
451  static bool isUnaryOp(AsmExprOp op)
452  { return (AsmExprOp::FIRST_UNARY <= op && op <= AsmExprOp::LAST_UNARY); }
454  static bool isBinaryOp(AsmExprOp op)
455  { return (AsmExprOp::FIRST_BINARY <= op && op <= AsmExprOp::LAST_BINARY); }
457  const AsmExprTarget& getTarget() const
458  { return target; }
460  size_t getSymOccursNum() const
461  { return symOccursNum; }
463  size_t hasRelativeSymOccurs() const
464  { return relativeSymOccurs; }
467  { return --symOccursNum!=0; }
468 
470  void substituteOccurrence(AsmExprSymbolOccurrence occurrence, uint64_t value,
471  cxuint sectionId = ASMSECT_ABS);
473  const Array<AsmExprOp>& getOps() const
474  { return ops; }
476  const AsmExprArg* getArgs() const
477  { return args.get(); }
479  const AsmSourcePos& getSourcePos() const
480  { return sourcePos; }
481 
482  size_t toTop(size_t opIndex) const;
483 
485  static bool makeSymbolSnapshot(Assembler& assembler, const AsmSymbolEntry& symEntry,
486  AsmSymbolEntry*& outSymEntry, const AsmSourcePos* parentExprSourcePos);
487 };
488 
490 {
491  if (base) delete expression; // if symbol with base expresion
493 }
494 
497 {
498  AsmSymbolEntry* symbol;
499  uint64_t value;
500  struct {
501  uint64_t value;
502  cxuint sectionId;
503  } relValue;
504 };
505 
507  uint64_t value, cxuint sectionId)
508 {
509  ops[occurrence.opIndex] = AsmExprOp::ARG_VALUE;
510  args[occurrence.argIndex].relValue.value = value;
511  args[occurrence.argIndex].relValue.sectionId = sectionId;
512  if (sectionId != ASMSECT_ABS)
513  relativeSymOccurs = true;
514 }
515 
518 {
519  const char* name;
520  cxuint kernelId;
523  uint64_t alignment;
524  uint64_t size;
525  std::vector<cxbyte> content;
526 
528  size_t getSize() const
529  { return ((flags&ASMSECT_WRITEABLE) != 0) ? content.size() : size; }
530 };
531 
533 enum class AsmClauseType
534 {
535  IF,
536  ELSEIF,
537  ELSE,
538  REPEAT,
539  MACRO
540 };
541 
543 struct AsmKernel
544 {
545  const char* name;
547 };
548 
550 struct AsmClause
551 {
556 };
557 
560 {
561 public:
563  typedef std::pair<CString, uint64_t> DefSym;
565  typedef std::unordered_map<CString, RefPtr<const AsmMacro> > MacroMap;
567  typedef std::unordered_map<CString, cxuint> KernelMap;
568 private:
569  friend class AsmStreamInputFilter;
570  friend class AsmMacroInputFilter;
571  friend class AsmExpression;
572  friend class AsmFormatHandler;
573  friend class AsmRawCodeHandler;
574  friend class AsmAmdHandler;
575  friend class AsmAmdCL2Handler;
576  friend class AsmGalliumHandler;
577  friend class ISAAssembler;
578 
579  friend struct AsmParseUtils; // INTERNAL LOGIC
580  friend struct AsmPseudoOps; // INTERNAL LOGIC
581  friend struct AsmGalliumPseudoOps; // INTERNAL LOGIC
582  friend struct AsmAmdPseudoOps; // INTERNAL LOGIC
583  friend struct AsmAmdCL2PseudoOps; // INTERNAL LOGIC
584  friend struct GCNAsmUtils; // INTERNAL LOGIC
585 
586  Array<CString> filenames;
587  BinaryFormat format;
588  GPUDeviceType deviceType;
589  uint32_t driverVersion;
590  bool _64bit;
591  bool good;
592  bool resolvingRelocs;
593  ISAAssembler* isaAssembler;
594  std::vector<DefSym> defSyms;
595  std::vector<CString> includeDirs;
596  std::vector<AsmSection> sections;
597  AsmSymbolMap symbolMap;
598  std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
599  std::vector<AsmRelocation> relocations;
600  MacroMap macroMap;
601  KernelMap kernelMap;
602  std::vector<AsmKernel> kernels;
603  Flags flags;
604  uint64_t macroCount;
605  uint64_t localCount; // macro's local count
606  bool alternateMacro;
607 
608  cxuint inclusionLevel;
609  cxuint macroSubstLevel;
610  cxuint repetitionLevel;
611  bool lineAlreadyRead; // if line already read
612 
613  size_t lineSize;
614  const char* line;
615  bool endOfAssembly;
616 
617  cxuint filenameIndex;
618  std::stack<AsmInputFilter*> asmInputFilters;
619  AsmInputFilter* currentInputFilter;
620 
621  std::ostream& messageStream;
622  std::ostream& printStream;
623 
624  AsmFormatHandler* formatHandler;
625 
626  std::stack<AsmClause> clauses;
627 
628  cxuint currentKernel;
629  cxuint& currentSection;
630  uint64_t& currentOutPos;
631 
632  AsmSourcePos getSourcePos(LineCol lineCol) const
633  {
634  return { currentInputFilter->getMacroSubst(), currentInputFilter->getSource(),
635  lineCol.lineNo, lineCol.colNo };
636  }
637 
638  AsmSourcePos getSourcePos(size_t pos) const
639  { return currentInputFilter->getSourcePos(pos); }
640  AsmSourcePos getSourcePos(const char* linePtr) const
641  { return getSourcePos(linePtr-line); }
642 
643  void printWarning(const AsmSourcePos& pos, const char* message);
644  void printError(const AsmSourcePos& pos, const char* message);
645 
646  void printWarning(const char* linePtr, const char* message)
647  { printWarning(getSourcePos(linePtr), message); }
648  void printError(const char* linePtr, const char* message)
649  { printError(getSourcePos(linePtr), message); }
650 
651  void printWarning(LineCol lineCol, const char* message)
652  { printWarning(getSourcePos(lineCol), message); }
653  void printError(LineCol lineCol, const char* message)
654  { printError(getSourcePos(lineCol), message); }
655 
656  LineCol translatePos(const char* linePtr) const
657  { return currentInputFilter->translatePos(linePtr-line); }
658  LineCol translatePos(size_t pos) const
659  { return currentInputFilter->translatePos(pos); }
660 
661  bool parseLiteral(uint64_t& value, const char*& linePtr);
662  bool parseString(std::string& outString, const char*& linePtr);
663 
664  enum class ParseState
665  {
666  FAILED = 0,
667  PARSED,
668  MISSING // missing element
669  };
670 
674  ParseState parseSymbol(const char*& linePtr, AsmSymbolEntry*& entry,
675  bool localLabel = true, bool dontCreateSymbol = false);
676  bool skipSymbol(const char*& linePtr);
677 
678  bool setSymbol(AsmSymbolEntry& symEntry, uint64_t value, cxuint sectionId);
679 
680  bool assignSymbol(const CString& symbolName, const char* symbolPlace,
681  const char* linePtr, bool reassign = true, bool baseExpr = false);
682 
683  bool assignOutputCounter(const char* symbolPlace, uint64_t value, cxuint sectionId,
684  cxbyte fillValue = 0);
685 
686  void parsePseudoOps(const CString& firstName, const char* stmtPlace,
687  const char* linePtr);
688 
690  bool skipClauses(bool exitm = false);
691  bool putMacroContent(RefPtr<AsmMacro> macro);
692  bool putRepetitionContent(AsmRepeat& repeat);
693 
694  void initializeOutputFormat();
695 
696  bool pushClause(const char* string, AsmClauseType clauseType)
697  {
698  bool included; // to ignore
699  return pushClause(string, clauseType, true, included);
700  }
701  bool pushClause(const char* string, AsmClauseType clauseType,
702  bool satisfied, bool& included);
703  // return false when failed (for example no clauses)
704  bool popClause(const char* string, AsmClauseType clauseType);
705 
707  bool includeFile(const char* pseudoOpPlace, const std::string& filename);
708 
709  ParseState makeMacroSubstitution(const char* string);
710 
711  bool parseMacroArgValue(const char*& linePtr, std::string& outStr);
712 
713  void putData(size_t size, const cxbyte* data)
714  {
715  AsmSection& section = sections[currentSection];
716  section.content.insert(section.content.end(), data, data+size);
717  currentOutPos += size;
718  }
719 
720  cxbyte* reserveData(size_t size, cxbyte fillValue = 0);
721 
722  void goToMain(const char* pseudoOpPlace);
723  void goToKernel(const char* pseudoOpPlace, const char* kernelName);
724  void goToSection(const char* pseudoOpPlace, const char* sectionName, uint64_t align=0);
725  void goToSection(const char* pseudoOpPlace, const char* sectionName,
726  AsmSectionType type, Flags flags, uint64_t align=0);
727  void goToSection(const char* pseudoOpPlace, cxuint sectionId, uint64_t align=0);
728 
729  void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos& pos,
730  cxbyte signess = WS_BOTH);
731 
732  bool checkReservedName(const CString& name);
733 
734  bool isAddressableSection() const
735  {
736  return currentSection==ASMSECT_ABS ||
737  (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
738  }
739  bool isWriteableSection() const
740  {
741  return currentSection!=ASMSECT_ABS &&
742  (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
743  }
744  bool isResolvableSection() const
745  {
746  return currentSection==ASMSECT_ABS ||
747  (sections[currentSection].flags & ASMSECT_UNRESOLVABLE) == 0;
748  }
749  bool isResolvableSection(cxuint sectionId) const
750  {
751  return sectionId==ASMSECT_ABS ||
752  (sections[sectionId].flags & ASMSECT_UNRESOLVABLE) == 0;
753  }
754 
755 protected:
757  bool readLine();
758 public:
760 
769  explicit Assembler(const CString& filename, std::istream& input, Flags flags = 0,
772  std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
773 
775 
783  explicit Assembler(const Array<CString>& filenames, Flags flags = 0,
786  std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
788  ~Assembler();
789 
791  bool assemble();
792 
794  void writeBinary(const char* filename) const;
796  void writeBinary(std::ostream& outStream) const;
798  void writeBinary(Array<cxbyte>& array) const;
799 
801  uint32_t getDriverVersion() const
802  { return driverVersion; }
804  void setDriverVersion(uint32_t driverVersion)
805  { this->driverVersion = driverVersion; }
806 
809  { return deviceType; }
811  void setDeviceType(const GPUDeviceType deviceType)
812  { this->deviceType = deviceType; }
815  { return format; }
818  { format = binFormat; }
820  bool is64Bit() const
821  { return _64bit; }
823  void set64Bit(bool this64Bit)
824  { _64bit = this64Bit; }
826  Flags getFlags() const
827  { return flags; }
829  void setFlags(Flags flags)
830  { this->flags = flags; }
832  const std::vector<CString>& getIncludeDirs() const
833  { return includeDirs; }
835  void addIncludeDir(const CString& includeDir);
837  const AsmSymbolMap& getSymbolMap() const
838  { return symbolMap; }
840  const std::vector<AsmSection>& getSections() const
841  { return sections; }
843  const KernelMap& getKernelMap() const
844  { return kernelMap; }
846  const std::vector<AsmKernel>& getKernels() const
847  { return kernels; }
848 
850  bool isAbsoluteSymbol(const AsmSymbol& symbol) const;
851 
853  void addInitialDefSym(const CString& symName, uint64_t value);
854 
857  { return formatHandler; }
858 };
859 
860 inline void ISAAssembler::printWarning(const char* linePtr, const char* message)
861 { return assembler.printWarning(linePtr, message); }
862 
863 inline void ISAAssembler::printError(const char* linePtr, const char* message)
864 { return assembler.printError(linePtr, message); }
865 
866 inline void ISAAssembler::printWarningForRange(cxuint bits, uint64_t value,
867  const AsmSourcePos& pos, cxbyte signess)
868 { return assembler.printWarningForRange(bits, value, pos, signess); }
869 
870 inline void ISAAssembler::printWarning(const AsmSourcePos& sourcePos, const char* message)
871 { return assembler.printWarning(sourcePos, message); }
872 
873 inline void ISAAssembler::printError(const AsmSourcePos& sourcePos, const char* message)
874 { return assembler.printError(sourcePos, message); }
875 
876 };
877 
878 #endif
AsmRepeatInputFilter or AsmIRPInputFilter.
void undefine()
make symbol as undefined
bool is64Bit() const
get bitness
Definition: Assembler.h:820
void set64Bit(bool this64Bit)
set bitness
Definition: Assembler.h:823
virtual void assemble(const CString &mnemonic, const char *mnemPlace, const char *linePtr, const char *lineEnd, std::vector< cxbyte > &output)=0
assemble single line
common definitions for assembler and disassembler
main class of assembler
Definition: Assembler.h:559
AsmExprTargetType type
type of target
Definition: Assembler.h:297
enable all warnings for assembler
Definition: Assembler.h:48
virtual bool parseRegisterRange(const char *&linePtr, cxuint &regStart, cxuint &regEnd)=0
parse register range
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: Assembler.h:350
AsmSymbolEntry * symbol
symbol
Definition: Assembler.h:343
AsmSymbol(bool _onceDefined=false)
empty constructor
Definition: Assembler.h:255
kernel entry structure
Definition: Assembler.h:543
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
void assemble(const CString &mnemonic, const char *mnemPlace, const char *linePtr, const char *lineEnd, std::vector< cxbyte > &output)
assemble single line
Flags getFlags() const
get flags
Definition: Assembler.h:826
plus (nothing)
AsmExpression * expression
expression of symbol (if not resolved)
Definition: Assembler.h:249
bool operator==(const AsmExprSymbolOccurrence &b) const
comparison operator
Definition: Assembler.h:230
cxuint kernelId
kernel id (optional)
Definition: Assembler.h:520
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:567
AMD CATALYST format.
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:808
none operation
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:840
ISAAssembler(Assembler &assembler)
constructor
ColNo colNo
column number, for macro substitution and IRP points to column preprocessed line
Definition: AsmSource.h:49
AsmSymbolEntry * symbol
symbol entry (if ASMXTGT_SYMBOL)
Definition: Assembler.h:300
cxuint regRange
if symbol is register range
Definition: Assembler.h:246
target for assembler expression
Definition: Assembler.h:295
void setDriverVersion(uint32_t driverVersion)
set AMD driver version
Definition: Assembler.h:804
~AsmSymbol()
destructor
Definition: Assembler.h:489
AsmClauseType
type of clause
Definition: Assembler.h:533
Assembler & assembler
assembler
Definition: Assembler.h:88
AsmExprOp
assembler expression operator
Definition: Assembler.h:171
assembler section
Definition: Assembler.h:517
assembler macro input filter (for macro filtering)
Definition: AsmSource.h:423
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:563
AsmExprTarget()
empty constructor
Definition: Assembler.h:307
handles raw code format
Definition: AsmFormats.h:175
assembler input layout filter
Definition: AsmSource.h:389
static AsmExprTarget symbolTarget(AsmSymbolEntry *entry)
make symbol target for expression
Definition: Assembler.h:315
cxbyte info
ELF symbol info.
Definition: Assembler.h:239
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:863
const AsmExprTarget & getTarget() const
get targer of expression
Definition: Assembler.h:457
signed (arithmetic) shift right
static bool isUnaryOp(AsmExprOp op)
return true if is unary op
Definition: Assembler.h:451
AsmSourcePos sourcePos
source position of definition
Definition: Assembler.h:546
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:860
size_t getSymOccursNum() const
get number of symbol occurrences in expression
Definition: Assembler.h:460
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:137
assembler symbol occurrence in expression
Definition: Assembler.h:223
Flags regFlags
define what extra register must be included
Definition: Assembler.h:139
cxuint sectionId
section id
Definition: Assembler.h:238
void addOccurrenceInExpr(AsmExpression *expr, size_t argIndex, size_t opIndex)
adds occurrence in expression
Definition: Assembler.h:276
virtual void setAllocatedRegisters(const cxuint *regs=nullptr, Flags regFlags=0)=0
set allocated registers (if regs is null then reset them)
ColNo colNo
column number
Definition: AsmSource.h:157
AsmSymbol(cxuint _sectionId, uint64_t _value, bool _onceDefined=false)
constructor with value and section id
Definition: Assembler.h:267
bool readLine()
helper for testing
cxuint resolving
helper
Definition: Assembler.h:243
cxuint RelocType
relocation type
Definition: Commons.h:32
less or equal than
const cxuint * getAllocatedRegisters(size_t &regTypesNum, Flags &regFlags) const
get allocated register numbers after assemblying
cxuint onceDefined
symbol can be only once defined (likes labels)
Definition: Assembler.h:242
unsigned modulo
void setTarget(AsmExprTarget _target)
set target of expression
Definition: Assembler.h:400
virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType)=0
return true if expresion of target fit to value with specified bits
bool isAbsoluteSymbol(const AsmSymbol &symbol) const
returns true if symbol contains absolute value
virtual bool checkMnemonic(const CString &mnemonic) const =0
check if name is mnemonic
line and column
Definition: AsmSource.h:44
bool isEmpty() const
return true if expression is empty
Definition: Assembler.h:393
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: Assembler.h:290
an assembler formats
cxuint sectionId
sectionId
Definition: Assembler.h:502
void clearOccurrencesInExpr()
clear list of occurrences in expression
LineNo lineNo
line number of top-most source
Definition: AsmSource.h:156
cxuint hasValue
symbol is defined
Definition: Assembler.h:241
size_t offset
offset of relocation
Definition: Assembler.h:340
bool resolveCode(const AsmSourcePos &sourcePos, cxuint targetSectionId, cxbyte *sectionData, size_t offset, AsmExprTargetType targetType, cxuint sectionId, uint64_t value)
resolve code with location, target and value
void setFlags(Flags flags)
set flags
Definition: Assembler.h:829
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:553
static AsmExpression * parse(Assembler &assembler, size_t &linePos, bool makeBase=false, bool dontResolveSymbolsLater=false)
parse expression. By default, also gets values of symbol or creates them
uint64_t value
value of symbol
Definition: Assembler.h:247
AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
constructor to create custom target
Definition: Assembler.h:310
virtual const cxuint * getAllocatedRegisters(size_t &regTypesNum, Flags &regFlags) const =0
get allocated register numbers after assemblying
bool checkMnemonic(const CString &mnemonic) const
check if name is mnemonic
assembler relocation
Definition: Assembler.h:337
logical shift irght
size_t opIndex
operator index
Definition: Assembler.h:227
unsigned less or equal
const char * name
name of kernel
Definition: Assembler.h:545
AsmSectionType
assembler section type
Definition: AsmFormats.h:44
assdembler format handler
Definition: AsmFormats.h:109
RefPtr< const AsmSource > getSource() const
get current source after reading line
Definition: AsmSource.h:368
absolute section id
Definition: AsmFormats.h:73
bool assemble()
main routine to assemble code
Assembler(const CString &filename, std::istream &input, Flags flags=0, BinaryFormat format=BinaryFormat::AMD, GPUDeviceType deviceType=GPUDeviceType::CAPE_VERDE, std::ostream &msgStream=std::cerr, std::ostream &printStream=std::cout)
constructor with filename and input stream
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:832
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:138
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:555
greater or equal than
AsmSymbolEntry * symbol
if symbol
Definition: Assembler.h:498
void addInitialDefSym(const CString &symName, uint64_t value)
add initiali defsyms
absolute symbol without defined value
LineCol translatePos(size_t position) const
translate position to line number and column number
void substituteOccurrence(AsmExprSymbolOccurrence occurrence, uint64_t value, cxuint sectionId=ASMSECT_ABS)
substitute occurrence in expression by value
Definition: Assembler.h:506
void removeOccurrenceInExpr(AsmExpression *expr, size_t argIndex, size_t opIndex)
remove occurrence in expression
cxuint base
with base expression
Definition: Assembler.h:244
static bool isArg(AsmExprOp op)
return true if is argument op
Definition: Assembler.h:448
AsmClauseType type
type of clause
Definition: Assembler.h:552
bool unrefSymOccursNum()
unreference symbol occurrences in expression (used internally)
Definition: Assembler.h:466
~AsmExpression()
destructor
only for running tests
Definition: Assembler.h:51
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:843
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:814
static bool isBinaryOp(AsmExprOp op)
return true if is binary op
Definition: Assembler.h:454
bool parseRegisterRange(const char *&linePtr, cxuint &regStart, cxuint &regEnd)
parse register range
uint64_t size
size of symbol
Definition: Assembler.h:248
size_t getSize() const
get section's size
Definition: Assembler.h:528
binary negation
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:837
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: Assembler.h:61
uint64_t size
section size
Definition: Assembler.h:524
std::vector< AsmExprSymbolOccurrence > occurrencesInExprs
Definition: Assembler.h:252
macro substitution
std::vector< cxbyte > content
content of section
Definition: Assembler.h:525
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: Assembler.h:292
Flags flags
section flags
Definition: Assembler.h:522
void addIncludeDir(const CString &includeDir)
adds include directory
uint64_t value
value
Definition: Assembler.h:499
cxuint refCount
reference counter (for internal use only)
Definition: Assembler.h:237
GPUDeviceType
type of GPU device
Definition: GPUId.h:38
void writeBinary(const char *filename) const
write binary to file
void setAllocatedRegisters(const cxuint *regs, Flags regFlags)
set allocated registers (if regs is null then reset them)
size_t offset
offset of destination
Definition: Assembler.h:303
~Assembler()
destructor
size_t argIndex
argument index
Definition: Assembler.h:226
cxuint sectionId
section id where relocation is present
Definition: Assembler.h:339
cxuint sectionId
section id of destination
Definition: Assembler.h:302
void fillAlignment(size_t size, cxbyte *output)
fill alignment when value is not given
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:811
utilities for other libraries and programs
const Array< AsmExprOp > & getOps() const
get operators list
Definition: Assembler.h:473
unsigned division
RelocType type
relocation type
Definition: Assembler.h:341
target is 32-bit word
Definition: Assembler.h:68
uint64_t alignment
section alignment
Definition: Assembler.h:523
static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
make n-bit word target for expression
Definition: Assembler.h:325
AsmExpression * createForSnapshot(const AsmSourcePos *exprSourcePos) const
helper to create symbol snapshot. Creates initial expression for symbol snapshot
GCN arch assembler.
Definition: Assembler.h:132
bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType)
return true if expresion of target fit to value with specified bits
virtual bool resolveCode(const AsmSourcePos &sourcePos, cxuint targetSectionId, cxbyte *sectionData, size_t offset, AsmExprTargetType targetType, cxuint sectionId, uint64_t value)=0
resolve code with location, target and value
virtual ~ISAAssembler()
destructor
AsmSectionType type
type of section
Definition: Assembler.h:521
register pool numbers
Definition: Assembler.h:136
assembler's clause (if,else,macro,rept)
Definition: Assembler.h:550
AsmSourcePos getSourcePos(size_t position) const
get source position after reading line
Definition: AsmSource.h:375
AsmSymbol(AsmExpression *expr, bool _onceDefined=false, bool _base=false)
constructor with expression
Definition: Assembler.h:261
virtual void fillAlignment(size_t size, cxbyte *output)=0
fill alignment when value is not given
LineNo lineNo
line number
Definition: AsmSource.h:46
struct CLRX::AsmExprArg::@13 relValue
relative value (with section)
cxbyte other
ELF symbol other.
Definition: Assembler.h:240
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:801
target is 64-bit word
Definition: Assembler.h:69
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:856
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:866
target is byte
Definition: Assembler.h:66
handles GalliumCompute format
Definition: AsmFormats.h:349
const char * name
section name
Definition: Assembler.h:519
an assembler sources handling
assembler input filter for reading lines
Definition: AsmSource.h:326
cxuint relSectionId
section for which relocation is defined
Definition: Assembler.h:344
assembler symbol structure
Definition: Assembler.h:235
RefPtr< const AsmMacroSubst > getMacroSubst() const
get current macro substitution after reading line
Definition: AsmSource.h:371
bool isDefined() const
return true if symbol defined (have value or expression)
Definition: Assembler.h:285
const AsmExprArg * getArgs() const
get argument list
Definition: Assembler.h:476
handles AMD Catalyst format
Definition: AsmFormats.h:201
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:267
~GCNAssembler()
destructor
cxuint snapshot
if symbol is snapshot
Definition: Assembler.h:245
unsigned less or equal
size_t hasRelativeSymOccurs() const
get number of symbol occurrences in expression
Definition: Assembler.h:463
std::unordered_map< CString, RefPtr< const AsmMacro > > MacroMap
macro map type
Definition: Assembler.h:565
section is unresolvable
Definition: AsmFormats.h:84
ISA assembler class.
Definition: Assembler.h:85
uint64_t addend
addend
Definition: Assembler.h:346
GCNAssembler(Assembler &assembler)
constructor
AsmExpression * expression
target expression pointer
Definition: Assembler.h:225
bool evaluate(Assembler &assembler, uint64_t &value, cxuint &sectionId) const
try to evaluate expression
Definition: Assembler.h:410
assembler expression argument
Definition: Assembler.h:496
simple C-string container
Definition: CString.h:38
all flags
Definition: Assembler.h:52
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:817
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:554
target is symbol
Definition: Assembler.h:65
target is 16-bit word
Definition: Assembler.h:67
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:846
const AsmSourcePos & getSourcePos() const
get source position
Definition: Assembler.h:479
assembler source position
Definition: AsmSource.h:152