aasmcpu.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. {
  2. Copyright (c) 1999-2008 by Mazen Neifer and Florian Klaempfl
  3. Contains the assembler object for the AVR
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,verbose,
  23. aasmbase,aasmtai,aasmdata,aasmsym,
  24. cgbase,cgutils,cpubase,cpuinfo,
  25. ogbase;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. maxinfolen = 5;
  32. type
  33. tinsentry = record
  34. opcode : tasmop;
  35. ops : byte;
  36. optypes : array[0..3] of longint;
  37. code : array[0..maxinfolen] of char;
  38. flags : longint;
  39. end;
  40. pinsentry=^tinsentry;
  41. { taicpu }
  42. taicpu = class(tai_cpu_abstract_sym)
  43. constructor op_none(op : tasmop);
  44. constructor op_reg(op : tasmop;_op1 : tregister);
  45. constructor op_const(op : tasmop;_op1 : LongInt);
  46. constructor op_ref(op : tasmop;const _op1 : treference);
  47. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  48. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  49. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  50. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  51. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  52. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  53. { this is for Jmp instructions }
  54. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  55. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  56. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  57. procedure loadbool(opidx:longint;_b:boolean);
  58. { register allocation }
  59. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  60. { register spilling code }
  61. function spilling_get_operation_type(opnr: longint): topertype;override;
  62. end;
  63. tai_align = class(tai_align_abstract)
  64. { nothing to add }
  65. end;
  66. procedure InitAsm;
  67. procedure DoneAsm;
  68. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  69. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  70. implementation
  71. {*****************************************************************************
  72. taicpu Constructors
  73. *****************************************************************************}
  74. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  75. begin
  76. if opidx>=ops then
  77. ops:=opidx+1;
  78. with oper[opidx]^ do
  79. begin
  80. if typ=top_ref then
  81. dispose(ref);
  82. b:=_b;
  83. typ:=top_bool;
  84. end;
  85. end;
  86. constructor taicpu.op_none(op : tasmop);
  87. begin
  88. inherited create(op);
  89. end;
  90. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  91. begin
  92. inherited create(op);
  93. ops:=1;
  94. loadreg(0,_op1);
  95. end;
  96. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  97. begin
  98. inherited create(op);
  99. ops:=1;
  100. loadref(0,_op1);
  101. end;
  102. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  103. begin
  104. inherited create(op);
  105. ops:=1;
  106. loadconst(0,_op1);
  107. end;
  108. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  109. begin
  110. inherited create(op);
  111. ops:=2;
  112. loadreg(0,_op1);
  113. loadreg(1,_op2);
  114. end;
  115. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  116. begin
  117. inherited create(op);
  118. ops:=2;
  119. loadreg(0,_op1);
  120. loadconst(1,_op2);
  121. end;
  122. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  123. begin
  124. inherited create(op);
  125. ops:=2;
  126. loadconst(0,_op1);
  127. loadreg(1,_op2);
  128. end;
  129. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  130. begin
  131. inherited create(op);
  132. ops:=2;
  133. loadreg(0,_op1);
  134. loadref(1,_op2);
  135. end;
  136. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  137. begin
  138. inherited create(op);
  139. ops:=2;
  140. loadref(0,_op1);
  141. loadreg(1,_op2);
  142. end;
  143. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  144. begin
  145. inherited create(op);
  146. ops:=2;
  147. loadref(0,_op1);
  148. loadconst(1,_op2);
  149. end;
  150. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  151. begin
  152. inherited create(op);
  153. is_jmp:=op in jmp_instructions;
  154. condition:=cond;
  155. ops:=1;
  156. loadsymbol(0,_op1,0);
  157. end;
  158. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  159. begin
  160. inherited create(op);
  161. is_jmp:=op in jmp_instructions;
  162. ops:=1;
  163. loadsymbol(0,_op1,0);
  164. end;
  165. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  166. begin
  167. inherited create(op);
  168. ops:=1;
  169. loadsymbol(0,_op1,_op1ofs);
  170. end;
  171. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  172. begin
  173. result:=(
  174. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  175. ) and
  176. (ops=2) and
  177. (oper[0]^.typ=top_reg) and
  178. (oper[1]^.typ=top_reg) and
  179. (oper[0]^.reg=oper[1]^.reg);
  180. end;
  181. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  182. begin
  183. result:=operand_read;
  184. case opcode of
  185. A_LD,
  186. A_POP:
  187. if opnr=0 then
  188. result:=operand_write;
  189. A_PUSH,
  190. A_BIT,
  191. A_DJNZ,
  192. A_JR,
  193. A_JP:
  194. ;
  195. A_SET:
  196. if opnr=1 then
  197. result:=operand_readwrite;
  198. A_EX:
  199. result:=operand_readwrite;
  200. else
  201. begin
  202. if opnr=0 then
  203. result:=operand_readwrite;
  204. end;
  205. end;
  206. end;
  207. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  208. begin
  209. case getregtype(r) of
  210. R_INTREGISTER :
  211. result:=taicpu.op_reg_ref(A_LD,r,ref)
  212. else
  213. internalerror(200401041);
  214. end;
  215. end;
  216. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  217. begin
  218. case getregtype(r) of
  219. R_INTREGISTER :
  220. result:=taicpu.op_ref_reg(A_LD,ref,r);
  221. else
  222. internalerror(200401041);
  223. end;
  224. end;
  225. procedure InitAsm;
  226. begin
  227. end;
  228. procedure DoneAsm;
  229. begin
  230. end;
  231. begin
  232. cai_cpu:=taicpu;
  233. cai_align:=tai_align;
  234. end.