aasmcpu.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. private
  94. { next fields are filled in pass1, so pass2 is faster }
  95. insentry : PInsEntry;
  96. inssize : shortint;
  97. function Matches(p:PInsEntry):boolean;
  98. function FindInsentry(objdata:TObjData):boolean;
  99. public
  100. constructor op_none(op : tasmop);
  101. constructor op_reg(op : tasmop;_op1 : tregister);
  102. constructor op_const(op : tasmop;_op1 : LongInt);
  103. constructor op_ref(op : tasmop;const _op1 : treference);
  104. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  105. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  106. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  107. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  108. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  109. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  110. { this is for Jmp instructions }
  111. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  112. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  113. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  114. procedure loadbool(opidx:longint;_b:boolean);
  115. { register allocation }
  116. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  117. { register spilling code }
  118. function spilling_get_operation_type(opnr: longint): topertype;override;
  119. function CheckIfValid:boolean;
  120. function GetString:string;
  121. end;
  122. tai_align = class(tai_align_abstract)
  123. { nothing to add }
  124. end;
  125. procedure InitAsm;
  126. procedure DoneAsm;
  127. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  128. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  129. function is_ref_addr16(const ref:treference): Boolean;
  130. function is_ref_bc(const ref:treference): Boolean;
  131. function is_ref_de(const ref:treference): Boolean;
  132. function is_ref_hl(const ref:treference): Boolean;
  133. function is_ref_sp(const ref:treference): Boolean;
  134. function is_ref_ix(const ref:treference): Boolean;
  135. function is_ref_iy(const ref:treference): Boolean;
  136. function is_ref_ix_d(const ref:treference): Boolean;
  137. function is_ref_iy_d(const ref:treference): Boolean;
  138. function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  139. function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  140. implementation
  141. {****************************************************************************
  142. Instruction table
  143. *****************************************************************************}
  144. type
  145. TInsTabCache=array[TasmOp] of longint;
  146. PInsTabCache=^TInsTabCache;
  147. const
  148. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  149. var
  150. InsTabCache : PInsTabCache;
  151. {*****************************************************************************
  152. taicpu Constructors
  153. *****************************************************************************}
  154. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  155. begin
  156. if opidx>=ops then
  157. ops:=opidx+1;
  158. with oper[opidx]^ do
  159. begin
  160. if typ=top_ref then
  161. dispose(ref);
  162. b:=_b;
  163. typ:=top_bool;
  164. end;
  165. end;
  166. function taicpu.Matches(p: PInsEntry): boolean;
  167. function OperandsMatch(const oper: toper; const ot: toperandtype): boolean;
  168. begin
  169. case ot of
  170. OT_IMM3:
  171. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=7);
  172. OT_IMM8:
  173. result:=((oper.typ=top_const) and (oper.val>=0) and (oper.val<=255)) or
  174. ((oper.typ=top_ref) and
  175. (oper.ref^.refaddr in [addr_hi8,addr_lo8]) and assigned(oper.ref^.symbol) and
  176. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  177. OT_IMM16:
  178. result:=((oper.typ=top_const) and (oper.val>=0) and (oper.val<=65535)) or
  179. ((oper.typ=top_ref) and
  180. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  181. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  182. OT_IMM_VAL0:
  183. result:=(oper.typ=top_const) and (oper.val=0);
  184. OT_IMM_VAL1:
  185. result:=(oper.typ=top_const) and (oper.val=1);
  186. OT_IMM_VAL2:
  187. result:=(oper.typ=top_const) and (oper.val=2);
  188. OT_IMM_RST:
  189. result:=(oper.typ=top_const) and ((oper.val=$00) or (oper.val=$08) or
  190. (oper.val=$10) or (oper.val=$18) or
  191. (oper.val=$20) or (oper.val=$28) or
  192. (oper.val=$30) or (oper.val=$38));
  193. OT_IMM_PORT:
  194. result:=(oper.typ=top_ref) and
  195. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  196. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO) and
  197. (oper.ref^.offset>=0) and (oper.ref^.offset<=255);
  198. OT_REG8:
  199. result:=(oper.typ=top_reg) and ((oper.reg=NR_A) or (oper.reg=NR_B) or
  200. (oper.reg=NR_C) or (oper.reg=NR_D) or
  201. (oper.reg=NR_E) or (oper.reg=NR_H) or
  202. (oper.reg=NR_L));
  203. OT_REG8_A:
  204. result:=(oper.typ=top_reg) and (oper.reg=NR_A);
  205. OT_REG8_I:
  206. result:=(oper.typ=top_reg) and (oper.reg=NR_I);
  207. OT_REG8_R:
  208. result:=(oper.typ=top_reg) and (oper.reg=NR_R);
  209. OT_REG8_C_PORT:
  210. result:=(oper.typ=top_ref) and
  211. (((oper.ref^.base=NR_C) and (oper.ref^.index=NR_NO)) or
  212. ((oper.ref^.base=NR_NO) and (oper.ref^.index=NR_C))) and
  213. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  214. (oper.ref^.offset=0);
  215. OT_REG16_IX:
  216. result:=(oper.typ=top_reg) and (oper.reg=NR_IX);
  217. OT_REG16_IY:
  218. result:=(oper.typ=top_reg) and (oper.reg=NR_IY);
  219. OT_REG16_SP:
  220. result:=(oper.typ=top_reg) and (oper.reg=NR_SP);
  221. OT_REG16_BC_DE_HL_SP:
  222. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_HL) or (oper.reg=NR_SP));
  223. OT_REG16_BC_DE_HL_AF:
  224. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_HL) or (oper.reg=NR_AF));
  225. OT_REG16_BC_DE_IX_SP:
  226. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_IX) or (oper.reg=NR_SP));
  227. OT_REG16_BC_DE_IY_SP:
  228. result:=(oper.typ=top_reg) and ((oper.reg=NR_BC) or (oper.reg=NR_DE) or (oper.reg=NR_IY) or (oper.reg=NR_SP));
  229. OT_REG16_DE:
  230. result:=(oper.typ=top_reg) and (oper.reg=NR_DE);
  231. OT_REG16_HL:
  232. result:=(oper.typ=top_reg) and (oper.reg=NR_HL);
  233. OT_REG16_AF:
  234. result:=(oper.typ=top_reg) and (oper.reg=NR_AF);
  235. OT_REG16_AF_:
  236. result:=(oper.typ=top_reg) and (oper.reg=NR_AF_);
  237. OT_RELJMP8:
  238. result:=(oper.typ=top_ref) and
  239. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  240. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO);
  241. OT_REF_ADDR16,
  242. OT_REF_BC,
  243. OT_REF_DE,
  244. OT_REF_HL,
  245. OT_REF_SP,
  246. OT_REF_IX,
  247. OT_REF_IY,
  248. OT_REF_IX_d,
  249. OT_REF_IY_d:
  250. result:=(oper.typ=top_ref) and is_ref_opertype(oper.ref^,ot);
  251. else
  252. internalerror(2020042901);
  253. end;
  254. end;
  255. var
  256. i: Integer;
  257. begin
  258. result:=false;
  259. { Check the opcode }
  260. if p^.opcode<>opcode then
  261. exit;
  262. { The opcode doesn't support conditions, but we have a condition?
  263. That's an invalid instruction, don't match it against anything. }
  264. if (condition<>C_NONE) and not (opcode in cond_instructions) then
  265. exit;
  266. { if our opcode supports a condition, but our operation doesn't have
  267. one, and we're matching it with an instruction entry 'p' that has a
  268. condition, then it doesn't match }
  269. if (opcode in cond_instructions) and (condition=C_None) and
  270. (p^.ops>0) and (p^.optypes[0] in [OT_COND..OT_COND_NZ]) then
  271. exit;
  272. { instruction has a condition? }
  273. if (opcode in cond_instructions) and (condition<>C_None) then
  274. begin
  275. { Check the operand count }
  276. if p^.ops<>(ops+1) then
  277. exit;
  278. { Check the condition }
  279. case p^.optypes[0] of
  280. OT_COND:
  281. { any condition accepted };
  282. OT_COND_C:
  283. if condition<>C_C then
  284. exit;
  285. OT_COND_NC:
  286. if condition<>C_NC then
  287. exit;
  288. OT_COND_Z:
  289. if condition<>C_Z then
  290. exit;
  291. OT_COND_NZ:
  292. if condition<>C_NZ then
  293. exit;
  294. else
  295. { no condition in 'p'? Then it's not a match! }
  296. exit;
  297. end;
  298. { Check the operands }
  299. for i:=1 to p^.ops-1 do
  300. if not OperandsMatch(oper[i-1]^,p^.optypes[i]) then
  301. exit;
  302. end
  303. else
  304. { no condition }
  305. begin
  306. { Check the operand count }
  307. if p^.ops<>ops then
  308. exit;
  309. { Check the operands }
  310. for i:=0 to p^.ops-1 do
  311. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  312. exit;
  313. end;
  314. result:=true;
  315. end;
  316. function taicpu.FindInsentry(objdata: TObjData): boolean;
  317. var
  318. i : longint;
  319. begin
  320. result:=false;
  321. { Things which may only be done once, not when a second pass is done to
  322. optimize }
  323. if (Insentry=nil) {or (IF_PASS2 in InsEntry^.flags)} then
  324. begin
  325. { set the file postion }
  326. current_filepos:=fileinfo;
  327. end
  328. else
  329. begin
  330. { we've already an insentry so it's valid }
  331. result:=true;
  332. exit;
  333. end;
  334. { Lookup opcode in the table }
  335. InsSize:=-1;
  336. i:=instabcache^[opcode];
  337. if i=-1 then
  338. begin
  339. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  340. exit;
  341. end;
  342. insentry:=@instab[i];
  343. while (insentry^.opcode=opcode) do
  344. begin
  345. if matches(insentry) then
  346. begin
  347. result:=true;
  348. exit;
  349. end;
  350. inc(insentry);
  351. end;
  352. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  353. { No instruction found, set insentry to nil and inssize to -1 }
  354. insentry:=nil;
  355. inssize:=-1;
  356. end;
  357. constructor taicpu.op_none(op : tasmop);
  358. begin
  359. inherited create(op);
  360. end;
  361. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  362. begin
  363. inherited create(op);
  364. ops:=1;
  365. loadreg(0,_op1);
  366. end;
  367. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  368. begin
  369. inherited create(op);
  370. ops:=1;
  371. loadref(0,_op1);
  372. end;
  373. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  374. begin
  375. inherited create(op);
  376. ops:=1;
  377. loadconst(0,_op1);
  378. end;
  379. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  380. begin
  381. inherited create(op);
  382. ops:=2;
  383. loadreg(0,_op1);
  384. loadreg(1,_op2);
  385. end;
  386. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  387. begin
  388. inherited create(op);
  389. ops:=2;
  390. loadreg(0,_op1);
  391. loadconst(1,_op2);
  392. end;
  393. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  394. begin
  395. inherited create(op);
  396. ops:=2;
  397. loadconst(0,_op1);
  398. loadreg(1,_op2);
  399. end;
  400. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  401. begin
  402. inherited create(op);
  403. ops:=2;
  404. loadreg(0,_op1);
  405. loadref(1,_op2);
  406. end;
  407. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  408. begin
  409. inherited create(op);
  410. ops:=2;
  411. loadref(0,_op1);
  412. loadreg(1,_op2);
  413. end;
  414. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  415. begin
  416. inherited create(op);
  417. ops:=2;
  418. loadref(0,_op1);
  419. loadconst(1,_op2);
  420. end;
  421. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  422. begin
  423. inherited create(op);
  424. is_jmp:=op in jmp_instructions;
  425. condition:=cond;
  426. ops:=1;
  427. loadsymbol(0,_op1,0);
  428. end;
  429. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  430. begin
  431. inherited create(op);
  432. is_jmp:=op in jmp_instructions;
  433. ops:=1;
  434. loadsymbol(0,_op1,0);
  435. end;
  436. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  437. begin
  438. inherited create(op);
  439. ops:=1;
  440. loadsymbol(0,_op1,_op1ofs);
  441. end;
  442. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  443. begin
  444. result:=(
  445. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  446. ) and
  447. (ops=2) and
  448. (oper[0]^.typ=top_reg) and
  449. (oper[1]^.typ=top_reg) and
  450. (oper[0]^.reg=oper[1]^.reg);
  451. end;
  452. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  453. begin
  454. result:=operand_read;
  455. case opcode of
  456. A_LD,
  457. A_POP:
  458. if opnr=0 then
  459. result:=operand_write;
  460. A_PUSH,
  461. A_BIT,
  462. A_CP,
  463. A_DJNZ,
  464. A_JR,
  465. A_JP,
  466. A_CALL,
  467. A_RET,
  468. A_RETI,
  469. A_RETN,
  470. A_RST,
  471. A_IM:
  472. ;
  473. A_SET,
  474. A_RES:
  475. if opnr=1 then
  476. result:=operand_readwrite;
  477. A_EX:
  478. result:=operand_readwrite;
  479. else
  480. begin
  481. if opnr=0 then
  482. result:=operand_readwrite;
  483. end;
  484. end;
  485. end;
  486. function taicpu.CheckIfValid: boolean;
  487. begin
  488. result:=FindInsEntry(nil);
  489. end;
  490. function taicpu.GetString: string;
  491. var
  492. i : longint;
  493. s : string;
  494. first: Boolean;
  495. begin
  496. s:='['+std_op2str[opcode];
  497. for i:=0 to ops-1 do
  498. begin
  499. with oper[i]^ do
  500. begin
  501. if i=0 then
  502. begin
  503. s:=s+' ';
  504. if condition<>C_None then
  505. s:=s+cond2str[condition]+',';
  506. end
  507. else
  508. s:=s+',';
  509. case typ of
  510. top_reg:
  511. s:=s+std_regname(reg);
  512. top_const:
  513. s:=s+'const';
  514. top_ref:
  515. case ref^.refaddr of
  516. addr_full:
  517. s:=s+'addr16';
  518. addr_lo8:
  519. s:=s+'addr_lo8';
  520. addr_hi8:
  521. s:=s+'addr_hi8';
  522. addr_no:
  523. begin
  524. s:=s+'(';
  525. first:=true;
  526. if ref^.base<>NR_NO then
  527. begin
  528. first:=false;
  529. s:=s+std_regname(ref^.base);
  530. end;
  531. if ref^.index<>NR_NO then
  532. begin
  533. if not first then
  534. s:=s+'+';
  535. first:=false;
  536. s:=s+std_regname(ref^.index);
  537. end;
  538. if assigned(ref^.symbol) then
  539. begin
  540. if not first then
  541. s:=s+'+';
  542. first:=false;
  543. s:=s+'addr16';
  544. end;
  545. if ref^.offset<>0 then
  546. begin
  547. if not first then
  548. s:=s+'+';
  549. if (ref^.offset>=-128) and (ref^.offset<=127) then
  550. s:=s+'const8'
  551. else
  552. s:=s+'const16';
  553. end;
  554. s:=s+')';
  555. end;
  556. else
  557. ;
  558. end;
  559. else
  560. ;
  561. end;
  562. end;
  563. end;
  564. GetString:=s+']';
  565. end;
  566. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  567. begin
  568. case getregtype(r) of
  569. R_INTREGISTER :
  570. result:=taicpu.op_reg_ref(A_LD,r,ref)
  571. else
  572. internalerror(200401041);
  573. end;
  574. end;
  575. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  576. begin
  577. case getregtype(r) of
  578. R_INTREGISTER :
  579. result:=taicpu.op_ref_reg(A_LD,ref,r);
  580. else
  581. internalerror(200401041);
  582. end;
  583. end;
  584. function is_ref_addr16(const ref: treference): Boolean;
  585. begin
  586. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  587. end;
  588. function is_ref_bc(const ref: treference): Boolean;
  589. begin
  590. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  591. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  592. (ref.offset=0) and (ref.scalefactor<=1) and
  593. (ref.symbol=nil) and (ref.relsymbol=nil);
  594. end;
  595. function is_ref_de(const ref: treference): Boolean;
  596. begin
  597. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  598. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  599. (ref.offset=0) and (ref.scalefactor<=1) and
  600. (ref.symbol=nil) and (ref.relsymbol=nil);
  601. end;
  602. function is_ref_hl(const ref: treference): Boolean;
  603. begin
  604. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  605. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  606. (ref.offset=0) and (ref.scalefactor<=1) and
  607. (ref.symbol=nil) and (ref.relsymbol=nil);
  608. end;
  609. function is_ref_sp(const ref: treference): Boolean;
  610. begin
  611. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  612. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  613. (ref.offset=0) and (ref.scalefactor<=1) and
  614. (ref.symbol=nil) and (ref.relsymbol=nil);
  615. end;
  616. function is_ref_ix(const ref: treference): Boolean;
  617. begin
  618. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  619. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  620. (ref.offset=0) and (ref.scalefactor<=1) and
  621. (ref.symbol=nil) and (ref.relsymbol=nil);
  622. end;
  623. function is_ref_iy(const ref: treference): Boolean;
  624. begin
  625. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  626. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  627. (ref.offset=0) and (ref.scalefactor<=1) and
  628. (ref.symbol=nil) and (ref.relsymbol=nil);
  629. end;
  630. function is_ref_ix_d(const ref: treference): Boolean;
  631. begin
  632. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  633. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  634. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  635. (ref.symbol=nil) and (ref.relsymbol=nil);
  636. end;
  637. function is_ref_iy_d(const ref: treference): Boolean;
  638. begin
  639. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  640. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  641. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  642. (ref.symbol=nil) and (ref.relsymbol=nil);
  643. end;
  644. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  645. begin
  646. case opertype of
  647. OT_REF_ADDR16:
  648. result:=is_ref_addr16(ref);
  649. OT_REF_BC:
  650. result:=is_ref_bc(ref);
  651. OT_REF_DE:
  652. result:=is_ref_de(ref);
  653. OT_REF_HL:
  654. result:=is_ref_hl(ref);
  655. OT_REF_SP:
  656. result:=is_ref_sp(ref);
  657. OT_REF_IX:
  658. result:=is_ref_ix(ref);
  659. OT_REF_IY:
  660. result:=is_ref_iy(ref);
  661. OT_REF_IX_d:
  662. result:=is_ref_ix_d(ref);
  663. OT_REF_IY_d:
  664. result:=is_ref_iy_d(ref);
  665. else
  666. internalerror(2020041801);
  667. end;
  668. end;
  669. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  670. var
  671. ot: trefoperandtype;
  672. begin
  673. result:=true;
  674. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  675. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  676. exit;
  677. result:=false;
  678. end;
  679. {****************************************************************************
  680. Instruction table
  681. *****************************************************************************}
  682. procedure BuildInsTabCache;
  683. var
  684. i : longint;
  685. begin
  686. new(instabcache);
  687. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  688. i:=0;
  689. while (i<InsTabEntries) do
  690. begin
  691. if InsTabCache^[InsTab[i].OPcode]=-1 then
  692. InsTabCache^[InsTab[i].OPcode]:=i;
  693. inc(i);
  694. end;
  695. end;
  696. procedure InitAsm;
  697. begin
  698. if not assigned(instabcache) then
  699. BuildInsTabCache;
  700. end;
  701. procedure DoneAsm;
  702. begin
  703. if assigned(instabcache) then
  704. begin
  705. dispose(instabcache);
  706. instabcache:=nil;
  707. end;
  708. end;
  709. begin
  710. cai_cpu:=taicpu;
  711. cai_align:=tai_align;
  712. end.