aasmcpu.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. {
  2. Copyright (c) 1999-2008 by Mazen Neifer and Florian Klaempfl
  3. Contains the assembler object for the Z80
  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. timmoperandtype = OT_IMM3..OT_IMM_PORT;
  77. tregoperandtype = OT_REG8..OT_REG16_AF_;
  78. treg8operandtype = OT_REG8..OT_REG8_C_PORT;
  79. treg16operandtype = OT_REG16_IX..OT_REG16_AF_;
  80. tcondoperandtype = OT_COND..OT_COND_NZ;
  81. trefoperandtype = OT_REF_ADDR16..OT_REF_IY_d;
  82. trefoperandtypes = set of trefoperandtype;
  83. tinsentry = record
  84. opcode : tasmop;
  85. ops : byte;
  86. optypes : array[0..max_operands-1] of toperandtype;
  87. code : array[0..maxinfolen] of char;
  88. flags : longint;
  89. end;
  90. pinsentry=^tinsentry;
  91. { taicpu }
  92. taicpu = class(tai_cpu_abstract_sym)
  93. constructor op_none(op : tasmop);
  94. constructor op_reg(op : tasmop;_op1 : tregister);
  95. constructor op_const(op : tasmop;_op1 : LongInt);
  96. constructor op_ref(op : tasmop;const _op1 : treference);
  97. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  98. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  99. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  100. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  101. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  102. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  103. { this is for Jmp instructions }
  104. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  105. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  106. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  107. procedure loadbool(opidx:longint;_b:boolean);
  108. { register allocation }
  109. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  110. { register spilling code }
  111. function spilling_get_operation_type(opnr: longint): topertype;override;
  112. end;
  113. tai_align = class(tai_align_abstract)
  114. { nothing to add }
  115. end;
  116. procedure InitAsm;
  117. procedure DoneAsm;
  118. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  119. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  120. function is_ref_addr16(const ref:treference): Boolean;
  121. function is_ref_bc(const ref:treference): Boolean;
  122. function is_ref_de(const ref:treference): Boolean;
  123. function is_ref_hl(const ref:treference): Boolean;
  124. function is_ref_sp(const ref:treference): Boolean;
  125. function is_ref_ix(const ref:treference): Boolean;
  126. function is_ref_iy(const ref:treference): Boolean;
  127. function is_ref_ix_d(const ref:treference): Boolean;
  128. function is_ref_iy_d(const ref:treference): Boolean;
  129. function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  130. function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  131. implementation
  132. {****************************************************************************
  133. Instruction table
  134. *****************************************************************************}
  135. type
  136. TInsTabCache=array[TasmOp] of longint;
  137. PInsTabCache=^TInsTabCache;
  138. const
  139. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  140. var
  141. InsTabCache : PInsTabCache;
  142. {*****************************************************************************
  143. taicpu Constructors
  144. *****************************************************************************}
  145. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  146. begin
  147. if opidx>=ops then
  148. ops:=opidx+1;
  149. with oper[opidx]^ do
  150. begin
  151. if typ=top_ref then
  152. dispose(ref);
  153. b:=_b;
  154. typ:=top_bool;
  155. end;
  156. end;
  157. constructor taicpu.op_none(op : tasmop);
  158. begin
  159. inherited create(op);
  160. end;
  161. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  162. begin
  163. inherited create(op);
  164. ops:=1;
  165. loadreg(0,_op1);
  166. end;
  167. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  168. begin
  169. inherited create(op);
  170. ops:=1;
  171. loadref(0,_op1);
  172. end;
  173. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  174. begin
  175. inherited create(op);
  176. ops:=1;
  177. loadconst(0,_op1);
  178. end;
  179. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  180. begin
  181. inherited create(op);
  182. ops:=2;
  183. loadreg(0,_op1);
  184. loadreg(1,_op2);
  185. end;
  186. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  187. begin
  188. inherited create(op);
  189. ops:=2;
  190. loadreg(0,_op1);
  191. loadconst(1,_op2);
  192. end;
  193. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  194. begin
  195. inherited create(op);
  196. ops:=2;
  197. loadconst(0,_op1);
  198. loadreg(1,_op2);
  199. end;
  200. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  201. begin
  202. inherited create(op);
  203. ops:=2;
  204. loadreg(0,_op1);
  205. loadref(1,_op2);
  206. end;
  207. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  208. begin
  209. inherited create(op);
  210. ops:=2;
  211. loadref(0,_op1);
  212. loadreg(1,_op2);
  213. end;
  214. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  215. begin
  216. inherited create(op);
  217. ops:=2;
  218. loadref(0,_op1);
  219. loadconst(1,_op2);
  220. end;
  221. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  222. begin
  223. inherited create(op);
  224. is_jmp:=op in jmp_instructions;
  225. condition:=cond;
  226. ops:=1;
  227. loadsymbol(0,_op1,0);
  228. end;
  229. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  230. begin
  231. inherited create(op);
  232. is_jmp:=op in jmp_instructions;
  233. ops:=1;
  234. loadsymbol(0,_op1,0);
  235. end;
  236. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  237. begin
  238. inherited create(op);
  239. ops:=1;
  240. loadsymbol(0,_op1,_op1ofs);
  241. end;
  242. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  243. begin
  244. result:=(
  245. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  246. ) and
  247. (ops=2) and
  248. (oper[0]^.typ=top_reg) and
  249. (oper[1]^.typ=top_reg) and
  250. (oper[0]^.reg=oper[1]^.reg);
  251. end;
  252. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  253. begin
  254. result:=operand_read;
  255. case opcode of
  256. A_LD,
  257. A_POP:
  258. if opnr=0 then
  259. result:=operand_write;
  260. A_PUSH,
  261. A_BIT,
  262. A_CP,
  263. A_DJNZ,
  264. A_JR,
  265. A_JP,
  266. A_CALL,
  267. A_RET,
  268. A_RETI,
  269. A_RETN,
  270. A_RST,
  271. A_IM:
  272. ;
  273. A_SET,
  274. A_RES:
  275. if opnr=1 then
  276. result:=operand_readwrite;
  277. A_EX:
  278. result:=operand_readwrite;
  279. else
  280. begin
  281. if opnr=0 then
  282. result:=operand_readwrite;
  283. end;
  284. end;
  285. end;
  286. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  287. begin
  288. case getregtype(r) of
  289. R_INTREGISTER :
  290. result:=taicpu.op_reg_ref(A_LD,r,ref)
  291. else
  292. internalerror(200401041);
  293. end;
  294. end;
  295. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  296. begin
  297. case getregtype(r) of
  298. R_INTREGISTER :
  299. result:=taicpu.op_ref_reg(A_LD,ref,r);
  300. else
  301. internalerror(200401041);
  302. end;
  303. end;
  304. function is_ref_addr16(const ref: treference): Boolean;
  305. begin
  306. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  307. end;
  308. function is_ref_bc(const ref: treference): Boolean;
  309. begin
  310. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  311. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  312. (ref.offset=0) and (ref.scalefactor<=1) and
  313. (ref.symbol=nil) and (ref.relsymbol=nil);
  314. end;
  315. function is_ref_de(const ref: treference): Boolean;
  316. begin
  317. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  318. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  319. (ref.offset=0) and (ref.scalefactor<=1) and
  320. (ref.symbol=nil) and (ref.relsymbol=nil);
  321. end;
  322. function is_ref_hl(const ref: treference): Boolean;
  323. begin
  324. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  325. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  326. (ref.offset=0) and (ref.scalefactor<=1) and
  327. (ref.symbol=nil) and (ref.relsymbol=nil);
  328. end;
  329. function is_ref_sp(const ref: treference): Boolean;
  330. begin
  331. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  332. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  333. (ref.offset=0) and (ref.scalefactor<=1) and
  334. (ref.symbol=nil) and (ref.relsymbol=nil);
  335. end;
  336. function is_ref_ix(const ref: treference): Boolean;
  337. begin
  338. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  339. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  340. (ref.offset=0) and (ref.scalefactor<=1) and
  341. (ref.symbol=nil) and (ref.relsymbol=nil);
  342. end;
  343. function is_ref_iy(const ref: treference): Boolean;
  344. begin
  345. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  346. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  347. (ref.offset=0) and (ref.scalefactor<=1) and
  348. (ref.symbol=nil) and (ref.relsymbol=nil);
  349. end;
  350. function is_ref_ix_d(const ref: treference): Boolean;
  351. begin
  352. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  353. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  354. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  355. (ref.symbol=nil) and (ref.relsymbol=nil);
  356. end;
  357. function is_ref_iy_d(const ref: treference): Boolean;
  358. begin
  359. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  360. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  361. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  362. (ref.symbol=nil) and (ref.relsymbol=nil);
  363. end;
  364. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  365. begin
  366. case opertype of
  367. OT_REF_ADDR16:
  368. result:=is_ref_addr16(ref);
  369. OT_REF_BC:
  370. result:=is_ref_bc(ref);
  371. OT_REF_DE:
  372. result:=is_ref_de(ref);
  373. OT_REF_HL:
  374. result:=is_ref_hl(ref);
  375. OT_REF_SP:
  376. result:=is_ref_sp(ref);
  377. OT_REF_IX:
  378. result:=is_ref_ix(ref);
  379. OT_REF_IY:
  380. result:=is_ref_iy(ref);
  381. OT_REF_IX_d:
  382. result:=is_ref_ix_d(ref);
  383. OT_REF_IY_d:
  384. result:=is_ref_iy_d(ref);
  385. else
  386. internalerror(2020041801);
  387. end;
  388. end;
  389. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  390. var
  391. ot: trefoperandtype;
  392. begin
  393. result:=true;
  394. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  395. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  396. exit;
  397. result:=false;
  398. end;
  399. {****************************************************************************
  400. Instruction table
  401. *****************************************************************************}
  402. procedure BuildInsTabCache;
  403. var
  404. i : longint;
  405. begin
  406. new(instabcache);
  407. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  408. i:=0;
  409. while (i<InsTabEntries) do
  410. begin
  411. if InsTabCache^[InsTab[i].OPcode]=-1 then
  412. InsTabCache^[InsTab[i].OPcode]:=i;
  413. inc(i);
  414. end;
  415. end;
  416. procedure InitAsm;
  417. begin
  418. if not assigned(instabcache) then
  419. BuildInsTabCache;
  420. end;
  421. procedure DoneAsm;
  422. begin
  423. if assigned(instabcache) then
  424. begin
  425. dispose(instabcache);
  426. instabcache:=nil;
  427. end;
  428. end;
  429. begin
  430. cai_cpu:=taicpu;
  431. cai_align:=tai_align;
  432. end.