home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / Chip_2000-02_cd.bin / zkuste / Delphi / navody / tt / objvm.exe / UNITS / ObjCodeGeneration.pas < prev    next >
Pascal/Delphi Source File  |  1998-03-12  |  9KB  |  325 lines

  1. unit ObjCodeGeneration;
  2.  
  3. interface
  4. uses
  5.      Code,OpCodes,IntStack,NodeStack;
  6. type TObjCodeGeneration=class
  7.      protected
  8.         fCode:TCode;
  9.      public
  10.         Cur:Integer;
  11.         Stack:TIntStack;
  12.         NodeStack:TNodeStack;
  13.         constructor Create(c:TCode);
  14.         destructor Destroy;override;
  15.         function Code:TCode;
  16.         procedure cOpCode(OpCode:Integer);
  17.         procedure cByte(a:Byte);
  18.         procedure cHalt;
  19.         procedure cAdd;
  20.         procedure cSub;
  21.         procedure cMul;
  22.         procedure cDiv;
  23.         procedure cNegate;
  24.         { Boolean operations       }
  25.         procedure cAnd;
  26.         procedure cOr;
  27.         procedure cNot;
  28.         {Comparison ops}
  29.         procedure cEqu;
  30.         procedure cNE;
  31.         procedure cG;
  32.         procedure cL;
  33.         procedure cLE;
  34.         procedure cGE;
  35.         {Value operations          }
  36.         procedure cGet;
  37.         procedure cSet;
  38.         procedure cExec;
  39.         procedure cEval;
  40.         procedure cGetElem;
  41.         procedure cRoot;
  42.         {Constant operations       }
  43.         procedure cStr(const a:string);
  44.         procedure cFloat(a:Extended);
  45.         procedure cInt(a:Integer);
  46.         procedure cBool(a:boolean);
  47.         {Execution flow operations }
  48.         procedure cNop;
  49.         procedure cJZ(a:Integer);
  50.         procedure cJNZ(a:Integer);
  51.         procedure cJMP(a:Integer);
  52.         procedure cJRZ(a:Integer);
  53.         procedure cDcrR;
  54.         procedure cToR;
  55.           {Moves value from return stack to data stack}
  56.         procedure cFromR;
  57.           {Drops value from reaturn stack}
  58.         procedure cRDrop;
  59.         {Structural Macros }
  60.         {<e> IF <1> ELSE <2> THEN - Like FORTH}
  61.         procedure cIF;
  62.         procedure cTHEN;
  63.         procedure cELSE;
  64.         {<e> TIMES <Action> LOOP }
  65.         procedure cTIMES;
  66.         procedure cLOOP;
  67.         {BEGIN <e> WHILE <Action> REPEAT}
  68.         procedure cBEGIN;
  69.         procedure cWHILE;
  70.         procedure cREPEAT;
  71.         {Adds Gen code to current generation}
  72.         procedure cGen(Gen:TObjCodeGeneration);
  73.      end;
  74. implementation
  75. function    TObjCodeGeneration.Code;
  76.             begin
  77.               Result:=fCode;
  78.             end;
  79. constructor TObjCodeGeneration.Create;
  80.             begin
  81.               fCode:=c;
  82.               Cur:=0;
  83.               Stack:=TIntStack.Create(20);
  84.               NodeStack:=TNodeSTack.Create;
  85.             end;
  86. destructor TObjCodeGeneration.Destroy;
  87.            begin
  88.              cHalt;
  89.              Stack.Free;
  90.              NodeStack.Free;
  91.              Inherited Destroy;
  92.            end;
  93. procedure  TObjCodeGeneration.cOpCode;
  94.            begin
  95.              fCode.Int[Cur]:=OpCode;
  96.              Cur:=Cur+Sizeof(Integer);
  97.            end;
  98. procedure  TObjCodeGeneration.cHalt;
  99.            begin
  100.              cOpCode(ocHalt);
  101.            end;
  102. procedure  TObjCodeGeneration.cAdd;
  103.            begin
  104.              cOpCode(ocAdd);
  105.            end;
  106. procedure  TObjCodeGeneration.cSub;
  107.            begin
  108.              cOpCode(ocSub);
  109.            end;
  110. procedure  TObjCodeGeneration.cMul;
  111.            begin
  112.              cOpCode(ocMul);
  113.            end;
  114. procedure  TObjCodeGeneration.cDiv;
  115.            begin
  116.              cOpCode(ocDiv);
  117.            end;
  118. procedure  TObjCodeGeneration.cNegate;
  119.            begin
  120.              cOpCode(ocNegate);
  121.            end;
  122.  
  123. { Boolean operations       }
  124. procedure  TObjCodeGeneration.cAnd;
  125.            begin
  126.              cOpCode(ocAnd);
  127.            end;
  128. procedure  TObjCodeGeneration.cOr;
  129.            begin
  130.              cOpCode(ocOr);
  131.            end;
  132. procedure  TObjCodeGeneration.cNot;
  133.            begin
  134.              cOpCode(ocNot);
  135.            end;
  136. {Comparison ops}
  137. procedure  TObjCodeGeneration.cEqu;
  138.            begin
  139.              cOpCode(ocEqu);
  140.            end;
  141. procedure  TObjCodeGeneration.cNE;
  142.            begin
  143.              cOpCode(ocNE);
  144.            end;
  145. procedure  TObjCodeGeneration.cG;
  146.            begin
  147.              cOpCode(ocG);
  148.            end;
  149. procedure  TObjCodeGeneration.cL;
  150.            begin
  151.              cOpCode(ocL);
  152.            end;
  153. procedure  TObjCodeGeneration.cLE;
  154.            begin
  155.              cOpCode(ocLE);
  156.            end;
  157. procedure  TObjCodeGeneration.cGE;
  158.            begin
  159.              cOpCode(ocGE);
  160.            end;
  161. {Value operations          }
  162. procedure  TObjCodeGeneration.cGet;
  163.            begin
  164.              cOpCode(ocGet);
  165.            end;
  166. procedure  TObjCodeGeneration.cSet;
  167.            begin
  168.              cOpCode(ocSet);
  169.            end;
  170. procedure  TObjCodeGeneration.cExec;
  171.            begin
  172.              cOpCode(ocExec);
  173.            end;
  174. procedure  TObjCodeGeneration.cEval;
  175.            begin
  176.              cOpCode(ocEval);
  177.            end;
  178. procedure  TObjCodeGeneration.cGetElem;
  179.            begin
  180.              cOpCode(ocGetElem);
  181.            end;
  182. procedure  TObjCodeGeneration.cRoot;
  183.            begin
  184.              cOpCode(ocRoot);
  185.            end;
  186. {Constant operations       }
  187. procedure  TObjCodeGeneration.cStr(const a:string);
  188.            Var i:Integer;
  189.            begin
  190.              cOpCode(ocStr);
  191.              fCode.Str[Cur]:=a;
  192.              Cur:=Cur+Length(a)+1;
  193.            end;
  194. procedure  TObjCodeGeneration.cFloat(a:Extended);
  195.            begin
  196.              cOpCode(ocFloat);
  197.              fCode.Num[Cur]:=a;
  198.              Cur:=Cur+SizeOf(a);
  199.            end;
  200. procedure  TObjCodeGeneration.cInt(a:Integer);
  201.            begin
  202.              cOpCode(ocInt);
  203.              fCode.Int[Cur]:=a;
  204.              Cur:=Cur+SizeOf(a);
  205.            end;
  206. procedure  TObjCodeGeneration.cBool(a:boolean);
  207.            begin
  208.              cOpCode(ocBool);
  209.              if a then
  210.                fCode.Int[Cur]:=-1
  211.              else
  212.                fCode.Int[Cur]:=0;
  213.              Cur:=Cur+SizeOf(a);
  214.            end;
  215. {Execution flow operations }
  216. procedure  TObjCodeGeneration.cNop;
  217.            begin
  218.              cOpCode(ocNop);
  219.            end;
  220. procedure  TObjCodeGeneration.cJZ(a:Integer);
  221.            begin
  222.              cOpCode(ocJZ);
  223.              fCode.Int[Cur]:=a;
  224.              Cur:=Cur+SizeOf(a);
  225.            end;
  226. procedure  TObjCodeGeneration.cJNZ(a:Integer);
  227.            begin
  228.              cOpCode(ocJNZ);
  229.              fCode.Int[Cur]:=a;
  230.              Cur:=Cur+SizeOf(a);
  231.            end;
  232. procedure  TObjCodeGeneration.cJMP(a:Integer);
  233.            begin
  234.              cOpCode(ocJMP);
  235.              fCode.Int[Cur]:=a;
  236.              Cur:=Cur+SizeOf(a);
  237.            end;
  238. procedure  TObjCodeGeneration.cJRZ(a:Integer);
  239.            begin
  240.              cOpCode(ocJRZ);
  241.              fCode.Int[Cur]:=a;
  242.              Cur:=Cur+SizeOf(a);
  243.            end;
  244. procedure  TObjCodeGeneration.cDcrR;
  245.            begin
  246.              cOpCode(ocDcrR);
  247.            end;
  248. procedure  TObjCodeGeneration.cToR;
  249.            begin
  250.              cOpCode(ocToR);
  251.            end;
  252. procedure  TObjCodeGeneration.cFromR;
  253.            begin
  254.              cOpCode(ocFromR);
  255.            end;
  256. procedure  TObjCodeGeneration.cRDrop;
  257.            begin
  258.              cOpCode(ocRDrop);
  259.            end;
  260. procedure  TObjCodeGeneration.cIF;
  261.            begin
  262.              cJNZ(0);
  263.              Stack.Push(Cur-SizeOf(Integer));
  264.            end;
  265. procedure  TObjCodeGeneration.cTHEN;
  266.            begin
  267.              fCode.Int[Stack.Pop]:=Cur;
  268.            end;
  269. procedure  TObjCodeGeneration.cELSE;
  270.            Var Old:integer;
  271.            begin
  272.              Old:=Stack.Pop;
  273.              cJMP(0);
  274.              Stack.Push(Cur-SizeOf(Integer));
  275.              fCode.Int[Old]:=Cur;
  276.            end;
  277.  
  278. procedure  TObjCodeGeneration.cTIMES;
  279.            begin
  280.              cToR;
  281.              Stack.Push(Cur);
  282.              cJRZ(0);
  283.              Stack.Push(Cur-Sizeof(Integer));
  284.              cDcrR;
  285.            end;
  286. procedure  TObjCodeGeneration.cLOOP;
  287.            Var a,b:Integer;
  288.            begin
  289.              a:=Stack.Pop;
  290.              b:=STack.Pop;
  291.              cJMP(b);
  292.              fCode.Int[a]:=Cur;
  293.              cRDrop;
  294.            end;
  295. procedure  TObjCodeGeneration.cBEGIN;
  296.            begin
  297.              Stack.Push(Cur);
  298.            end;
  299. procedure  TObjCodeGeneration.cWHILE;
  300.            begin
  301.              cJNZ(0);
  302.              Stack.Push(Cur-SizeOf(Integer));
  303.            end;
  304. procedure  TObjCodeGeneration.cREPEAT;
  305.            Var a,b:Integer;
  306.            begin
  307.              a:=STack.Pop;
  308.              b:=Stack.Pop;
  309.              cJMP(b);
  310.              fCode.Int[a]:=Cur;
  311.            end;
  312. procedure TObjCodeGeneration.cByte;
  313.           begin
  314.             Code[Cur]:=a;
  315.             Cur:=Cur+1;
  316.           end;
  317. procedure TObjCodeGeneration.cGen;
  318.           Var i:Integer;
  319.           begin
  320.             for i:=0 to Gen.Cur-1 do
  321.               cByte(Gen.Code[i]);
  322.           end;
  323.  
  324. end.
  325.