aasmcpu.pas 30 KB

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