⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trival_h.h

📁 单片机宏汇编器的源程序。给一些爱好者作为学习编译原理和 C 程序设计的例子.
💻 H
字号:
//---------------------------------------------------------------------------
//-------- TriVal_H.h -------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef	TriVal_H.h  // 防止被重复引用
#define TriVal_H.h
//---------------------------------------------------------------------------

#include "Jtypes_H.h"
#include "ASM_Segment_H.h"
//---------------------------------------------------------------------------
#define FIX_None     0
#define FIX_LOW      1
#define FIX_BYTE     2
#define FIX_REL      3
#define FIX_HIGH     4
#define FIX_WORD     5
#define FIX_INBLK    6
#define FIX_BIT      7
#define FIX_CONV     8
// 0x08 - 0x0F Reserved for bit addr.     (Important!!)
//---------------------------------------------------------------------------
class TriVal : public JObject
{ public:
   int32 val;               // 数值(32位)
   union
   { ASM_Segment* rseg;     // 参考的段
     int16u ExtID;          // 外部变量ID号
   }; // end union
   int8u typ   :4;          // NUM, CODE, DATA, BIT, XDATA, IDATA,SEG,REG
   int8u fix   :4;          // None, LOW, HIGH

   int8u rel;               // 'A', 'R', 'E'
  // ----- Functions ------
   void evaluate(int32 v, int8u t, int8u r, ASM_Segment* s, int8u fx=0);
   void evaluatE(int32 v, int8u t, int8u r, int16u eid, int8u fx=0);
  // ---------------------
   void show()const;
}; // end TriVal
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
inline void TriVal::
evaluatE(int32 v, int8u t, int8u r, int16u eid, int8u fx)
{ val = v; typ = t; rel = r; ExtID = eid; fix = fx;
} // end evaluate
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
inline void TriVal::
evaluate(int32 v, int8u t, int8u r, ASM_Segment* s, int8u fx)
{ val = v; typ = t; rel = r; rseg = s; fix = fx;
} // end evaluate
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//   栈
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#define TknStackSize 64
#define TkSize int16u
#define SPsize int8u
//---------------------------------------------------------------------------
class TokenStack : public JObject
{ private:
   TkSize stk[TknStackSize];   // stk[0] 不用. 使用stk[1~TknStackSize-1].
   SPsize sp;	               // sp==0  为栈空. (sp<255)

  public:
    TokenStack():sp(0)	      {	*stk=0;	}   // constructor

     void push(TkSize data)   {	if(sp<TknStackSize-1) stk[++sp]=data; } // 栈满就压不进.
   TkSize pop()		      {	return(	sp ? stk[sp--] : NULL);	} // 栈空返回零
   TkSize gettop()const	      {	return stk[sp]; } // 栈空返回零
   SPsize getSP()const	      {	return sp; }	// 返回栈顶指针.也是栈中元素的个数.
     void clear()             {	sp = 0;	}
     bool empty()const	      {	return (sp == 0); }
     bool Notempty()const     {	return (sp != 0); }
   TkSize GetSK(SPsize i)const   { return stk[i]; }

   // ----------------------
    void show()const;
}; // end TokenStack
//---------------------------------------------------------------------------
#undef TknStackSize
#undef TkSize
#undef SPsize
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#define TknStackSize 64
#define ValSize int32
#define SPsize  int8u
//---------------------------------------------------------------------------
class ValueStack : public JObject
{ private:
    ValSize stk[TknStackSize];   // stk[0] 不用. 使用stk[1~TknStackSize-1].
    SPsize sp;	               // sp==0  为栈空. (sp<255)

  public:
    ValueStack():sp(0)	      {	*stk=0;	}   // constructor

    void push(ValSize data)   {	if(sp<TknStackSize-1) stk[++sp]=data; } // 栈满就压不进.
    ValSize pop()	      {	return(	sp ? stk[sp--] : NULL);	} // 栈空返回零
    ValSize gettop()const     {	return stk[sp]; } // 栈空返回零
    SPsize getSP()const	      {	return sp; }	// 返回栈顶.也是栈中元素的个数.
     void clear()             {	sp = 0;	}
     bool empty()const	      {	return (sp == 0); }
     bool Notempty()const     {	return (sp != 0); }
   ValSize GetSK(SPsize i)const  { return stk[i]; }
   // ----------------------
    void show()const;
}; // end TokenStack
//---------------------------------------------------------------------------
#undef TknStackSize
#undef ValSize
#undef SPsize
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#define CharStackSize 64
#define SPsize  int8u
//---------------------------------------------------------------------------
class IFeStack : public JObject
{ private:
    pool_16 stk[CharStackSize];   // stk[0] 不用。 使用stk[1~255]。
    SPsize sp;	                  // sp==0  为栈空。 (sp<255)

  public:
    IFeStack():sp(0)	      {	stk[0].pot16 = 0; }   // constructor

     void push(bool a, bool b)
          { if(sp < CharStackSize - 1)  // 栈满就压不进。
             { ++sp; settop(a, b); } // endif
          } // end push

     void pop(bool &a, bool &b)  // a, b 带值返回。       // 第一种pop形式
          { gettop(a, b);        // 栈空就还是返回零。
            if(sp > 0) { --sp; } // endif
          } // end pop

     void pop()                  // 栈空就 do nothing。   // 第二种pop形式
          { if(sp > 0) { --sp; } // endif  栈元素被丢弃。
          } // end pop

     void gettop(bool &a, bool &b)const // a,b 带值返回。 // 第一种gettop形式
          { a = stk[sp].lowbyte; b = stk[sp].highbyte; } // end gettop

   int16u gettop()const                // 函数返回一个值。// 第二种gettop形式
          { return stk[sp].pot16; } // end gettop

     void settop(bool a, bool b)    // 改变栈顶元素
          { stk[sp].lowbyte = a; stk[sp].highbyte = b; }

   SPsize getSP()const	      {	return sp; }	// 返回栈顶.也是栈中元素的个数.
     void clear()             {	sp = 0;	}
     bool empty()const	      {	return (sp == 0); }
     bool Notempty()const     {	return (sp != 0); }

     bool GetXorTop()const    { return stk[sp].lowbyte ^ stk[sp].highbyte; }
     bool getA()const         { return stk[sp].lowbyte; }
     bool getB()const         { return stk[sp].highbyte; }
   // ----------------------
     void show()const;
}; // end IFeStack
//---------------------------------------------------------------------------
#undef CharStackSize
#undef SPsize
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#define ResStackSize 64
#define SPsize  int8u
//---------------------------------------------------------------------------
class TriValStack : public JObject
{ private:
   TriVal stk[ResStackSize];   // stk[0] 不用. 使用stk[1~ResStackSize-1].
   SPsize sp;	               // sp==0  为栈空. (sp<255)

  public:
    TriValStack():sp(0)	      {} // constructor

     void push(TriVal &t)     {	if(sp<ResStackSize-1) stk[++sp]=t; } // 栈满就压不进.
   TriVal pop()		      {	return(	sp?stk[sp--]:stk[0]); } // 栈空返回零
   TriVal gettop()const	      {	return stk[sp]; }  // 栈空返回零
   SPsize getSP()const	      {	return sp; }	// 返回栈顶.也是栈中元素的个数.
     void clear()             {	sp = 0;	}
     bool empty()const	      {	return (sp == 0); }
     bool Notempty()const     {	return (sp != 0); }
   // ----------------------
     void show()const;
}; // end TriValStack
//---------------------------------------------------------------------------
#undef ResStackSize
#undef SPsize
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
//               Written by JamesyFront.    ZLGmcu Dev.Co.Ltd.  2002.
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -