aasmcpu.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. { todo: lo8, hi8 addresses }
  174. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=255);
  175. OT_IMM16:
  176. { todo: addresses }
  177. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=65535);
  178. OT_IMM_VAL0:
  179. result:=(oper.typ=top_const) and (oper.val=0);
  180. OT_IMM_VAL1:
  181. result:=(oper.typ=top_const) and (oper.val=1);
  182. OT_IMM_VAL2:
  183. result:=(oper.typ=top_const) and (oper.val=2);
  184. OT_IMM_RST:
  185. result:=(oper.typ=top_const) and ((oper.val=$00) or (oper.val=$08) or
  186. (oper.val=$10) or (oper.val=$18) or
  187. (oper.val=$20) or (oper.val=$28) or
  188. (oper.val=$30) or (oper.val=$38));
  189. OT_IMM_PORT:
  190. result:=(oper.typ=top_ref) and
  191. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  192. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO) and
  193. (oper.ref^.offset>=0) and (oper.ref^.offset<=255);
  194. OT_REG8:
  195. result:=(oper.typ=top_reg) and ((oper.reg=NR_A) or (oper.reg=NR_B) or
  196. (oper.reg=NR_C) or (oper.reg=NR_D) or
  197. (oper.reg=NR_E) or (oper.reg=NR_H) or
  198. (oper.reg=NR_L));
  199. OT_REG8_A:
  200. result:=(oper.typ=top_reg) and (oper.reg=NR_A);
  201. OT_REG8_I:
  202. result:=(oper.typ=top_reg) and (oper.reg=NR_I);
  203. OT_REG8_R:
  204. result:=(oper.typ=top_reg) and (oper.reg=NR_R);
  205. OT_REG8_C_PORT:
  206. result:=(oper.typ=top_ref) and
  207. (((oper.ref^.base=NR_C) and (oper.ref^.index=NR_NO)) or
  208. ((oper.ref^.base=NR_NO) and (oper.ref^.index=NR_C))) and
  209. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  210. (oper.ref^.offset=0);
  211. OT_REG16_IX:
  212. result:=(oper.typ=top_reg) and (oper.reg=NR_IX);
  213. OT_REG16_IY:
  214. result:=(oper.typ=top_reg) and (oper.reg=NR_IY);
  215. OT_REG16_SP:
  216. result:=(oper.typ=top_reg) and (oper.reg=NR_SP);
  217. OT_REG16_BC_DE_HL_SP:
  218. 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));
  219. OT_REG16_BC_DE_HL_AF:
  220. 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));
  221. OT_REG16_BC_DE_IX_SP:
  222. 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));
  223. OT_REG16_BC_DE_IY_SP:
  224. 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));
  225. OT_REG16_DE:
  226. result:=(oper.typ=top_reg) and (oper.reg=NR_DE);
  227. OT_REG16_HL:
  228. result:=(oper.typ=top_reg) and (oper.reg=NR_HL);
  229. OT_REG16_AF:
  230. result:=(oper.typ=top_reg) and (oper.reg=NR_AF);
  231. OT_REG16_AF_:
  232. result:=(oper.typ=top_reg) and (oper.reg=NR_AF_);
  233. {todo: OT_RELJMP8}
  234. OT_REF_ADDR16,
  235. OT_REF_BC,
  236. OT_REF_DE,
  237. OT_REF_HL,
  238. OT_REF_SP,
  239. OT_REF_IX,
  240. OT_REF_IY,
  241. OT_REF_IX_d,
  242. OT_REF_IY_d:
  243. result:=(oper.typ=top_ref) and is_ref_opertype(oper.ref^,ot);
  244. else
  245. internalerror(2020042901);
  246. end;
  247. end;
  248. var
  249. i: Integer;
  250. begin
  251. result:=false;
  252. { Check the opcode }
  253. if p^.opcode<>opcode then
  254. exit;
  255. { The opcode doesn't support conditions, but we have a condition?
  256. That's an invalid instruction, don't match it against anything. }
  257. if (condition<>C_NONE) and not (opcode in cond_instructions) then
  258. exit;
  259. { if our opcode supports a condition, but our operation doesn't have
  260. one, and we're matching it with an instruction entry 'p' that has a
  261. condition, then it doesn't match }
  262. if (opcode in cond_instructions) and (condition=C_None) and
  263. (p^.ops>0) and (p^.optypes[0] in [OT_COND..OT_COND_NZ]) then
  264. exit;
  265. { instruction has a condition? }
  266. if (opcode in cond_instructions) and (condition<>C_None) then
  267. begin
  268. { Check the operand count }
  269. if p^.ops<>(ops+1) then
  270. exit;
  271. { Check the condition }
  272. case p^.optypes[0] of
  273. OT_COND:
  274. { any condition accepted };
  275. OT_COND_C:
  276. if condition<>C_C then
  277. exit;
  278. OT_COND_NC:
  279. if condition<>C_NC then
  280. exit;
  281. OT_COND_Z:
  282. if condition<>C_Z then
  283. exit;
  284. OT_COND_NZ:
  285. if condition<>C_NZ then
  286. exit;
  287. else
  288. { no condition in 'p'? Then it's not a match! }
  289. exit;
  290. end;
  291. { Check the operands }
  292. for i:=1 to p^.ops-1 do
  293. if not OperandsMatch(oper[i-1]^,p^.optypes[i]) then
  294. exit;
  295. end
  296. else
  297. { no condition }
  298. begin
  299. { Check the operand count }
  300. if p^.ops<>ops then
  301. exit;
  302. { Check the operands }
  303. for i:=0 to p^.ops-1 do
  304. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  305. exit;
  306. end;
  307. result:=true;
  308. end;
  309. function taicpu.FindInsentry(objdata: TObjData): boolean;
  310. var
  311. i : longint;
  312. begin
  313. result:=false;
  314. { Things which may only be done once, not when a second pass is done to
  315. optimize }
  316. if (Insentry=nil) {or (IF_PASS2 in InsEntry^.flags)} then
  317. begin
  318. { set the file postion }
  319. current_filepos:=fileinfo;
  320. end
  321. else
  322. begin
  323. { we've already an insentry so it's valid }
  324. result:=true;
  325. exit;
  326. end;
  327. { Lookup opcode in the table }
  328. InsSize:=-1;
  329. i:=instabcache^[opcode];
  330. if i=-1 then
  331. begin
  332. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  333. exit;
  334. end;
  335. insentry:=@instab[i];
  336. while (insentry^.opcode=opcode) do
  337. begin
  338. if matches(insentry) then
  339. begin
  340. result:=true;
  341. exit;
  342. end;
  343. inc(insentry);
  344. end;
  345. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  346. { No instruction found, set insentry to nil and inssize to -1 }
  347. insentry:=nil;
  348. inssize:=-1;
  349. end;
  350. constructor taicpu.op_none(op : tasmop);
  351. begin
  352. inherited create(op);
  353. end;
  354. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  355. begin
  356. inherited create(op);
  357. ops:=1;
  358. loadreg(0,_op1);
  359. end;
  360. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  361. begin
  362. inherited create(op);
  363. ops:=1;
  364. loadref(0,_op1);
  365. end;
  366. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  367. begin
  368. inherited create(op);
  369. ops:=1;
  370. loadconst(0,_op1);
  371. end;
  372. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  373. begin
  374. inherited create(op);
  375. ops:=2;
  376. loadreg(0,_op1);
  377. loadreg(1,_op2);
  378. end;
  379. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  380. begin
  381. inherited create(op);
  382. ops:=2;
  383. loadreg(0,_op1);
  384. loadconst(1,_op2);
  385. end;
  386. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  387. begin
  388. inherited create(op);
  389. ops:=2;
  390. loadconst(0,_op1);
  391. loadreg(1,_op2);
  392. end;
  393. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  394. begin
  395. inherited create(op);
  396. ops:=2;
  397. loadreg(0,_op1);
  398. loadref(1,_op2);
  399. end;
  400. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  401. begin
  402. inherited create(op);
  403. ops:=2;
  404. loadref(0,_op1);
  405. loadreg(1,_op2);
  406. end;
  407. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  408. begin
  409. inherited create(op);
  410. ops:=2;
  411. loadref(0,_op1);
  412. loadconst(1,_op2);
  413. end;
  414. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  415. begin
  416. inherited create(op);
  417. is_jmp:=op in jmp_instructions;
  418. condition:=cond;
  419. ops:=1;
  420. loadsymbol(0,_op1,0);
  421. end;
  422. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  423. begin
  424. inherited create(op);
  425. is_jmp:=op in jmp_instructions;
  426. ops:=1;
  427. loadsymbol(0,_op1,0);
  428. end;
  429. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  430. begin
  431. inherited create(op);
  432. ops:=1;
  433. loadsymbol(0,_op1,_op1ofs);
  434. end;
  435. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  436. begin
  437. result:=(
  438. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  439. ) and
  440. (ops=2) and
  441. (oper[0]^.typ=top_reg) and
  442. (oper[1]^.typ=top_reg) and
  443. (oper[0]^.reg=oper[1]^.reg);
  444. end;
  445. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  446. begin
  447. result:=operand_read;
  448. case opcode of
  449. A_LD,
  450. A_POP:
  451. if opnr=0 then
  452. result:=operand_write;
  453. A_PUSH,
  454. A_BIT,
  455. A_CP,
  456. A_DJNZ,
  457. A_JR,
  458. A_JP,
  459. A_CALL,
  460. A_RET,
  461. A_RETI,
  462. A_RETN,
  463. A_RST,
  464. A_IM:
  465. ;
  466. A_SET,
  467. A_RES:
  468. if opnr=1 then
  469. result:=operand_readwrite;
  470. A_EX:
  471. result:=operand_readwrite;
  472. else
  473. begin
  474. if opnr=0 then
  475. result:=operand_readwrite;
  476. end;
  477. end;
  478. end;
  479. function taicpu.CheckIfValid: boolean;
  480. begin
  481. result:=FindInsEntry(nil);
  482. end;
  483. function taicpu.GetString: string;
  484. var
  485. //i : longint;
  486. s : string;
  487. //regnr: string;
  488. //addsize : boolean;
  489. begin
  490. s:='['+std_op2str[opcode];
  491. //for i:=0 to ops-1 do
  492. // begin
  493. // with oper[i]^ do
  494. // begin
  495. // if i=0 then
  496. // s:=s+' '
  497. // else
  498. // s:=s+',';
  499. // { type }
  500. // addsize:=false;
  501. //
  502. // regnr := '';
  503. // if getregtype(reg) = R_MMREGISTER then
  504. // str(getsupreg(reg),regnr);
  505. //
  506. // if (ot and OT_XMMREG)=OT_XMMREG then
  507. // s:=s+'xmmreg' + regnr
  508. // else
  509. // if (ot and OT_YMMREG)=OT_YMMREG then
  510. // s:=s+'ymmreg' + regnr
  511. // else
  512. // if (ot and OT_ZMMREG)=OT_ZMMREG then
  513. // s:=s+'zmmreg' + regnr
  514. //
  515. // else
  516. // if (ot and OT_REG_EXTRA_MASK)=OT_MMXREG then
  517. // s:=s+'mmxreg'
  518. // else
  519. // if (ot and OT_REG_EXTRA_MASK)=OT_FPUREG then
  520. // s:=s+'fpureg'
  521. // else
  522. // if (ot and OT_REGISTER)=OT_REGISTER then
  523. // begin
  524. // s:=s+'reg';
  525. // addsize:=true;
  526. // end
  527. // else
  528. // if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  529. // begin
  530. // s:=s+'imm';
  531. // addsize:=true;
  532. // end
  533. // else
  534. // if (ot and OT_MEMORY)=OT_MEMORY then
  535. // begin
  536. // s:=s+'mem';
  537. // addsize:=true;
  538. // end
  539. // else
  540. // s:=s+'???';
  541. // { size }
  542. // if addsize then
  543. // begin
  544. // if (ot and OT_BITS8)<>0 then
  545. // s:=s+'8'
  546. // else
  547. // if (ot and OT_BITS16)<>0 then
  548. // s:=s+'16'
  549. // else
  550. // if (ot and OT_BITS32)<>0 then
  551. // s:=s+'32'
  552. // else
  553. // if (ot and OT_BITS64)<>0 then
  554. // s:=s+'64'
  555. // else
  556. // if (ot and OT_BITS128)<>0 then
  557. // s:=s+'128'
  558. // else
  559. // if (ot and OT_BITS256)<>0 then
  560. // s:=s+'256'
  561. // else
  562. // if (ot and OT_BITS512)<>0 then
  563. // s:=s+'512'
  564. // else
  565. // s:=s+'??';
  566. // { signed }
  567. // if (ot and OT_SIGNED)<>0 then
  568. // s:=s+'s';
  569. // end;
  570. //
  571. // if vopext <> 0 then
  572. // begin
  573. // str(vopext and $07, regnr);
  574. // if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  575. // s := s + ' {k' + regnr + '}';
  576. //
  577. // if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  578. // s := s + ' {z}';
  579. //
  580. // if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  581. // s := s + ' {sae}';
  582. //
  583. //
  584. // if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  585. // case vopext and OTVE_VECTOR_BCST_MASK of
  586. // OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  587. // OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  588. // OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  589. // OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  590. // end;
  591. //
  592. // if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  593. // case vopext and OTVE_VECTOR_ER_MASK of
  594. // OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  595. // OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  596. // OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  597. // OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  598. // end;
  599. //
  600. // end;
  601. // end;
  602. // end;
  603. GetString:=s+']';
  604. end;
  605. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  606. begin
  607. case getregtype(r) of
  608. R_INTREGISTER :
  609. result:=taicpu.op_reg_ref(A_LD,r,ref)
  610. else
  611. internalerror(200401041);
  612. end;
  613. end;
  614. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  615. begin
  616. case getregtype(r) of
  617. R_INTREGISTER :
  618. result:=taicpu.op_ref_reg(A_LD,ref,r);
  619. else
  620. internalerror(200401041);
  621. end;
  622. end;
  623. function is_ref_addr16(const ref: treference): Boolean;
  624. begin
  625. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  626. end;
  627. function is_ref_bc(const ref: treference): Boolean;
  628. begin
  629. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  630. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  631. (ref.offset=0) and (ref.scalefactor<=1) and
  632. (ref.symbol=nil) and (ref.relsymbol=nil);
  633. end;
  634. function is_ref_de(const ref: treference): Boolean;
  635. begin
  636. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  637. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  638. (ref.offset=0) and (ref.scalefactor<=1) and
  639. (ref.symbol=nil) and (ref.relsymbol=nil);
  640. end;
  641. function is_ref_hl(const ref: treference): Boolean;
  642. begin
  643. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  644. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  645. (ref.offset=0) and (ref.scalefactor<=1) and
  646. (ref.symbol=nil) and (ref.relsymbol=nil);
  647. end;
  648. function is_ref_sp(const ref: treference): Boolean;
  649. begin
  650. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  651. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  652. (ref.offset=0) and (ref.scalefactor<=1) and
  653. (ref.symbol=nil) and (ref.relsymbol=nil);
  654. end;
  655. function is_ref_ix(const ref: treference): Boolean;
  656. begin
  657. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  658. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  659. (ref.offset=0) and (ref.scalefactor<=1) and
  660. (ref.symbol=nil) and (ref.relsymbol=nil);
  661. end;
  662. function is_ref_iy(const ref: treference): Boolean;
  663. begin
  664. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  665. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  666. (ref.offset=0) and (ref.scalefactor<=1) and
  667. (ref.symbol=nil) and (ref.relsymbol=nil);
  668. end;
  669. function is_ref_ix_d(const ref: treference): Boolean;
  670. begin
  671. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  672. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  673. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  674. (ref.symbol=nil) and (ref.relsymbol=nil);
  675. end;
  676. function is_ref_iy_d(const ref: treference): Boolean;
  677. begin
  678. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  679. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  680. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  681. (ref.symbol=nil) and (ref.relsymbol=nil);
  682. end;
  683. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  684. begin
  685. case opertype of
  686. OT_REF_ADDR16:
  687. result:=is_ref_addr16(ref);
  688. OT_REF_BC:
  689. result:=is_ref_bc(ref);
  690. OT_REF_DE:
  691. result:=is_ref_de(ref);
  692. OT_REF_HL:
  693. result:=is_ref_hl(ref);
  694. OT_REF_SP:
  695. result:=is_ref_sp(ref);
  696. OT_REF_IX:
  697. result:=is_ref_ix(ref);
  698. OT_REF_IY:
  699. result:=is_ref_iy(ref);
  700. OT_REF_IX_d:
  701. result:=is_ref_ix_d(ref);
  702. OT_REF_IY_d:
  703. result:=is_ref_iy_d(ref);
  704. else
  705. internalerror(2020041801);
  706. end;
  707. end;
  708. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  709. var
  710. ot: trefoperandtype;
  711. begin
  712. result:=true;
  713. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  714. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  715. exit;
  716. result:=false;
  717. end;
  718. {****************************************************************************
  719. Instruction table
  720. *****************************************************************************}
  721. procedure BuildInsTabCache;
  722. var
  723. i : longint;
  724. begin
  725. new(instabcache);
  726. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  727. i:=0;
  728. while (i<InsTabEntries) do
  729. begin
  730. if InsTabCache^[InsTab[i].OPcode]=-1 then
  731. InsTabCache^[InsTab[i].OPcode]:=i;
  732. inc(i);
  733. end;
  734. end;
  735. procedure InitAsm;
  736. begin
  737. if not assigned(instabcache) then
  738. BuildInsTabCache;
  739. end;
  740. procedure DoneAsm;
  741. begin
  742. if assigned(instabcache) then
  743. begin
  744. dispose(instabcache);
  745. instabcache:=nil;
  746. end;
  747. end;
  748. begin
  749. cai_cpu:=taicpu;
  750. cai_align:=tai_align;
  751. end.