source: CLRX/CLRadeonExtender/trunk/CLRX/amdasm/AsmDefs.h @ 3630

Last change on this file since 3630 was 3630, checked in by matszpk, 19 months ago

CLRadeonExtender: Asm: Add preliminary code of AsmForInputFilter?. Fixed old stupid bug of error handling when makeSymbolSnapshot called.

File size: 23.6 KB
Line 
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 */
19/*! \file AsmDefs.h
20 * \brief an assembler for Radeon GPU's
21 */
22
23#ifndef __CLRX_ASMDEFS_H__
24#define __CLRX_ASMDEFS_H__
25
26#include <CLRX/Config.h>
27#include <cstdint>
28#include <vector>
29#include <utility>
30#include <list>
31#include <unordered_map>
32#include <CLRX/utils/Utilities.h>
33#include <CLRX/amdasm/Commons.h>
34#include <CLRX/amdasm/AsmSource.h>
35#include <CLRX/amdasm/AsmFormats.h>
36
37/// main namespace
38namespace CLRX
39{
40
41/// Assembler exception class
42class AsmException: public Exception
43{
44public:
45    /// empty constructor
46    AsmException() = default;
47    /// constructor with messasge
48    explicit AsmException(const std::string& message);
49    /// destructor
50    virtual ~AsmException() noexcept = default;
51};
52
53enum: cxbyte {
54    WS_UNSIGNED = 0,  // only unsigned
55    WS_BOTH = 1,  // both signed and unsigned range checking
56};
57
58/// expression target type (one byte)
59typedef cxbyte AsmExprTargetType;
60
61enum : AsmExprTargetType
62{
63    ASMXTGT_SYMBOL = 0, ///< target is symbol
64    ASMXTGT_DATA8,      ///< target is byte
65    ASMXTGT_DATA16,     ///< target is 16-bit word
66    ASMXTGT_DATA32,     ///< target is 32-bit word
67    ASMXTGT_DATA64,     ///< target is 64-bit word
68    ASMXTGT_CODEFLOW,    ///< code flow entry
69   
70    GCNTGT_LITIMM = 16,
71    GCNTGT_SOPKSIMM16,
72    GCNTGT_SOPJMP,
73    GCNTGT_SMRDOFFSET,
74    GCNTGT_DSOFFSET16,
75    GCNTGT_DSOFFSET8_0,
76    GCNTGT_DSOFFSET8_1,
77    GCNTGT_MXBUFOFFSET,
78    GCNTGT_SMEMOFFSET,
79    GCNTGT_SOPCIMM8,
80    GCNTGT_SMEMIMM,
81    GCNTGT_SMEMOFFSETVEGA,
82    GCNTGT_INSTOFFSET,
83    GCNTGT_INSTOFFSET_S
84};
85
86/*
87 * assembler expressions
88 */
89
90/// assembler expression operator
91enum class AsmExprOp : cxbyte
92{
93    ARG_VALUE = 0,  ///< value
94    ARG_SYMBOL = 1,  ///< symbol without defined value
95    NEGATE = 2, ///< negation
96    BIT_NOT,    ///< binary negation
97    LOGICAL_NOT,    ///< logical negation
98    PLUS,   ///< plus (nothing)
99    ADDITION,   ///< addition
100    SUBTRACT,       ///< subtraction
101    MULTIPLY,       ///< multiplication
102    DIVISION,       ///< unsigned division
103    SIGNED_DIVISION,    ///< signed division
104    MODULO,         ///< unsigned modulo
105    SIGNED_MODULO,      ///< signed modulo
106    BIT_AND,        ///< bitwise AND
107    BIT_OR,         //< bitwise OR
108    BIT_XOR,        ///< bitwise XOR
109    BIT_ORNOT,      ///< bitwise OR-not
110    SHIFT_LEFT,     ///< shift left
111    SHIFT_RIGHT,    ///< logical shift irght
112    SIGNED_SHIFT_RIGHT, ///< signed (arithmetic) shift right
113    LOGICAL_AND,    ///< logical AND
114    LOGICAL_OR,     ///< logical OR
115    EQUAL,          ///< equality
116    NOT_EQUAL,      ///< unequality
117    LESS,           ///< less than
118    LESS_EQ,        ///< less or equal than
119    GREATER,        ///< greater than
120    GREATER_EQ,     ///< greater or equal than
121    BELOW, ///< unsigned less
122    BELOW_EQ, ///< unsigned less or equal
123    ABOVE, ///< unsigned less
124    ABOVE_EQ, ///< unsigned less or equal
125    CHOICE,  ///< a ? b : c
126    CHOICE_START,   ///< helper
127    FIRST_ARG = ARG_VALUE,
128    LAST_ARG = ARG_SYMBOL,
129    FIRST_UNARY = NEGATE,   ///< helper
130    LAST_UNARY = PLUS,  ///< helper
131    FIRST_BINARY = ADDITION,    ///< helper
132    LAST_BINARY = ABOVE_EQ, ///< helper
133    NONE = 0xff ///< none operation
134};
135
136struct AsmExprTarget;
137
138union AsmExprArg;
139
140class AsmExpression;
141
142/// assembler symbol occurrence in expression
143struct AsmExprSymbolOccurrence
144{
145    AsmExpression* expression;      ///< target expression pointer
146    size_t argIndex;        ///< argument index
147    size_t opIndex;         ///< operator index
148   
149    /// comparison operator
150    bool operator==(const AsmExprSymbolOccurrence& b) const
151    { return expression==b.expression && opIndex==b.opIndex && argIndex==b.argIndex; }
152};
153
154struct AsmRegVar;
155struct AsmScope;
156
157/// assembler symbol structure
158struct AsmSymbol
159{
160    cxuint refCount;    ///< reference counter (for internal use only)
161    cxuint sectionId;       ///< section id
162    cxbyte info;           ///< ELF symbol info
163    cxbyte other;           ///< ELF symbol other
164    cxuint hasValue:1;         ///< symbol is defined
165    cxuint onceDefined:1;       ///< symbol can be only once defined (likes labels)
166    cxuint resolving:1;         ///< helper
167    cxuint base:1;              ///< with base expression
168    cxuint snapshot:1;          ///< if symbol is snapshot
169    cxuint regRange:1;          ///< if symbol is register range
170    uint64_t value;         ///< value of symbol
171    uint64_t size;          ///< size of symbol
172    union {
173        AsmExpression* expression;      ///< expression of symbol (if not resolved)
174        const AsmRegVar* regVar;
175    };
176   
177    /** list of occurrences in expressions */
178    std::vector<AsmExprSymbolOccurrence> occurrencesInExprs;
179   
180    /// empty constructor
181    explicit AsmSymbol(bool _onceDefined = false) :
182            refCount(1), sectionId(ASMSECT_ABS), info(0), other(0), hasValue(false),
183            onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
184            regRange(false), value(0), size(0), expression(nullptr)
185    { }
186    /// constructor with expression
187    explicit AsmSymbol(AsmExpression* expr, bool _onceDefined = false, bool _base = false) :
188            refCount(1), sectionId(ASMSECT_ABS), info(0), other(0), hasValue(false),
189            onceDefined(_onceDefined), resolving(false), base(_base),
190            snapshot(false), regRange(false), value(0), size(0), expression(expr)
191    { }
192    /// constructor with value and section id
193    explicit AsmSymbol(cxuint _sectionId, uint64_t _value, bool _onceDefined = false) :
194            refCount(1), sectionId(_sectionId), info(0), other(0), hasValue(true),
195            onceDefined(_onceDefined), resolving(false), base(false), snapshot(false),
196            regRange(false), value(_value), size(0), expression(nullptr)
197    { }
198    /// destructor
199    ~AsmSymbol();
200   
201    /// adds occurrence in expression
202    void addOccurrenceInExpr(AsmExpression* expr, size_t argIndex, size_t opIndex)
203    { occurrencesInExprs.push_back({expr, argIndex, opIndex}); }
204    /// remove occurrence in expression
205    void removeOccurrenceInExpr(AsmExpression* expr, size_t argIndex, size_t opIndex);
206    /// clear list of occurrences in expression
207    void clearOccurrencesInExpr();
208    /// make symbol as undefined
209    void undefine();
210    /// return true if symbol defined (have value or expression)
211    bool isDefined() const
212    { return hasValue || expression!=nullptr; }
213};
214
215/// assembler symbol map
216typedef std::unordered_map<CString, AsmSymbol> AsmSymbolMap;
217/// assembler symbol entry
218typedef AsmSymbolMap::value_type AsmSymbolEntry;
219
220/// target for assembler expression
221struct AsmExprTarget
222{
223    AsmExprTargetType type;    ///< type of target
224    union
225    {
226        AsmSymbolEntry* symbol; ///< symbol entry (if ASMXTGT_SYMBOL)
227        struct {
228            cxuint sectionId;   ///< section id of destination
229            union {
230                size_t offset;      ///< offset of destination
231                size_t cflowId;     ///< cflow index of destination
232            };
233        };
234    };
235    /// empty constructor
236    AsmExprTarget() { }
237   
238    /// constructor to create custom target
239    AsmExprTarget(AsmExprTargetType _type, cxuint _sectionId, size_t _offset)
240            : type(_type), sectionId(_sectionId), offset(_offset)
241    { }
242   
243    /// make symbol target for expression
244    static AsmExprTarget symbolTarget(AsmSymbolEntry* entry)
245    { 
246        AsmExprTarget target;
247        target.type = ASMXTGT_SYMBOL;
248        target.symbol = entry;
249        return target;
250    }
251    /// make code flow target for expression
252    static AsmExprTarget codeFlowTarget(cxuint sectionId, size_t cflowIndex)
253    {
254        AsmExprTarget target;
255        target.type = ASMXTGT_CODEFLOW;
256        target.sectionId = sectionId;
257        target.cflowId = cflowIndex;
258        return target;
259    }
260   
261    /// make n-bit word target for expression
262    template<typename T>
263    static AsmExprTarget dataTarget(cxuint sectionId, size_t offset)
264    {
265        AsmExprTarget target;
266        target.type = (sizeof(T)==1) ? ASMXTGT_DATA8 : (sizeof(T)==2) ? ASMXTGT_DATA16 :
267                (sizeof(T)==4) ? ASMXTGT_DATA32 : ASMXTGT_DATA64;
268        target.sectionId = sectionId;
269        target.offset = offset;
270        return target;
271    }
272};
273
274/// assembler relocation
275struct AsmRelocation
276{
277    cxuint sectionId;   ///< section id where relocation is present
278    size_t offset;  ///< offset of relocation
279    RelocType type; ///< relocation type
280    union {
281        AsmSymbolEntry* symbol; ///< symbol
282        cxuint relSectionId;    ///< section for which relocation is defined
283    };
284    uint64_t addend;    ///< addend
285};
286
287/// assembler expression class
288class AsmExpression: public NonCopyableAndNonMovable
289{
290private:
291    class TempSymbolSnapshotMap;
292   
293    AsmExprTarget target;
294    AsmSourcePos sourcePos;
295    size_t symOccursNum;
296    bool relativeSymOccurs;
297    bool baseExpr;
298    Array<AsmExprOp> ops;
299    std::unique_ptr<LineCol[]> messagePositions;    ///< for every potential message
300    std::unique_ptr<AsmExprArg[]> args;
301   
302    AsmSourcePos getSourcePos(size_t msgPosIndex) const
303    {
304        AsmSourcePos pos = sourcePos;
305        pos.lineNo = messagePositions[msgPosIndex].lineNo;
306        pos.colNo = messagePositions[msgPosIndex].colNo;
307        return pos;
308    }
309   
310    static bool makeSymbolSnapshot(Assembler& assembler,
311               TempSymbolSnapshotMap* snapshotMap, const AsmSymbolEntry& symEntry,
312               AsmSymbolEntry*& outSymEntry, const AsmSourcePos* topParentSourcePos);
313   
314    AsmExpression();
315    void setParams(size_t symOccursNum, bool relativeSymOccurs,
316            size_t _opsNum, const AsmExprOp* ops, size_t opPosNum, const LineCol* opPos,
317            size_t argsNum, const AsmExprArg* args, bool baseExpr = false);
318public:
319    /// constructor of expression (helper)
320    AsmExpression(const AsmSourcePos& pos, size_t symOccursNum, bool relativeSymOccurs,
321            size_t opsNum, size_t opPosNum, size_t argsNum, bool baseExpr = false);
322    /// constructor of expression (helper)
323    AsmExpression(const AsmSourcePos& pos, size_t symOccursNum, bool relativeSymOccurs,
324              size_t opsNum, const AsmExprOp* ops, size_t opPosNum,
325              const LineCol* opPos, size_t argsNum, const AsmExprArg* args,
326              bool baseExpr = false);
327    /// destructor
328    ~AsmExpression();
329   
330    /// return true if expression is empty
331    bool isEmpty() const
332    { return ops.empty(); }
333   
334    /// helper to create symbol snapshot. Creates initial expression for symbol snapshot
335    AsmExpression* createForSnapshot(const AsmSourcePos* exprSourcePos) const;
336   
337    /// set target of expression
338    void setTarget(AsmExprTarget _target)
339    { target = _target; }
340   
341    /// try to evaluate expression
342    /**
343     * \param assembler assembler instace
344     * \param value output value
345     * \param sectionId output section id
346     * \return true if evaluated
347     */
348    bool evaluate(Assembler& assembler, uint64_t& value, cxuint& sectionId) const
349    { return evaluate(assembler, 0, ops.size(), value, sectionId); }
350   
351    /// try to evaluate expression
352    /**
353     * \param assembler assembler instace
354     * \param value output value
355     * \param opStart start operand
356     * \param opEnd end operand
357     * \param sectionId output section id
358     * \return true if evaluated
359     */
360    bool evaluate(Assembler& assembler, size_t opStart, size_t opEnd,
361                  uint64_t& value, cxuint& sectionId) const;
362   
363    /// parse expression. By default, also gets values of symbol or  creates them
364    /** parse expresion from assembler's line string. Accepts empty expression.
365     * \param assembler assembler
366     * \param linePos position in line and output position in line
367     * \param makeBase do not evaluate resolved symbols, put them to expression
368     * \param dontResolveSymbolsLater do not resolve symbols later
369     * \return expression pointer
370     */
371    static AsmExpression* parse(Assembler& assembler, size_t& linePos,
372                    bool makeBase = false, bool dontResolveSymbolsLater = false);
373   
374    /// parse expression. By default, also gets values of symbol or  creates them
375    /** parse expresion from assembler's line string. Accepts empty expression.
376     * \param assembler assembler
377     * \param linePtr string at position in line (returns output line pointer)
378     * \param makeBase do not evaluate resolved symbols, put them to expression
379     * \param dontResolveSymbolsLater do not resolve symbols later
380     * \return expression pointer
381     */
382    static AsmExpression* parse(Assembler& assembler, const char*& linePtr,
383              bool makeBase = false, bool dontResolveSymbolsLater = false);
384   
385    /// return true if is argument op
386    static bool isArg(AsmExprOp op)
387    { return (AsmExprOp::FIRST_ARG <= op && op <= AsmExprOp::LAST_ARG); }
388    /// return true if is unary op
389    static bool isUnaryOp(AsmExprOp op)
390    { return (AsmExprOp::FIRST_UNARY <= op && op <= AsmExprOp::LAST_UNARY); }
391    /// return true if is binary op
392    static bool isBinaryOp(AsmExprOp op)
393    { return (AsmExprOp::FIRST_BINARY <= op && op <= AsmExprOp::LAST_BINARY); }
394    /// get targer of expression
395    const AsmExprTarget& getTarget() const
396    { return target; }
397    /// get number of symbol occurrences in expression
398    size_t getSymOccursNum() const
399    { return symOccursNum; }
400    /// get number of symbol occurrences in expression
401    size_t hasRelativeSymOccurs() const
402    { return relativeSymOccurs; }
403    /// unreference symbol occurrences in expression (used internally)
404    bool unrefSymOccursNum()
405    { return --symOccursNum!=0; }
406   
407    /// substitute occurrence in expression by value
408    void substituteOccurrence(AsmExprSymbolOccurrence occurrence, uint64_t value,
409                  cxuint sectionId = ASMSECT_ABS);
410    /// get operators list
411    const Array<AsmExprOp>& getOps() const
412    { return ops; }
413    /// get argument list
414    const AsmExprArg* getArgs() const
415    { return args.get(); }
416    /// get source position
417    const AsmSourcePos& getSourcePos() const
418    { return sourcePos; }
419   
420    /// for internal usage
421    size_t toTop(size_t opIndex) const;
422   
423    /// create an expression to evaluate from base expression
424    AsmExpression* createExprToEvaluate(Assembler& assembler) const;
425   
426    /// make symbol snapshot (required to implement .eqv pseudo-op)   
427    static bool makeSymbolSnapshot(Assembler& assembler, const AsmSymbolEntry& symEntry,
428               AsmSymbolEntry*& outSymEntry, const AsmSourcePos* parentExprSourcePos);
429   
430    /// fast expression parse and evaluate
431    static bool fastExprEvaluate(Assembler& assembler, const char*& linePtr,
432                uint64_t& value);
433    static bool fastExprEvaluate(Assembler& assembler, size_t& linePos,
434                uint64_t& value);
435};
436
437inline AsmSymbol::~AsmSymbol()
438{
439    if (base) delete expression; // if symbol with base expresion
440    clearOccurrencesInExpr();
441}
442
443/// assembler expression argument
444union AsmExprArg
445{
446    AsmSymbolEntry* symbol; ///< if symbol
447    uint64_t value;         ///< value
448    struct {
449        uint64_t value;         ///< value
450        cxuint sectionId;       ///< sectionId
451    } relValue; ///< relative value (with section)
452};
453
454inline void AsmExpression::substituteOccurrence(AsmExprSymbolOccurrence occurrence,
455                        uint64_t value, cxuint sectionId)
456{
457    ops[occurrence.opIndex] = AsmExprOp::ARG_VALUE;
458    args[occurrence.argIndex].relValue.value = value;
459    args[occurrence.argIndex].relValue.sectionId = sectionId;
460    if (sectionId != ASMSECT_ABS)
461        relativeSymOccurs = true;
462}
463
464/// type of register field
465typedef cxbyte AsmRegField;
466
467enum : AsmRegField
468{
469    ASMFIELD_NONE = 0,
470    GCNFIELD_SSRC0,
471    GCNFIELD_SSRC1,
472    GCNFIELD_SDST,
473    GCNFIELD_SMRD_SBASE,
474    GCNFIELD_SMRD_SDST,
475    GCNFIELD_SMRD_SDSTH,
476    GCNFIELD_SMRD_SOFFSET,
477    GCNFIELD_VOP_SRC0,
478    GCNFIELD_VOP_VSRC1,
479    GCNFIELD_VOP_SSRC1,
480    GCNFIELD_VOP_VDST,
481    GCNFIELD_VOP_SDST,
482    GCNFIELD_VOP3_SRC0,
483    GCNFIELD_VOP3_SRC1,
484    GCNFIELD_VOP3_SRC2,
485    GCNFIELD_VOP3_VDST,
486    GCNFIELD_VOP3_SDST0,
487    GCNFIELD_VOP3_SSRC,
488    GCNFIELD_VOP3_SDST1,
489    GCNFIELD_VINTRP_VSRC0,
490    GCNFIELD_VINTRP_VDST,
491    GCNFIELD_DS_ADDR,
492    GCNFIELD_DS_DATA0,
493    GCNFIELD_DS_DATA1,
494    GCNFIELD_DS_VDST,
495    GCNFIELD_M_VADDR,
496    GCNFIELD_M_VDATA,
497    GCNFIELD_M_VDATAH,
498    GCNFIELD_M_VDATALAST,
499    GCNFIELD_M_SRSRC,
500    GCNFIELD_MIMG_SSAMP,
501    GCNFIELD_M_SOFFSET,
502    GCNFIELD_EXP_VSRC0,
503    GCNFIELD_EXP_VSRC1,
504    GCNFIELD_EXP_VSRC2,
505    GCNFIELD_EXP_VSRC3,
506    GCNFIELD_FLAT_ADDR,
507    GCNFIELD_FLAT_DATA,
508    GCNFIELD_FLAT_VDST,
509    GCNFIELD_FLAT_VDSTLAST,
510    GCNFIELD_DPPSDWA_SRC0,
511    GCNFIELD_DPPSDWA_SSRC0,
512    GCNFIELD_FLAT_SADDR,
513    GCNFIELD_SDWAB_SDST
514};
515
516struct AsmScope;
517
518/// Regvar info structure
519struct AsmRegVar
520{
521    cxuint type;    ///< scalar/vector/other
522    uint16_t size;  ///< in regs
523};
524
525/// single regvar id
526struct AsmSingleVReg // key for regvar
527{
528    const AsmRegVar* regVar;    ///< regvar
529    uint16_t index; ///< index of regvar array
530   
531    /// equal operator
532    bool operator==(const AsmSingleVReg& r2) const
533    { return regVar == r2.regVar && index == r2.index; }
534    /// not equal operator
535    bool operator!=(const AsmSingleVReg& r2) const
536    { return regVar == r2.regVar && index == r2.index; }
537};
538
539/// regvar map
540typedef std::unordered_map<CString, AsmRegVar> AsmRegVarMap;
541/// regvar entry
542typedef AsmRegVarMap::value_type AsmRegVarEntry;
543
544enum : cxbyte {
545    ASMRVU_READ = 1,        ///< register will be read
546    ASMRVU_WRITE = 2,       ///< register will be written
547    ASMRVU_ACCESS_MASK = 3, ///< access mask
548    ASMRVU_REGTYPE_MASK = 4,    ///< for internal usage (regtype)
549    ASMRVU_REGSIZE_SHIFT = 3,   ///< for internal usage
550    ASMRVU_REGSIZE_MASK = 0x78  ///< for internal usage
551};
552
553/// regvar usage in code
554struct AsmRegVarUsage
555{
556    size_t offset;  ///< offset in section
557    const AsmRegVar* regVar;    ///< if null, then usage of called register
558    uint16_t rstart; ///< register start
559    uint16_t rend;  ///< register end
560    AsmRegField regField;   ///< place in instruction
561    cxbyte rwFlags;  ///< 1 - read, 2 - write
562    cxbyte align;   ///< register alignment
563    bool useRegMode; ///< if RVU from useReg pseudo-ops
564};
565
566/// regvar usage (internal)
567struct AsmRegVarUsageInt
568{
569    const AsmRegVar* regVar;    ///< if null, then usage of called register
570    uint16_t rstart;    ///< register start
571    uint16_t rend;      ///< register end
572    AsmRegField regField;   ///< place in instruction
573    cxbyte rwFlags;  ///< 1 - read, 2 - write
574    cxbyte align;   ///< register alignment
575};
576
577/// internal structure for regusage
578struct AsmRegUsageInt
579{
580    AsmRegField regField;   ///< place in instruction
581    cxbyte rwFlags;     ///< 1 - read, 2 - write, other flags
582};
583
584/// internal structure for regusage
585struct AsmRegUsage2Int
586{
587    uint16_t rstart;    ///< register start
588    uint16_t rend;      ///< register end
589    cxbyte rwFlags;     ///< rw flags and others
590};
591
592/// code flow type
593enum AsmCodeFlowType
594{
595    JUMP = 0,   ///< jump
596    CJUMP,   ///< conditional jump
597    CALL,   ///< call of procedure
598    RETURN, ///< return from procedure
599    START,  ///< code start
600    END     ///< code end
601};
602
603/// code flow entry
604struct AsmCodeFlowEntry
605{
606    size_t offset;      ///< offset where is this entry
607    size_t target;      ///< target jump addreses
608    AsmCodeFlowType type;   ///< type of code flow entry
609};
610
611/// assembler macro map
612typedef std::unordered_map<CString, RefPtr<const AsmMacro> > AsmMacroMap;
613
614struct AsmScope;
615
616/// type definition of scope's map
617typedef std::unordered_map<CString, AsmScope*> AsmScopeMap;
618
619/// assembler scope for symbol, macros, regvars
620struct AsmScope
621{
622    AsmScope* parent;   ///< parent scope
623    AsmSymbolMap symbolMap; ///< symbol map
624    AsmRegVarMap regVarMap; ///< regvar map
625    AsmScopeMap scopeMap;   ///< scope map
626    bool temporary; ///< true if temporary
627    std::list<AsmScope*> usedScopes;    ///< list of used scope in this scope
628   
629    /// set of used scopes in this scope
630    std::unordered_map<AsmScope*, std::list<AsmScope*>::iterator> usedScopesSet;
631   
632    /// constructor
633    AsmScope(AsmScope* _parent, const AsmSymbolMap& _symbolMap,
634                     bool _temporary = false)
635            : parent(_parent), symbolMap(_symbolMap), temporary(_temporary)
636    { }
637    /// constructor
638    AsmScope(AsmScope* _parent = nullptr, bool _temporary= false)
639            : parent(_parent), temporary(_temporary)
640    { }
641    /// destructor
642    ~AsmScope();
643   
644    /// start using scope in this scope
645    void startUsingScope(AsmScope* scope);
646    /// stop using scope in this scope
647    void stopUsingScope(AsmScope* scope);
648    /// remove all usings
649    void stopUsingScopes()
650    {
651        usedScopes.clear();
652        usedScopesSet.clear();
653    }
654    /// delete symbols recursively
655    void deleteSymbolsRecursively();
656};
657
658class ISAUsageHandler;
659
660/// assembler section
661struct AsmSection
662{
663    const char* name;       ///< section name
664    cxuint kernelId;    ///< kernel id (optional)
665    AsmSectionType type;        ///< type of section
666    Flags flags;   ///< section flags
667    uint64_t alignment; ///< section alignment
668    uint64_t size;  ///< section size
669    std::vector<cxbyte> content;    ///< content of section
670   
671    std::unique_ptr<ISAUsageHandler> usageHandler;  ///< usage handler
672    std::vector<AsmCodeFlowEntry> codeFlow;  ///< code flow info
673   
674    /// constructor
675    AsmSection();
676    /// constructor
677    AsmSection(const char* _name, cxuint _kernelId, AsmSectionType _type,
678            Flags _flags, uint64_t _alignment, uint64_t _size = 0)
679            : name(_name), kernelId(_kernelId), type(_type), flags(_flags),
680              alignment(_alignment), size(_size)
681    { }
682   
683    /// copy constructor
684    AsmSection(const AsmSection& section);
685    /// copy assignment
686    AsmSection& operator=(const AsmSection& section);
687   
688    /// add code flow entry to this section
689    void addCodeFlowEntry(const AsmCodeFlowEntry& entry)
690    { codeFlow.push_back(entry); }
691   
692    /// get section's size
693    size_t getSize() const
694    { return ((flags&ASMSECT_WRITEABLE) != 0) ? content.size() : size; }
695};
696
697/// kernel entry structure
698struct AsmKernel
699{
700    const char* name;   ///< name of kernel
701    AsmSourcePos sourcePos; ///< source position of definition
702    std::vector<std::pair<size_t, size_t> > codeRegions; ///< code regions
703   
704    /// open kernel region in code
705    void openCodeRegion(size_t offset);
706    /// close kernel region in code
707    void closeCodeRegion(size_t offset);
708};
709
710};
711
712namespace std
713{
714
715/// std::hash specialization for CLRX CString
716template<>
717struct hash<CLRX::AsmSingleVReg>
718{
719    typedef CLRX::AsmSingleVReg argument_type;    ///< argument type
720    typedef std::size_t result_type;    ///< result type
721   
722    /// a calling operator
723    size_t operator()(const CLRX::AsmSingleVReg& r1) const
724    {
725        std::hash<const CLRX::AsmRegVar*> h1;
726        std::hash<uint16_t> h2;
727        return h1(r1.regVar) ^ (h2(r1.index)<<1);
728    }
729};
730
731}
732
733#endif
Note: See TracBrowser for help on using the repository browser.