aasmcpu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. instabentries = {$i z80nop.inc}
  32. maxinfolen = 18;
  33. type
  34. { Operand types }
  35. toperandtype=(
  36. OT_NONE,
  37. OT_IMM3, { 3-bit immediate value (bit number: [0..7]) }
  38. OT_IMM8, { 8-bit immediate value }
  39. OT_IMM16, { 16-bit immediate value }
  40. OT_IMM_VAL0, { the immediate value 0 }
  41. OT_IMM_VAL1, { the immediate value 1 }
  42. OT_IMM_VAL2, { the immediate value 2 }
  43. OT_IMM_RST, { immediate value in [$00,$08,$10,$18,$20,$28,$30,$38] }
  44. OT_IMM_PORT, { 8-bit immediate port number for the IN and OUT instructions }
  45. OT_REG8, { 8-bit register: A/B/C/D/E/H/L }
  46. OT_REG8_A, { register A }
  47. OT_REG8_I, { register I }
  48. OT_REG8_R, { register R }
  49. OT_REG8_C_PORT, { implied parameter of the IN and OUT instructions }
  50. OT_REG16_IX, { register IX }
  51. OT_REG16_IY, { register IY }
  52. OT_REG16_SP, { register SP }
  53. OT_REG16_BC_DE_HL_SP, { 16-bit register pair: BC/DE/HL/SP }
  54. OT_REG16_BC_DE_HL_AF, { 16-bit register pair: BC/DE/HL/AF }
  55. OT_REG16_BC_DE_IX_SP, { 16-bit register pair: BC/DE/IX/SP }
  56. OT_REG16_BC_DE_IY_SP, { 16-bit register pair: BC/DE/IY/SP }
  57. OT_REG16_DE, { 16-bit register pair DE }
  58. OT_REG16_HL, { 16-bit register pair HL }
  59. OT_REG16_AF, { 16-bit register pair AF }
  60. OT_REG16_AF_, { alternate register set, 16-bit register pair AF' }
  61. OT_RELJMP8, { 8-bit relative jump offset }
  62. OT_COND, { condition: NZ/Z/NC/C/PO/PE/P/M }
  63. OT_COND_C, { condition C }
  64. OT_COND_NC, { condition NC }
  65. OT_COND_Z, { condition Z }
  66. OT_COND_NZ, { condition NZ }
  67. OT_REF_ADDR16, { memory contents at address (nn = 16-bit immediate address) }
  68. OT_REF_BC, { memory contents at address in register BC }
  69. OT_REF_DE, { memory contents at address in register DE }
  70. OT_REF_HL, { memory contents at address in register HL }
  71. OT_REF_SP, { memory contents at address in register SP }
  72. OT_REF_IX, { memory contents at address in register IX }
  73. OT_REF_IY, { memory contents at address in register IY }
  74. OT_REF_IX_d, { memory contents at address in register IX+d, d is in [-128..127] }
  75. OT_REF_IY_d); { memory contents at address in register IY+d, d is in [-128..127] }
  76. tinsentry = record
  77. opcode : tasmop;
  78. ops : byte;
  79. optypes : array[0..max_operands-1] of toperandtype;
  80. code : array[0..maxinfolen] of char;
  81. flags : longint;
  82. end;
  83. pinsentry=^tinsentry;
  84. { taicpu }
  85. taicpu = class(tai_cpu_abstract_sym)
  86. constructor op_none(op : tasmop);
  87. constructor op_reg(op : tasmop;_op1 : tregister);
  88. constructor op_const(op : tasmop;_op1 : LongInt);
  89. constructor op_ref(op : tasmop;const _op1 : treference);
  90. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  91. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  92. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  93. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  94. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  95. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  96. { this is for Jmp instructions }
  97. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  98. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  99. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  100. procedure loadbool(opidx:longint;_b:boolean);
  101. { register allocation }
  102. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  103. { register spilling code }
  104. function spilling_get_operation_type(opnr: longint): topertype;override;
  105. end;
  106. tai_align = class(tai_align_abstract)
  107. { nothing to add }
  108. end;
  109. procedure InitAsm;
  110. procedure DoneAsm;
  111. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  112. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  113. function is_ref_bc(const ref:treference): Boolean;
  114. function is_ref_de(const ref:treference): Boolean;
  115. function is_ref_hl(const ref:treference): Boolean;
  116. function is_ref_sp(const ref:treference): Boolean;
  117. function is_ref_ix(const ref:treference): Boolean;
  118. function is_ref_iy(const ref:treference): Boolean;
  119. function is_ref_ix_d(const ref:treference): Boolean;
  120. function is_ref_iy_d(const ref:treference): Boolean;
  121. implementation
  122. {****************************************************************************
  123. Instruction table
  124. *****************************************************************************}
  125. const
  126. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  127. {*****************************************************************************
  128. taicpu Constructors
  129. *****************************************************************************}
  130. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  131. begin
  132. if opidx>=ops then
  133. ops:=opidx+1;
  134. with oper[opidx]^ do
  135. begin
  136. if typ=top_ref then
  137. dispose(ref);
  138. b:=_b;
  139. typ:=top_bool;
  140. end;
  141. end;
  142. constructor taicpu.op_none(op : tasmop);
  143. begin
  144. inherited create(op);
  145. end;
  146. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  147. begin
  148. inherited create(op);
  149. ops:=1;
  150. loadreg(0,_op1);
  151. end;
  152. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  153. begin
  154. inherited create(op);
  155. ops:=1;
  156. loadref(0,_op1);
  157. end;
  158. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  159. begin
  160. inherited create(op);
  161. ops:=1;
  162. loadconst(0,_op1);
  163. end;
  164. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  165. begin
  166. inherited create(op);
  167. ops:=2;
  168. loadreg(0,_op1);
  169. loadreg(1,_op2);
  170. end;
  171. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  172. begin
  173. inherited create(op);
  174. ops:=2;
  175. loadreg(0,_op1);
  176. loadconst(1,_op2);
  177. end;
  178. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  179. begin
  180. inherited create(op);
  181. ops:=2;
  182. loadconst(0,_op1);
  183. loadreg(1,_op2);
  184. end;
  185. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  186. begin
  187. inherited create(op);
  188. ops:=2;
  189. loadreg(0,_op1);
  190. loadref(1,_op2);
  191. end;
  192. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  193. begin
  194. inherited create(op);
  195. ops:=2;
  196. loadref(0,_op1);
  197. loadreg(1,_op2);
  198. end;
  199. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  200. begin
  201. inherited create(op);
  202. ops:=2;
  203. loadref(0,_op1);
  204. loadconst(1,_op2);
  205. end;
  206. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  207. begin
  208. inherited create(op);
  209. is_jmp:=op in jmp_instructions;
  210. condition:=cond;
  211. ops:=1;
  212. loadsymbol(0,_op1,0);
  213. end;
  214. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  215. begin
  216. inherited create(op);
  217. is_jmp:=op in jmp_instructions;
  218. ops:=1;
  219. loadsymbol(0,_op1,0);
  220. end;
  221. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  222. begin
  223. inherited create(op);
  224. ops:=1;
  225. loadsymbol(0,_op1,_op1ofs);
  226. end;
  227. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  228. begin
  229. result:=(
  230. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  231. ) and
  232. (ops=2) and
  233. (oper[0]^.typ=top_reg) and
  234. (oper[1]^.typ=top_reg) and
  235. (oper[0]^.reg=oper[1]^.reg);
  236. end;
  237. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  238. begin
  239. result:=operand_read;
  240. case opcode of
  241. A_LD,
  242. A_POP:
  243. if opnr=0 then
  244. result:=operand_write;
  245. A_PUSH,
  246. A_BIT,
  247. A_DJNZ,
  248. A_JR,
  249. A_JP:
  250. ;
  251. A_SET:
  252. if opnr=1 then
  253. result:=operand_readwrite;
  254. A_EX:
  255. result:=operand_readwrite;
  256. else
  257. begin
  258. if opnr=0 then
  259. result:=operand_readwrite;
  260. end;
  261. end;
  262. end;
  263. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  264. begin
  265. case getregtype(r) of
  266. R_INTREGISTER :
  267. result:=taicpu.op_reg_ref(A_LD,r,ref)
  268. else
  269. internalerror(200401041);
  270. end;
  271. end;
  272. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  273. begin
  274. case getregtype(r) of
  275. R_INTREGISTER :
  276. result:=taicpu.op_ref_reg(A_LD,ref,r);
  277. else
  278. internalerror(200401041);
  279. end;
  280. end;
  281. function is_ref_bc(const ref: treference): Boolean;
  282. begin
  283. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  284. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  285. (ref.offset=0) and (ref.scalefactor<=1) and
  286. (ref.symbol=nil) and (ref.relsymbol=nil);
  287. end;
  288. function is_ref_de(const ref: treference): Boolean;
  289. begin
  290. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  291. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  292. (ref.offset=0) and (ref.scalefactor<=1) and
  293. (ref.symbol=nil) and (ref.relsymbol=nil);
  294. end;
  295. function is_ref_hl(const ref: treference): Boolean;
  296. begin
  297. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  298. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  299. (ref.offset=0) and (ref.scalefactor<=1) and
  300. (ref.symbol=nil) and (ref.relsymbol=nil);
  301. end;
  302. function is_ref_sp(const ref: treference): Boolean;
  303. begin
  304. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  305. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  306. (ref.offset=0) and (ref.scalefactor<=1) and
  307. (ref.symbol=nil) and (ref.relsymbol=nil);
  308. end;
  309. function is_ref_ix(const ref: treference): Boolean;
  310. begin
  311. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  312. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  313. (ref.offset=0) and (ref.scalefactor<=1) and
  314. (ref.symbol=nil) and (ref.relsymbol=nil);
  315. end;
  316. function is_ref_iy(const ref: treference): Boolean;
  317. begin
  318. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  319. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  320. (ref.offset=0) and (ref.scalefactor<=1) and
  321. (ref.symbol=nil) and (ref.relsymbol=nil);
  322. end;
  323. function is_ref_ix_d(const ref: treference): Boolean;
  324. begin
  325. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  326. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  327. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  328. (ref.symbol=nil) and (ref.relsymbol=nil);
  329. end;
  330. function is_ref_iy_d(const ref: treference): Boolean;
  331. begin
  332. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  333. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  334. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  335. (ref.symbol=nil) and (ref.relsymbol=nil);
  336. end;
  337. procedure InitAsm;
  338. begin
  339. end;
  340. procedure DoneAsm;
  341. begin
  342. end;
  343. begin
  344. cai_cpu:=taicpu;
  345. cai_align:=tai_align;
  346. end.