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-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_BUGGYFPLIT = 8, // buggy handling of fpliterals (including fp constants)
52  ASM_TESTRUN = (1U<<31),
53  ASM_ALL = FLAGS_ALL&~(ASM_TESTRUN|ASM_BUGGYFPLIT)
54 };
55 
56 enum: cxbyte {
57  WS_UNSIGNED = 0, // only unsigned
58  WS_BOTH = 1, // both signed and unsigned range checking
59 };
60 
62 typedef cxbyte AsmExprTargetType;
63 
64 enum : AsmExprTargetType
65 {
71 
72  GCNTGT_LITIMM = 16,
73  GCNTGT_SOPKSIMM16,
74  GCNTGT_SOPJMP,
75  GCNTGT_SMRDOFFSET,
76  GCNTGT_DSOFFSET16,
77  GCNTGT_DSOFFSET8_0,
78  GCNTGT_DSOFFSET8_1,
79  GCNTGT_MXBUFOFFSET,
80  GCNTGT_SMEMOFFSET,
81  GCNTGT_SOPCIMM8,
82  GCNTGT_SMEMIMM
83 };
84 
85 struct AsmRegVar;
86 
89 {
90 protected:
92 
94  void printWarning(const char* linePtr, const char* message);
96  void printError(const char* linePtr, const char* message);
98  void printWarning(const AsmSourcePos& sourcePos, const char* message);
100  void printError(const AsmSourcePos& sourcePos, const char* message);
102  void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos& pos,
103  cxbyte signess = WS_BOTH);
105  explicit ISAAssembler(Assembler& assembler);
106 public:
108  virtual ~ISAAssembler();
109 
111  virtual void assemble(const CString& mnemonic, const char* mnemPlace,
112  const char* linePtr, const char* lineEnd, std::vector<cxbyte>& output) = 0;
114  virtual bool resolveCode(const AsmSourcePos& sourcePos, cxuint targetSectionId,
115  cxbyte* sectionData, size_t offset, AsmExprTargetType targetType,
116  cxuint sectionId, uint64_t value) = 0;
118  virtual bool checkMnemonic(const CString& mnemonic) const = 0;
120  virtual void setAllocatedRegisters(const cxuint* regs = nullptr,
121  Flags regFlags = 0) = 0;
123  virtual const cxuint* getAllocatedRegisters(size_t& regTypesNum,
124  Flags& regFlags) const = 0;
126  virtual void fillAlignment(size_t size, cxbyte* output) = 0;
128  virtual bool parseRegisterRange(const char*& linePtr, cxuint& regStart,
129  cxuint& regEnd, const AsmRegVar*& regVar) = 0;
131  virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType) = 0;
133  virtual bool parseRegisterType(const char*& linePtr,
134  const char* end, cxuint& type) = 0;
135 };
136 
139 {
140 public:
142  struct Regs {
143  cxuint sgprsNum;
144  cxuint vgprsNum;
146  };
147 private:
148  union {
149  Regs regs;
150  cxuint regTable[2];
151  };
152  uint16_t curArchMask;
153 public:
155  explicit GCNAssembler(Assembler& assembler);
157  ~GCNAssembler();
158 
159  void assemble(const CString& mnemonic, const char* mnemPlace, const char* linePtr,
160  const char* lineEnd, std::vector<cxbyte>& output);
161  bool resolveCode(const AsmSourcePos& sourcePos, cxuint targetSectionId,
162  cxbyte* sectionData, size_t offset, AsmExprTargetType targetType,
163  cxuint sectionId, uint64_t value);
164  bool checkMnemonic(const CString& mnemonic) const;
165  void setAllocatedRegisters(const cxuint* regs, Flags regFlags);
166  const cxuint* getAllocatedRegisters(size_t& regTypesNum, Flags& regFlags) const;
167  void fillAlignment(size_t size, cxbyte* output);
168  bool parseRegisterRange(const char*& linePtr, cxuint& regStart, cxuint& regEnd,
169  const AsmRegVar*& regVar);
170  bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType);
171  bool parseRegisterType(const char*& linePtr, const char* end, cxuint& type);
172 };
173 
174 /*
175  * assembler expressions
176  */
177 
179 enum class AsmExprOp : cxbyte
180 {
181  ARG_VALUE = 0,
182  ARG_SYMBOL = 1,
183  NEGATE = 2,
184  BIT_NOT,
185  LOGICAL_NOT,
186  PLUS,
187  ADDITION,
188  SUBTRACT,
189  MULTIPLY,
190  DIVISION,
192  MODULO,
193  SIGNED_MODULO,
194  BIT_AND,
195  BIT_OR, //< bitwise OR
196  BIT_XOR,
197  BIT_ORNOT,
198  SHIFT_LEFT,
199  SHIFT_RIGHT,
201  LOGICAL_AND,
202  LOGICAL_OR,
203  EQUAL,
204  NOT_EQUAL,
205  LESS,
206  LESS_EQ,
207  GREATER,
208  GREATER_EQ,
209  BELOW,
210  BELOW_EQ,
211  ABOVE,
212  ABOVE_EQ,
213  CHOICE,
214  CHOICE_START,
215  FIRST_ARG = ARG_VALUE,
216  LAST_ARG = ARG_SYMBOL,
217  FIRST_UNARY = NEGATE,
218  LAST_UNARY = PLUS,
221  NONE = 0xff
222 };
223 
224 struct AsmExprTarget;
225 
226 union AsmExprArg;
227 
228 class AsmExpression;
229 
232 {
234  size_t argIndex;
235  size_t opIndex;
236 
238  bool operator==(const AsmExprSymbolOccurrence& b) const
239  { return expression==b.expression && opIndex==b.opIndex && argIndex==b.argIndex; }
240 };
241 
242 struct AsmRegVar;
243 
245 struct AsmSymbol
246 {
247  cxuint refCount;
248  cxuint sectionId;
249  cxbyte info;
250  cxbyte other;
251  cxuint hasValue:1;
252  cxuint onceDefined:1;
253  cxuint resolving:1;
254  cxuint base:1;
255  cxuint snapshot:1;
256  cxuint regRange:1;
257  uint64_t value;
258  uint64_t size;
259  union {
261  AsmRegVar* var;
262  };
263 
265  std::vector<AsmExprSymbolOccurrence> occurrencesInExprs;
266 
268  explicit AsmSymbol(bool _onceDefined = false) :
269  refCount(1), sectionId(ASMSECT_ABS), info(0), other(0), hasValue(false),
270  onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
271  regRange(false), value(0), size(0), expression(nullptr)
272  { }
274  explicit AsmSymbol(AsmExpression* expr, bool _onceDefined = false, bool _base = false) :
275  refCount(1), sectionId(ASMSECT_ABS), info(0), other(0), hasValue(false),
276  onceDefined(_onceDefined), resolving(false), base(_base),
277  snapshot(false), regRange(false), value(0), size(0), expression(expr)
278  { }
280  explicit AsmSymbol(cxuint _sectionId, uint64_t _value, bool _onceDefined = false) :
281  refCount(1), sectionId(_sectionId), info(0), other(0), hasValue(true),
282  onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
283  regRange(false), value(_value), size(0), expression(nullptr)
284  { }
286  ~AsmSymbol();
287 
289  void addOccurrenceInExpr(AsmExpression* expr, size_t argIndex, size_t opIndex)
290  { occurrencesInExprs.push_back({expr, argIndex, opIndex}); }
292  void removeOccurrenceInExpr(AsmExpression* expr, size_t argIndex, size_t opIndex);
294  void clearOccurrencesInExpr();
296  void undefine();
298  bool isDefined() const
299  { return hasValue || expression!=nullptr; }
300 };
301 
303 typedef std::unordered_map<CString, AsmSymbol> AsmSymbolMap;
305 typedef AsmSymbolMap::value_type AsmSymbolEntry;
306 
309 {
310  AsmExprTargetType type;
311  union
312  {
313  AsmSymbolEntry* symbol;
314  struct {
315  cxuint sectionId;
316  size_t offset;
317  };
318  };
321 
323  AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
324  : type(_type), sectionId(_sectionId), offset(_offset)
325  { }
326 
328  static AsmExprTarget symbolTarget(AsmSymbolEntry* entry)
329  {
330  AsmExprTarget target;
331  target.type = ASMXTGT_SYMBOL;
332  target.symbol = entry;
333  return target;
334  }
335 
337  template<typename T>
338  static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
339  {
340  AsmExprTarget target;
341  target.type = (sizeof(T)==1) ? ASMXTGT_DATA8 : (sizeof(T)==2) ? ASMXTGT_DATA16 :
342  (sizeof(T)==4) ? ASMXTGT_DATA32 : ASMXTGT_DATA64;
343  target.sectionId = sectionId;
344  target.offset = offset;
345  return target;
346  }
347 };
348 
351 {
352  cxuint sectionId;
353  size_t offset;
355  union {
356  AsmSymbolEntry* symbol;
357  cxuint relSectionId;
358  };
359  uint64_t addend;
360 };
361 
364 {
365 private:
366  class TempSymbolSnapshotMap;
367 
368  AsmExprTarget target;
369  AsmSourcePos sourcePos;
370  size_t symOccursNum;
371  bool relativeSymOccurs;
372  bool baseExpr;
373  Array<AsmExprOp> ops;
374  std::unique_ptr<LineCol[]> messagePositions;
375  std::unique_ptr<AsmExprArg[]> args;
376 
377  AsmSourcePos getSourcePos(size_t msgPosIndex) const
378  {
379  AsmSourcePos pos = sourcePos;
380  pos.lineNo = messagePositions[msgPosIndex].lineNo;
381  pos.colNo = messagePositions[msgPosIndex].colNo;
382  return pos;
383  }
384 
385  static bool makeSymbolSnapshot(Assembler& assembler,
386  TempSymbolSnapshotMap* snapshotMap, const AsmSymbolEntry& symEntry,
387  AsmSymbolEntry*& outSymEntry, const AsmSourcePos* topParentSourcePos);
388 
389  AsmExpression();
390  void setParams(size_t symOccursNum, bool relativeSymOccurs,
391  size_t _opsNum, const AsmExprOp* ops, size_t opPosNum, const LineCol* opPos,
392  size_t argsNum, const AsmExprArg* args, bool baseExpr = false);
393 public:
395  AsmExpression(const AsmSourcePos& pos, size_t symOccursNum, bool relativeSymOccurs,
396  size_t opsNum, size_t opPosNum, size_t argsNum, bool baseExpr = false);
398  AsmExpression(const AsmSourcePos& pos, size_t symOccursNum, bool relativeSymOccurs,
399  size_t opsNum, const AsmExprOp* ops, size_t opPosNum,
400  const LineCol* opPos, size_t argsNum, const AsmExprArg* args,
401  bool baseExpr = false);
403  ~AsmExpression();
404 
406  bool isEmpty() const
407  { return ops.empty(); }
408 
410  AsmExpression* createForSnapshot(const AsmSourcePos* exprSourcePos) const;
411 
413  void setTarget(AsmExprTarget _target)
414  { target = _target; }
415 
417 
423  bool evaluate(Assembler& assembler, uint64_t& value, cxuint& sectionId) const
424  { return evaluate(assembler, 0, ops.size(), value, sectionId); }
425 
427 
435  bool evaluate(Assembler& assembler, size_t opStart, size_t opEnd,
436  uint64_t& value, cxuint& sectionId) const;
437 
439 
446  static AsmExpression* parse(Assembler& assembler, size_t& linePos,
447  bool makeBase = false, bool dontResolveSymbolsLater = false);
448 
450 
457  static AsmExpression* parse(Assembler& assembler, const char*& linePtr,
458  bool makeBase = false, bool dontResolveSymbolsLater = false);
459 
461  static bool isArg(AsmExprOp op)
462  { return (AsmExprOp::FIRST_ARG <= op && op <= AsmExprOp::LAST_ARG); }
464  static bool isUnaryOp(AsmExprOp op)
465  { return (AsmExprOp::FIRST_UNARY <= op && op <= AsmExprOp::LAST_UNARY); }
467  static bool isBinaryOp(AsmExprOp op)
468  { return (AsmExprOp::FIRST_BINARY <= op && op <= AsmExprOp::LAST_BINARY); }
470  const AsmExprTarget& getTarget() const
471  { return target; }
473  size_t getSymOccursNum() const
474  { return symOccursNum; }
476  size_t hasRelativeSymOccurs() const
477  { return relativeSymOccurs; }
480  { return --symOccursNum!=0; }
481 
483  void substituteOccurrence(AsmExprSymbolOccurrence occurrence, uint64_t value,
484  cxuint sectionId = ASMSECT_ABS);
486  const Array<AsmExprOp>& getOps() const
487  { return ops; }
489  const AsmExprArg* getArgs() const
490  { return args.get(); }
492  const AsmSourcePos& getSourcePos() const
493  { return sourcePos; }
494 
495  size_t toTop(size_t opIndex) const;
496 
498  static bool makeSymbolSnapshot(Assembler& assembler, const AsmSymbolEntry& symEntry,
499  AsmSymbolEntry*& outSymEntry, const AsmSourcePos* parentExprSourcePos);
500 };
501 
503 {
504  if (base) delete expression; // if symbol with base expresion
505  clearOccurrencesInExpr();
506 }
507 
510 {
511  AsmSymbolEntry* symbol;
512  uint64_t value;
513  struct {
514  uint64_t value;
515  cxuint sectionId;
516  } relValue;
517 };
518 
520  uint64_t value, cxuint sectionId)
521 {
522  ops[occurrence.opIndex] = AsmExprOp::ARG_VALUE;
523  args[occurrence.argIndex].relValue.value = value;
524  args[occurrence.argIndex].relValue.sectionId = sectionId;
525  if (sectionId != ASMSECT_ABS)
526  relativeSymOccurs = true;
527 }
528 
529 typedef cxbyte AsmRegField;
530 
531 enum : AsmRegField
532 {
533  GCNFIELD_SSRC0 = 0,
534  GCNFIELD_SSRC1,
535  GCNFIELD_SDST,
536  GCNFIELD_SMRD_SBASE,
537  GCNFIELD_SMRD_SDST,
538  GCNFIELD_SMRD_SOFFSET,
539  GCNFIELD_VOP_SRC0,
540  GCNFIELD_VOP_SRC1,
541  GCNFIELD_VOP_VDST,
542  GCNFIELD_VOP3_VSRC0,
543  GCNFIELD_VOP3_SRC1,
544  GCNFIELD_VOP3_VDST,
545  GCNFIELD_VOP3_SSRC,
546  GCNFIELD_VOP3_SDST,
547  GCNFIELD_VINTRP_VSRC0,
548  GCNFIELD_VINTRP_VDST,
549  GCNFIELD_DS_ADDR,
550  GCNFIELD_DS_DATA0,
551  GCNFIELD_DS_DATA1,
552  GCNFIELD_DS_VDST,
553  GCNFIELD_M_VADDR,
554  GCNFIELD_M_VDATA,
555  GCNFIELD_M_SRSRC,
556  GCNFIELD_MIMG_SSAMP,
557  GCNFIELD_M_SOFFSET,
558  GCNFIELD_EXP_VSRC0,
559  GCNFIELD_EXP_VSRC1,
560  GCNFIELD_EXP_VSRC2,
561  GCNFIELD_EXP_VSRC3,
562  GCNFIELD_FLAT_ADDR,
563  GCNFIELD_FLAT_DATA,
564  GCNFIELD_FLAT_VDST,
565  GCNFIELD_DPPSDWA_SRC0,
566  GCNFIELD_SMEM_SBASE,
567  GCNFIELD_SMEM_SDATA,
568  GCNFIELD_SMEM_OFFSET,
569  ASMFIELD_NONE = 255
570 };
571 
572 enum : cxbyte
573 {
574  GCNREGTYPE_SGPR,
575  GCNREGTYPE_VGPR
576 };
577 
578 struct AsmRegVar
579 {
580  cxuint type; // scalar/vector/other
581  uint16_t size; // in regs
582 };
583 
585 {
586  size_t offset;
587  AsmRegField regField;
588  uint16_t rstart, rend;
589  bool read;
590  bool write;
591  cxbyte align;
592  const AsmRegVar* regVar; // if null, then usage of called register
593 };
594 
597 {
598  const char* name;
599  cxuint kernelId;
602  uint64_t alignment;
603  uint64_t size;
604  std::vector<cxbyte> content;
605 
607  std::unordered_map<CString, AsmRegVar> regVars;
609  std::vector<AsmVarUsage> regVarUsages;
610 
611  bool addRegVar(const CString& name, const AsmRegVar& var);
612 
613  bool getRegVar(const CString& name, const AsmRegVar*& regVar) const;
614 
615  void addVarUsage(const AsmVarUsage& varUsage)
616  { regVarUsages.push_back(varUsage); }
617 
619  size_t getSize() const
620  { return ((flags&ASMSECT_WRITEABLE) != 0) ? content.size() : size; }
621 };
622 
624 enum class AsmClauseType
625 {
626  IF,
627  ELSEIF,
628  ELSE,
629  REPEAT,
630  MACRO
631 };
632 
634 struct AsmKernel
635 {
636  const char* name;
638 };
639 
641 struct AsmClause
642 {
647 };
648 
651 {
652 public:
654  typedef std::pair<CString, uint64_t> DefSym;
656  typedef std::unordered_map<CString, RefPtr<const AsmMacro> > MacroMap;
658  typedef std::unordered_map<CString, cxuint> KernelMap;
659 private:
660  friend class AsmStreamInputFilter;
661  friend class AsmMacroInputFilter;
662  friend class AsmExpression;
663  friend class AsmFormatHandler;
664  friend class AsmRawCodeHandler;
665  friend class AsmAmdHandler;
666  friend class AsmAmdCL2Handler;
667  friend class AsmGalliumHandler;
668  friend class AsmROCmHandler;
669  friend class ISAAssembler;
670 
671  friend struct AsmParseUtils; // INTERNAL LOGIC
672  friend struct AsmPseudoOps; // INTERNAL LOGIC
673  friend struct AsmGalliumPseudoOps; // INTERNAL LOGIC
674  friend struct AsmAmdPseudoOps; // INTERNAL LOGIC
675  friend struct AsmAmdCL2PseudoOps; // INTERNAL LOGIC
676  friend struct AsmROCmPseudoOps; // INTERNAL LOGIC
677  friend struct GCNAsmUtils; // INTERNAL LOGIC
678 
679  Array<CString> filenames;
680  BinaryFormat format;
681  GPUDeviceType deviceType;
682  uint32_t driverVersion;
683  bool _64bit;
684  bool good;
685  bool resolvingRelocs;
686  ISAAssembler* isaAssembler;
687  std::vector<DefSym> defSyms;
688  std::vector<CString> includeDirs;
689  std::vector<AsmSection> sections;
690  AsmSymbolMap symbolMap;
691  std::unordered_set<AsmSymbolEntry*> symbolSnapshots;
692  std::vector<AsmRelocation> relocations;
693  MacroMap macroMap;
694  KernelMap kernelMap;
695  std::vector<AsmKernel> kernels;
696  Flags flags;
697  uint64_t macroCount;
698  uint64_t localCount; // macro's local count
699  bool alternateMacro;
700  bool buggyFPLit;
701 
702  cxuint inclusionLevel;
703  cxuint macroSubstLevel;
704  cxuint repetitionLevel;
705  bool lineAlreadyRead; // if line already read
706 
707  size_t lineSize;
708  const char* line;
709  bool endOfAssembly;
710 
711  cxuint filenameIndex;
712  std::stack<AsmInputFilter*> asmInputFilters;
713  AsmInputFilter* currentInputFilter;
714 
715  std::ostream& messageStream;
716  std::ostream& printStream;
717 
718  AsmFormatHandler* formatHandler;
719 
720  std::stack<AsmClause> clauses;
721 
722  cxuint currentKernel;
723  cxuint& currentSection;
724  uint64_t& currentOutPos;
725 
726  AsmSourcePos getSourcePos(LineCol lineCol) const
727  {
728  return { currentInputFilter->getMacroSubst(), currentInputFilter->getSource(),
729  lineCol.lineNo, lineCol.colNo };
730  }
731 
732  AsmSourcePos getSourcePos(size_t pos) const
733  { return currentInputFilter->getSourcePos(pos); }
734  AsmSourcePos getSourcePos(const char* linePtr) const
735  { return getSourcePos(linePtr-line); }
736 
737  void printWarning(const AsmSourcePos& pos, const char* message);
738  void printError(const AsmSourcePos& pos, const char* message);
739 
740  void printWarning(const char* linePtr, const char* message)
741  { printWarning(getSourcePos(linePtr), message); }
742  void printError(const char* linePtr, const char* message)
743  { printError(getSourcePos(linePtr), message); }
744 
745  void printWarning(LineCol lineCol, const char* message)
746  { printWarning(getSourcePos(lineCol), message); }
747  void printError(LineCol lineCol, const char* message)
748  { printError(getSourcePos(lineCol), message); }
749 
750  LineCol translatePos(const char* linePtr) const
751  { return currentInputFilter->translatePos(linePtr-line); }
752  LineCol translatePos(size_t pos) const
753  { return currentInputFilter->translatePos(pos); }
754 
755  bool parseLiteral(uint64_t& value, const char*& linePtr);
756  bool parseString(std::string& outString, const char*& linePtr);
757 
758  enum class ParseState
759  {
760  FAILED = 0,
761  PARSED,
762  MISSING // missing element
763  };
764 
768  ParseState parseSymbol(const char*& linePtr, AsmSymbolEntry*& entry,
769  bool localLabel = true, bool dontCreateSymbol = false);
770  bool skipSymbol(const char*& linePtr);
771 
772  bool setSymbol(AsmSymbolEntry& symEntry, uint64_t value, cxuint sectionId);
773 
774  bool assignSymbol(const CString& symbolName, const char* symbolPlace,
775  const char* linePtr, bool reassign = true, bool baseExpr = false);
776 
777  bool assignOutputCounter(const char* symbolPlace, uint64_t value, cxuint sectionId,
778  cxbyte fillValue = 0);
779 
780  void parsePseudoOps(const CString& firstName, const char* stmtPlace,
781  const char* linePtr);
782 
784  bool skipClauses(bool exitm = false);
785  bool putMacroContent(RefPtr<AsmMacro> macro);
786  bool putRepetitionContent(AsmRepeat& repeat);
787 
788  void initializeOutputFormat();
789 
790  bool pushClause(const char* string, AsmClauseType clauseType)
791  {
792  bool included; // to ignore
793  return pushClause(string, clauseType, true, included);
794  }
795  bool pushClause(const char* string, AsmClauseType clauseType,
796  bool satisfied, bool& included);
797  // return false when failed (for example no clauses)
798  bool popClause(const char* string, AsmClauseType clauseType);
799 
801  bool includeFile(const char* pseudoOpPlace, const std::string& filename);
802 
803  ParseState makeMacroSubstitution(const char* string);
804 
805  bool parseMacroArgValue(const char*& linePtr, std::string& outStr);
806 
807  void putData(size_t size, const cxbyte* data)
808  {
809  AsmSection& section = sections[currentSection];
810  section.content.insert(section.content.end(), data, data+size);
811  currentOutPos += size;
812  }
813 
814  cxbyte* reserveData(size_t size, cxbyte fillValue = 0);
815 
816  void goToMain(const char* pseudoOpPlace);
817  void goToKernel(const char* pseudoOpPlace, const char* kernelName);
818  void goToSection(const char* pseudoOpPlace, const char* sectionName, uint64_t align=0);
819  void goToSection(const char* pseudoOpPlace, const char* sectionName,
820  AsmSectionType type, Flags flags, uint64_t align=0);
821  void goToSection(const char* pseudoOpPlace, cxuint sectionId, uint64_t align=0);
822 
823  void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos& pos,
824  cxbyte signess = WS_BOTH);
825 
826  bool checkReservedName(const CString& name);
827 
828  bool isAddressableSection() const
829  {
830  return currentSection==ASMSECT_ABS ||
831  (sections[currentSection].flags & ASMSECT_ADDRESSABLE) != 0;
832  }
833  bool isWriteableSection() const
834  {
835  return currentSection!=ASMSECT_ABS &&
836  (sections[currentSection].flags & ASMSECT_WRITEABLE) != 0;
837  }
838  bool isResolvableSection() const
839  {
840  return currentSection==ASMSECT_ABS ||
841  (sections[currentSection].flags & ASMSECT_UNRESOLVABLE) == 0;
842  }
843  bool isResolvableSection(cxuint sectionId) const
844  {
845  return sectionId==ASMSECT_ABS ||
846  (sections[sectionId].flags & ASMSECT_UNRESOLVABLE) == 0;
847  }
848 
849 protected:
851  bool readLine();
852 public:
854 
863  explicit Assembler(const CString& filename, std::istream& input, Flags flags = 0,
866  std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
867 
869 
877  explicit Assembler(const Array<CString>& filenames, Flags flags = 0,
880  std::ostream& msgStream = std::cerr, std::ostream& printStream = std::cout);
882  ~Assembler();
883 
885  bool assemble();
886 
888  void writeBinary(const char* filename) const;
890  void writeBinary(std::ostream& outStream) const;
892  void writeBinary(Array<cxbyte>& array) const;
893 
895  uint32_t getDriverVersion() const
896  { return driverVersion; }
898  void setDriverVersion(uint32_t driverVersion)
899  { this->driverVersion = driverVersion; }
900 
903  { return deviceType; }
905  void setDeviceType(const GPUDeviceType deviceType)
906  { this->deviceType = deviceType; }
909  { return format; }
912  { format = binFormat; }
914  bool is64Bit() const
915  { return _64bit; }
917  void set64Bit(bool this64Bit)
918  { _64bit = this64Bit; }
920  Flags getFlags() const
921  { return flags; }
923  void setFlags(Flags flags)
924  { this->flags = flags; }
926  const std::vector<CString>& getIncludeDirs() const
927  { return includeDirs; }
929  void addIncludeDir(const CString& includeDir);
931  const AsmSymbolMap& getSymbolMap() const
932  { return symbolMap; }
934  const std::vector<AsmSection>& getSections() const
935  { return sections; }
937  const KernelMap& getKernelMap() const
938  { return kernelMap; }
940  const std::vector<AsmKernel>& getKernels() const
941  { return kernels; }
942 
944  bool isAbsoluteSymbol(const AsmSymbol& symbol) const;
945 
947  void addInitialDefSym(const CString& symName, uint64_t value);
948 
951  { return formatHandler; }
952 };
953 
954 inline void ISAAssembler::printWarning(const char* linePtr, const char* message)
955 { return assembler.printWarning(linePtr, message); }
956 
957 inline void ISAAssembler::printError(const char* linePtr, const char* message)
958 { return assembler.printError(linePtr, message); }
959 
960 inline void ISAAssembler::printWarningForRange(cxuint bits, uint64_t value,
961  const AsmSourcePos& pos, cxbyte signess)
962 { return assembler.printWarningForRange(bits, value, pos, signess); }
963 
964 inline void ISAAssembler::printWarning(const AsmSourcePos& sourcePos, const char* message)
965 { return assembler.printWarning(sourcePos, message); }
966 
967 inline void ISAAssembler::printError(const AsmSourcePos& sourcePos, const char* message)
968 { return assembler.printError(sourcePos, message); }
969 
970 };
971 
972 #endif
AsmRepeatInputFilter or AsmIRPInputFilter.
bool is64Bit() const
get bitness
Definition: Assembler.h:914
void set64Bit(bool this64Bit)
set bitness
Definition: Assembler.h:917
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:650
AsmExprTargetType type
type of target
Definition: Assembler.h:310
enable all warnings for assembler
Definition: Assembler.h:48
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: Assembler.h:363
AsmSymbolEntry * symbol
symbol
Definition: Assembler.h:356
AsmSymbol(bool _onceDefined=false)
empty constructor
Definition: Assembler.h:268
kernel entry structure
Definition: Assembler.h:634
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
Flags getFlags() const
get flags
Definition: Assembler.h:920
plus (nothing)
AsmExpression * expression
expression of symbol (if not resolved)
Definition: Assembler.h:260
bool operator==(const AsmExprSymbolOccurrence &b) const
comparison operator
Definition: Assembler.h:238
assembler repeat
Definition: AsmSource.h:238
cxuint kernelId
kernel id (optional)
Definition: Assembler.h:599
std::unordered_map< CString, cxuint > KernelMap
kernel map type
Definition: Assembler.h:658
AMD CATALYST format.
GPUDeviceType getDeviceType() const
get GPU device type
Definition: Assembler.h:902
none operation
virtual bool parseRegisterType(const char *&linePtr, const char *end, cxuint &type)=0
parse register type for &#39;.reg&#39; pseudo-op
BinaryFormat
binary for Disassembler
Definition: Commons.h:33
const std::vector< AsmSection > & getSections() const
get sections
Definition: Assembler.h:934
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:313
target for assembler expression
Definition: Assembler.h:308
void setDriverVersion(uint32_t driverVersion)
set AMD driver version
Definition: Assembler.h:898
reference pointer based on Glibmm refptr
Definition: Utilities.h:761
~AsmSymbol()
destructor
Definition: Assembler.h:502
AsmClauseType
type of clause
Definition: Assembler.h:624
Assembler & assembler
assembler
Definition: Assembler.h:91
AsmExprOp
assembler expression operator
Definition: Assembler.h:179
assembler section
Definition: Assembler.h:596
assembler macro input filter (for macro filtering)
Definition: AsmSource.h:423
std::pair< CString, uint64_t > DefSym
defined symbol entry
Definition: Assembler.h:654
AsmExprTarget()
empty constructor
Definition: Assembler.h:320
handles raw code format
Definition: AsmFormats.h:180
assembler input layout filter
Definition: AsmSource.h:389
static AsmExprTarget symbolTarget(AsmSymbolEntry *entry)
make symbol target for expression
Definition: Assembler.h:328
Definition: Assembler.h:578
cxbyte info
ELF symbol info.
Definition: Assembler.h:249
void printError(const char *linePtr, const char *message)
print error for position pointed by line pointer
Definition: Assembler.h:957
const AsmExprTarget & getTarget() const
get targer of expression
Definition: Assembler.h:470
signed (arithmetic) shift right
const AsmRegVar * regVar
register alignment
Definition: Assembler.h:592
static bool isUnaryOp(AsmExprOp op)
return true if is unary op
Definition: Assembler.h:464
AsmSourcePos sourcePos
source position of definition
Definition: Assembler.h:637
void printWarning(const char *linePtr, const char *message)
print warning for position pointed by line pointer
Definition: Assembler.h:954
size_t getSymOccursNum() const
get number of symbol occurrences in expression
Definition: Assembler.h:473
cxuint sgprsNum
SGPRs number.
Definition: Assembler.h:143
assembler symbol occurrence in expression
Definition: Assembler.h:231
Flags regFlags
define what extra register must be included
Definition: Assembler.h:145
cxuint sectionId
section id
Definition: Assembler.h:248
void addOccurrenceInExpr(AsmExpression *expr, size_t argIndex, size_t opIndex)
adds occurrence in expression
Definition: Assembler.h:289
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:280
handles ROCM binary format
Definition: AsmFormats.h:437
cxuint RelocType
relocation type
Definition: Commons.h:32
less or equal than
unsigned modulo
void setTarget(AsmExprTarget _target)
set target of expression
Definition: Assembler.h:413
virtual bool relocationIsFit(cxuint bits, AsmExprTargetType tgtType)=0
return true if expresion of target fit to value with specified bits
Definition: Assembler.h:584
line and column
Definition: AsmSource.h:44
bool isEmpty() const
return true if expression is empty
Definition: Assembler.h:406
std::unordered_map< CString, AsmSymbol > AsmSymbolMap
assembler symbol map
Definition: Assembler.h:303
an assembler formats
cxuint sectionId
sectionId
Definition: Assembler.h:515
LineNo lineNo
line number of top-most source
Definition: AsmSource.h:156
size_t offset
offset of relocation
Definition: Assembler.h:353
void setFlags(Flags flags)
set flags
Definition: Assembler.h:923
AsmSourcePos sourcePos
position in source code
Definition: Assembler.h:644
AsmRegField regField
place in instruction
Definition: Assembler.h:587
uint64_t value
value of symbol
Definition: Assembler.h:257
AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
constructor to create custom target
Definition: Assembler.h:323
assembler relocation
Definition: Assembler.h:350
logical shift irght
std::unordered_map< CString, AsmRegVar > regVars
register variables
Definition: Assembler.h:607
size_t opIndex
operator index
Definition: Assembler.h:235
unsigned less or equal
const char * name
name of kernel
Definition: Assembler.h:636
AsmSectionType
assembler section type
Definition: AsmFormats.h:45
assdembler format handler
Definition: AsmFormats.h:114
RefPtr< const AsmSource > getSource() const
get current source after reading line
Definition: AsmSource.h:368
absolute section id
Definition: AsmFormats.h:78
const std::vector< CString > & getIncludeDirs() const
get include directory list
Definition: Assembler.h:926
cxuint vgprsNum
VGPRs number.
Definition: Assembler.h:144
AsmSourcePos prevIfPos
position of previous if-clause
Definition: Assembler.h:646
greater or equal than
main namespace
Definition: AsmFormats.h:41
AsmSymbolEntry * symbol
if symbol
Definition: Assembler.h:511
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:519
static bool isArg(AsmExprOp op)
return true if is argument op
Definition: Assembler.h:461
AsmClauseType type
type of clause
Definition: Assembler.h:643
bool unrefSymOccursNum()
unreference symbol occurrences in expression (used internally)
Definition: Assembler.h:479
virtual bool parseRegisterRange(const char *&linePtr, cxuint &regStart, cxuint &regEnd, const AsmRegVar *&regVar)=0
parse register range
only for running tests
Definition: Assembler.h:52
const KernelMap & getKernelMap() const
get kernel map
Definition: Assembler.h:937
BinaryFormat getBinaryFormat() const
get binary format
Definition: Assembler.h:908
static bool isBinaryOp(AsmExprOp op)
return true if is binary op
Definition: Assembler.h:467
uint64_t size
size of symbol
Definition: Assembler.h:258
size_t getSize() const
get section&#39;s size
Definition: Assembler.h:619
binary negation
const AsmSymbolMap & getSymbolMap() const
get symbols map
Definition: Assembler.h:931
cxbyte AsmExprTargetType
expression target type (one byte)
Definition: Assembler.h:62
uint64_t size
section size
Definition: Assembler.h:603
std::vector< AsmExprSymbolOccurrence > occurrencesInExprs
Definition: Assembler.h:265
macro substitution
std::vector< cxbyte > content
content of section
Definition: Assembler.h:604
AsmSymbolMap::value_type AsmSymbolEntry
assembler symbol entry
Definition: Assembler.h:305
Flags flags
section flags
Definition: Assembler.h:601
uint64_t value
value
Definition: Assembler.h:512
cxuint refCount
reference counter (for internal use only)
Definition: Assembler.h:247
GPUDeviceType
type of GPU device
Definition: GPUId.h:38
size_t offset
offset of destination
Definition: Assembler.h:316
size_t argIndex
argument index
Definition: Assembler.h:234
cxuint sectionId
section id where relocation is present
Definition: Assembler.h:352
cxuint sectionId
section id of destination
Definition: Assembler.h:315
void setDeviceType(const GPUDeviceType deviceType)
set GPU device type
Definition: Assembler.h:905
utilities for other libraries and programs
const Array< AsmExprOp > & getOps() const
get operators list
Definition: Assembler.h:486
unsigned division
RelocType type
relocation type
Definition: Assembler.h:354
target is 32-bit word
Definition: Assembler.h:69
uint64_t alignment
section alignment
Definition: Assembler.h:602
static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
make n-bit word target for expression
Definition: Assembler.h:338
GCN arch assembler.
Definition: Assembler.h:138
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
virtual bool checkMnemonic(const CString &mnemonic) const =0
check if name is mnemonic
AsmSectionType type
type of section
Definition: Assembler.h:600
register pool numbers
Definition: Assembler.h:142
assembler&#39;s clause (if,else,macro,rept)
Definition: Assembler.h:641
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:274
virtual void fillAlignment(size_t size, cxbyte *output)=0
fill alignment when value is not given
LineNo lineNo
line number
Definition: AsmSource.h:46
cxbyte other
ELF symbol other.
Definition: Assembler.h:250
uint32_t getDriverVersion() const
get AMD driver version
Definition: Assembler.h:895
target is 64-bit word
Definition: Assembler.h:70
const AsmFormatHandler * getFormatHandler() const
get format handler
Definition: Assembler.h:950
void printWarningForRange(cxuint bits, uint64_t value, const AsmSourcePos &pos, cxbyte signess=WS_BOTH)
print warning about integer out of range
Definition: Assembler.h:960
target is byte
Definition: Assembler.h:67
handles GalliumCompute format
Definition: AsmFormats.h:354
const char * name
section name
Definition: Assembler.h:598
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:357
assembler symbol structure
Definition: Assembler.h:245
virtual const cxuint * getAllocatedRegisters(size_t &regTypesNum, Flags &regFlags) const =0
get allocated register numbers after assemblying
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:298
const AsmExprArg * getArgs() const
get argument list
Definition: Assembler.h:489
std::vector< AsmVarUsage > regVarUsages
reg-var usage in section
Definition: Assembler.h:609
handles AMD Catalyst format
Definition: AsmFormats.h:206
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:272
unsigned less or equal
size_t hasRelativeSymOccurs() const
get number of symbol occurrences in expression
Definition: Assembler.h:476
std::unordered_map< CString, RefPtr< const AsmMacro > > MacroMap
macro map type
Definition: Assembler.h:656
section is unresolvable
Definition: AsmFormats.h:89
ISA assembler class.
Definition: Assembler.h:88
uint64_t addend
addend
Definition: Assembler.h:359
AsmExpression * expression
target expression pointer
Definition: Assembler.h:233
bool evaluate(Assembler &assembler, uint64_t &value, cxuint &sectionId) const
try to evaluate expression
Definition: Assembler.h:423
assembler expression argument
Definition: Assembler.h:509
simple C-string container
Definition: CString.h:38
all flags
Definition: Assembler.h:53
void setBinaryFormat(BinaryFormat binFormat)
set binary format
Definition: Assembler.h:911
bool condSatisfied
if conditional clause has already been satisfied
Definition: Assembler.h:645
target is symbol
Definition: Assembler.h:66
target is 16-bit word
Definition: Assembler.h:68
const std::vector< AsmKernel > & getKernels() const
get kernels
Definition: Assembler.h:940
const AsmSourcePos & getSourcePos() const
get source position
Definition: Assembler.h:492
assembler source position
Definition: AsmSource.h:152