aasmcpu.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. insoffset : longint;
  98. LastInsOffset : longint;
  99. function Matches(p:PInsEntry):boolean;
  100. function FindInsentry(objdata:TObjData):boolean;
  101. function calcsize(p:PInsEntry):shortint;
  102. procedure gencode(objdata:TObjData);
  103. public
  104. constructor op_none(op : tasmop);
  105. constructor op_reg(op : tasmop;_op1 : tregister);
  106. constructor op_const(op : tasmop;_op1 : LongInt);
  107. constructor op_ref(op : tasmop;const _op1 : treference);
  108. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  109. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  110. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  111. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  112. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  113. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  114. { this is for Jmp instructions }
  115. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  116. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  117. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  118. procedure loadbool(opidx:longint;_b:boolean);
  119. { register allocation }
  120. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  121. { register spilling code }
  122. function spilling_get_operation_type(opnr: longint): topertype;override;
  123. procedure ResetPass1;override;
  124. procedure ResetPass2;override;
  125. function Pass1(objdata:TObjData):longint;override;
  126. procedure Pass2(objdata:TObjData);override;
  127. function CheckIfValid:boolean;
  128. function GetString:string;
  129. end;
  130. tai_align = class(tai_align_abstract)
  131. { nothing to add }
  132. end;
  133. procedure InitAsm;
  134. procedure DoneAsm;
  135. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  136. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  137. function is_ref_addr16(const ref:treference): Boolean;
  138. function is_ref_bc(const ref:treference): Boolean;
  139. function is_ref_de(const ref:treference): Boolean;
  140. function is_ref_hl(const ref:treference): Boolean;
  141. function is_ref_sp(const ref:treference): Boolean;
  142. function is_ref_ix(const ref:treference): Boolean;
  143. function is_ref_iy(const ref:treference): Boolean;
  144. function is_ref_ix_d(const ref:treference): Boolean;
  145. function is_ref_iy_d(const ref:treference): Boolean;
  146. function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  147. function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  148. implementation
  149. {****************************************************************************
  150. Instruction table
  151. *****************************************************************************}
  152. type
  153. TInsTabCache=array[TasmOp] of longint;
  154. PInsTabCache=^TInsTabCache;
  155. const
  156. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  157. var
  158. InsTabCache : PInsTabCache;
  159. {*****************************************************************************
  160. taicpu Constructors
  161. *****************************************************************************}
  162. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  163. begin
  164. if opidx>=ops then
  165. ops:=opidx+1;
  166. with oper[opidx]^ do
  167. begin
  168. if typ=top_ref then
  169. dispose(ref);
  170. b:=_b;
  171. typ:=top_bool;
  172. end;
  173. end;
  174. function taicpu.Matches(p: PInsEntry): boolean;
  175. function OperandsMatch(const oper: toper; const ot: toperandtype): boolean;
  176. begin
  177. case ot of
  178. OT_IMM3:
  179. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=7);
  180. OT_IMM8:
  181. result:=((oper.typ=top_const) and (oper.val>=0) and (oper.val<=255)) or
  182. ((oper.typ=top_ref) and
  183. (oper.ref^.refaddr in [addr_hi8,addr_lo8]) and assigned(oper.ref^.symbol) and
  184. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  185. OT_IMM16:
  186. result:=((oper.typ=top_const) and (oper.val>=-32768) and (oper.val<=65535)) or
  187. ((oper.typ=top_ref) and
  188. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  189. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  190. OT_IMM_VAL0:
  191. result:=(oper.typ=top_const) and (oper.val=0);
  192. OT_IMM_VAL1:
  193. result:=(oper.typ=top_const) and (oper.val=1);
  194. OT_IMM_VAL2:
  195. result:=(oper.typ=top_const) and (oper.val=2);
  196. OT_IMM_RST:
  197. result:=(oper.typ=top_const) and ((oper.val=$00) or (oper.val=$08) or
  198. (oper.val=$10) or (oper.val=$18) or
  199. (oper.val=$20) or (oper.val=$28) or
  200. (oper.val=$30) or (oper.val=$38));
  201. OT_IMM_PORT:
  202. result:=(oper.typ=top_ref) and
  203. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  204. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO) and
  205. (oper.ref^.offset>=0) and (oper.ref^.offset<=255);
  206. OT_REG8:
  207. result:=(oper.typ=top_reg) and ((oper.reg=NR_A) or (oper.reg=NR_B) or
  208. (oper.reg=NR_C) or (oper.reg=NR_D) or
  209. (oper.reg=NR_E) or (oper.reg=NR_H) or
  210. (oper.reg=NR_L));
  211. OT_REG8_A:
  212. result:=(oper.typ=top_reg) and (oper.reg=NR_A);
  213. OT_REG8_I:
  214. result:=(oper.typ=top_reg) and (oper.reg=NR_I);
  215. OT_REG8_R:
  216. result:=(oper.typ=top_reg) and (oper.reg=NR_R);
  217. OT_REG8_C_PORT:
  218. result:=(oper.typ=top_ref) and
  219. (((oper.ref^.base=NR_C) and (oper.ref^.index=NR_NO)) or
  220. ((oper.ref^.base=NR_NO) and (oper.ref^.index=NR_C))) and
  221. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  222. (oper.ref^.offset=0);
  223. OT_REG16_IX:
  224. result:=(oper.typ=top_reg) and (oper.reg=NR_IX);
  225. OT_REG16_IY:
  226. result:=(oper.typ=top_reg) and (oper.reg=NR_IY);
  227. OT_REG16_SP:
  228. result:=(oper.typ=top_reg) and (oper.reg=NR_SP);
  229. OT_REG16_BC_DE_HL_SP:
  230. 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));
  231. OT_REG16_BC_DE_HL_AF:
  232. 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));
  233. OT_REG16_BC_DE_IX_SP:
  234. 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));
  235. OT_REG16_BC_DE_IY_SP:
  236. 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));
  237. OT_REG16_DE:
  238. result:=(oper.typ=top_reg) and (oper.reg=NR_DE);
  239. OT_REG16_HL:
  240. result:=(oper.typ=top_reg) and (oper.reg=NR_HL);
  241. OT_REG16_AF:
  242. result:=(oper.typ=top_reg) and (oper.reg=NR_AF);
  243. OT_REG16_AF_:
  244. result:=(oper.typ=top_reg) and (oper.reg=NR_AF_);
  245. OT_RELJMP8:
  246. result:=(oper.typ=top_ref) and
  247. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  248. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO);
  249. OT_REF_ADDR16,
  250. OT_REF_BC,
  251. OT_REF_DE,
  252. OT_REF_HL,
  253. OT_REF_SP,
  254. OT_REF_IX,
  255. OT_REF_IY,
  256. OT_REF_IX_d,
  257. OT_REF_IY_d:
  258. result:=(oper.typ=top_ref) and is_ref_opertype(oper.ref^,ot);
  259. else
  260. internalerror(2020042901);
  261. end;
  262. end;
  263. var
  264. i: Integer;
  265. begin
  266. result:=false;
  267. { Check the opcode }
  268. if p^.opcode<>opcode then
  269. exit;
  270. { The opcode doesn't support conditions, but we have a condition?
  271. That's an invalid instruction, don't match it against anything. }
  272. if (condition<>C_NONE) and not (opcode in cond_instructions) then
  273. exit;
  274. { if our opcode supports a condition, but our operation doesn't have
  275. one, and we're matching it with an instruction entry 'p' that has a
  276. condition, then it doesn't match }
  277. if (opcode in cond_instructions) and (condition=C_None) and
  278. (p^.ops>0) and (p^.optypes[0] in [OT_COND..OT_COND_NZ]) then
  279. exit;
  280. { instruction has a condition? }
  281. if (opcode in cond_instructions) and (condition<>C_None) then
  282. begin
  283. { Check the operand count }
  284. if p^.ops<>(ops+1) then
  285. exit;
  286. { Check the condition }
  287. case p^.optypes[0] of
  288. OT_COND:
  289. { any condition accepted };
  290. OT_COND_C:
  291. if condition<>C_C then
  292. exit;
  293. OT_COND_NC:
  294. if condition<>C_NC then
  295. exit;
  296. OT_COND_Z:
  297. if condition<>C_Z then
  298. exit;
  299. OT_COND_NZ:
  300. if condition<>C_NZ then
  301. exit;
  302. else
  303. { no condition in 'p'? Then it's not a match! }
  304. exit;
  305. end;
  306. { Check the operands }
  307. for i:=1 to p^.ops-1 do
  308. if not OperandsMatch(oper[i-1]^,p^.optypes[i]) then
  309. exit;
  310. end
  311. else
  312. { no condition }
  313. begin
  314. { Check the operand count }
  315. if p^.ops<>ops then
  316. exit;
  317. { Check the operands }
  318. for i:=0 to p^.ops-1 do
  319. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  320. exit;
  321. end;
  322. result:=true;
  323. end;
  324. function taicpu.FindInsentry(objdata: TObjData): boolean;
  325. var
  326. i : longint;
  327. begin
  328. result:=false;
  329. { Things which may only be done once, not when a second pass is done to
  330. optimize }
  331. if (Insentry=nil) {or (IF_PASS2 in InsEntry^.flags)} then
  332. begin
  333. { set the file postion }
  334. current_filepos:=fileinfo;
  335. end
  336. else
  337. begin
  338. { we've already an insentry so it's valid }
  339. result:=true;
  340. exit;
  341. end;
  342. { Lookup opcode in the table }
  343. InsSize:=-1;
  344. i:=instabcache^[opcode];
  345. if i=-1 then
  346. begin
  347. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  348. exit;
  349. end;
  350. insentry:=@instab[i];
  351. while (insentry^.opcode=opcode) do
  352. begin
  353. if matches(insentry) then
  354. begin
  355. result:=true;
  356. exit;
  357. end;
  358. inc(insentry);
  359. end;
  360. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  361. { No instruction found, set insentry to nil and inssize to -1 }
  362. insentry:=nil;
  363. inssize:=-1;
  364. end;
  365. function taicpu.calcsize(p: PInsEntry): shortint;
  366. var
  367. code, token: string;
  368. i: Integer;
  369. ch: Char;
  370. begin
  371. result:=0;
  372. code:=insentry^.code;
  373. i:=1;
  374. token:='';
  375. while i<=length(code) do
  376. begin
  377. ch:=code[i];
  378. Inc(i);
  379. if ch<>',' then
  380. token:=token+ch;
  381. if (ch=',') or (i>length(code)) then
  382. begin
  383. if token='' then
  384. internalerror(2020050402);
  385. if (token[1]='$') or (token[1]='%') then
  386. Inc(result)
  387. else if token='nn' then
  388. Inc(result,2);
  389. token:='';
  390. end;
  391. end;
  392. end;
  393. procedure taicpu.gencode(objdata: TObjData);
  394. procedure WriteByte(b: byte);
  395. begin
  396. objdata.writebytes(b,1);
  397. end;
  398. procedure WriteWord(w: byte);
  399. var
  400. bytes: array [0..1] of Byte;
  401. begin
  402. bytes[0]:=Byte(w);
  403. bytes[1]:=Byte(w shr 8);
  404. objdata.writebytes(bytes,2);
  405. end;
  406. procedure WriteNN;
  407. var
  408. i: Integer;
  409. begin
  410. for i:=0 to insentry^.ops-1 do
  411. begin
  412. //Writeln(insentry^.optypes[i]);
  413. if insentry^.optypes[i]=OT_IMM16 then
  414. begin
  415. //Writeln(oper[i]^.typ);
  416. case oper[i]^.typ of
  417. top_const:
  418. begin
  419. WriteWord(Word(oper[i]^.val));
  420. exit;
  421. end;
  422. top_ref:
  423. begin
  424. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  425. internalerror(2020050406);
  426. if Assigned(oper[i]^.ref^.symbol) then
  427. begin
  428. if oper[i]^.ref^.refaddr<>addr_full then
  429. internalerror(2020050407);
  430. objdata.writeReloc(oper[i]^.ref^.offset,2,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE);
  431. exit;
  432. end
  433. else
  434. begin
  435. WriteWord(oper[i]^.ref^.offset);
  436. exit;
  437. end;
  438. end;
  439. else
  440. InternalError(2020050404);
  441. end;
  442. end;
  443. end;
  444. InternalError(2020050403);
  445. end;
  446. procedure HandlePercent(token: string);
  447. var
  448. bincodestr: string;
  449. i, valcode: integer;
  450. b: Byte;
  451. begin
  452. bincodestr:='';
  453. for i:=1 to length(token) do
  454. case token[i] of
  455. '%':
  456. begin
  457. bincodestr:=bincodestr+'%';
  458. end;
  459. '0','1':
  460. begin
  461. bincodestr:=bincodestr+token[i];
  462. end;
  463. 'p','d','r','q':
  464. begin
  465. bincodestr:=bincodestr+'0';
  466. end;
  467. '''':
  468. begin
  469. end;
  470. else
  471. internalerror(2020050405);
  472. end;
  473. Val(bincodestr,b,valcode);
  474. objdata.writebytes(b,1);
  475. end;
  476. var
  477. i: Integer;
  478. ch: Char;
  479. b: Byte;
  480. valcode: integer;
  481. code: string;
  482. token: string;
  483. begin
  484. { safety check }
  485. if objdata.currobjsec.size<>longword(insoffset) then
  486. internalerror(2020050401);
  487. code:=insentry^.code;
  488. //Writeln('>',code,'<');
  489. i:=1;
  490. token:='';
  491. while i<=length(code) do
  492. begin
  493. ch:=code[i];
  494. Inc(i);
  495. if ch<>',' then
  496. token:=token+ch;
  497. if (ch=',') or (i>length(code)) then
  498. begin
  499. if token='' then
  500. internalerror(2020050402);
  501. if token[1]='$' then
  502. begin
  503. Val(token,b,valcode);
  504. WriteByte(b);
  505. end
  506. else if token[1]='%' then
  507. begin
  508. HandlePercent(token);
  509. end
  510. else if token='nn' then
  511. WriteNN;
  512. token:='';
  513. end;
  514. end;
  515. end;
  516. constructor taicpu.op_none(op : tasmop);
  517. begin
  518. inherited create(op);
  519. end;
  520. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  521. begin
  522. inherited create(op);
  523. ops:=1;
  524. loadreg(0,_op1);
  525. end;
  526. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  527. begin
  528. inherited create(op);
  529. ops:=1;
  530. loadref(0,_op1);
  531. end;
  532. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  533. begin
  534. inherited create(op);
  535. ops:=1;
  536. loadconst(0,_op1);
  537. end;
  538. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  539. begin
  540. inherited create(op);
  541. ops:=2;
  542. loadreg(0,_op1);
  543. loadreg(1,_op2);
  544. end;
  545. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  546. begin
  547. inherited create(op);
  548. ops:=2;
  549. loadreg(0,_op1);
  550. loadconst(1,_op2);
  551. end;
  552. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  553. begin
  554. inherited create(op);
  555. ops:=2;
  556. loadconst(0,_op1);
  557. loadreg(1,_op2);
  558. end;
  559. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  560. begin
  561. inherited create(op);
  562. ops:=2;
  563. loadreg(0,_op1);
  564. loadref(1,_op2);
  565. end;
  566. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  567. begin
  568. inherited create(op);
  569. ops:=2;
  570. loadref(0,_op1);
  571. loadreg(1,_op2);
  572. end;
  573. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  574. begin
  575. inherited create(op);
  576. ops:=2;
  577. loadref(0,_op1);
  578. loadconst(1,_op2);
  579. end;
  580. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  581. begin
  582. inherited create(op);
  583. is_jmp:=op in jmp_instructions;
  584. condition:=cond;
  585. ops:=1;
  586. loadsymbol(0,_op1,0);
  587. end;
  588. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  589. begin
  590. inherited create(op);
  591. is_jmp:=op in jmp_instructions;
  592. ops:=1;
  593. loadsymbol(0,_op1,0);
  594. end;
  595. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  596. begin
  597. inherited create(op);
  598. ops:=1;
  599. loadsymbol(0,_op1,_op1ofs);
  600. end;
  601. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  602. begin
  603. result:=(
  604. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  605. ) and
  606. (ops=2) and
  607. (oper[0]^.typ=top_reg) and
  608. (oper[1]^.typ=top_reg) and
  609. (oper[0]^.reg=oper[1]^.reg);
  610. end;
  611. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  612. begin
  613. result:=operand_read;
  614. case opcode of
  615. A_LD,
  616. A_POP:
  617. if opnr=0 then
  618. result:=operand_write;
  619. A_PUSH,
  620. A_BIT,
  621. A_CP,
  622. A_DJNZ,
  623. A_JR,
  624. A_JP,
  625. A_CALL,
  626. A_RET,
  627. A_RETI,
  628. A_RETN,
  629. A_RST,
  630. A_IM:
  631. ;
  632. A_SET,
  633. A_RES:
  634. if opnr=1 then
  635. result:=operand_readwrite;
  636. A_EX:
  637. result:=operand_readwrite;
  638. else
  639. begin
  640. if opnr=0 then
  641. result:=operand_readwrite;
  642. end;
  643. end;
  644. end;
  645. procedure taicpu.ResetPass1;
  646. begin
  647. { we need to reset everything here, because the choosen insentry
  648. can be invalid for a new situation where the previously optimized
  649. insentry is not correct }
  650. InsEntry:=nil;
  651. InsSize:=0;
  652. LastInsOffset:=-1;
  653. end;
  654. procedure taicpu.ResetPass2;
  655. begin
  656. { we are here in a second pass, check if the instruction can be optimized }
  657. {if assigned(InsEntry) and
  658. (IF_PASS2 in InsEntry^.flags) then
  659. begin
  660. InsEntry:=nil;
  661. InsSize:=0;
  662. end;}
  663. LastInsOffset:=-1;
  664. end;
  665. function taicpu.Pass1(objdata: TObjData): longint;
  666. begin
  667. Pass1:=0;
  668. { Save the old offset and set the new offset }
  669. InsOffset:=ObjData.CurrObjSec.Size;
  670. { Error? }
  671. if (Insentry=nil) and (InsSize=-1) then
  672. exit;
  673. { set the file postion }
  674. current_filepos:=fileinfo;
  675. { Get InsEntry }
  676. if FindInsEntry(ObjData) then
  677. begin
  678. { Calculate instruction size }
  679. InsSize:=calcsize(insentry);
  680. LastInsOffset:=InsOffset;
  681. Pass1:=InsSize;
  682. exit;
  683. end;
  684. LastInsOffset:=-1;
  685. end;
  686. procedure taicpu.Pass2(objdata: TObjData);
  687. begin
  688. { error in pass1 ? }
  689. if insentry=nil then
  690. exit;
  691. current_filepos:=fileinfo;
  692. { Generate the instruction }
  693. GenCode(objdata);
  694. end;
  695. function taicpu.CheckIfValid: boolean;
  696. begin
  697. result:=FindInsEntry(nil);
  698. end;
  699. function taicpu.GetString: string;
  700. var
  701. i : longint;
  702. s : string;
  703. first: Boolean;
  704. begin
  705. s:='['+std_op2str[opcode];
  706. for i:=0 to ops-1 do
  707. begin
  708. with oper[i]^ do
  709. begin
  710. if i=0 then
  711. begin
  712. s:=s+' ';
  713. if condition<>C_None then
  714. s:=s+cond2str[condition]+',';
  715. end
  716. else
  717. s:=s+',';
  718. case typ of
  719. top_reg:
  720. s:=s+std_regname(reg);
  721. top_const:
  722. s:=s+'const';
  723. top_ref:
  724. case ref^.refaddr of
  725. addr_full:
  726. s:=s+'addr16';
  727. addr_lo8:
  728. s:=s+'addr_lo8';
  729. addr_hi8:
  730. s:=s+'addr_hi8';
  731. addr_no:
  732. begin
  733. s:=s+'(';
  734. first:=true;
  735. if ref^.base<>NR_NO then
  736. begin
  737. first:=false;
  738. s:=s+std_regname(ref^.base);
  739. end;
  740. if ref^.index<>NR_NO then
  741. begin
  742. if not first then
  743. s:=s+'+';
  744. first:=false;
  745. s:=s+std_regname(ref^.index);
  746. end;
  747. if assigned(ref^.symbol) then
  748. begin
  749. if not first then
  750. s:=s+'+';
  751. first:=false;
  752. s:=s+'addr16';
  753. end;
  754. if ref^.offset<>0 then
  755. begin
  756. if not first then
  757. s:=s+'+';
  758. if (ref^.offset>=-128) and (ref^.offset<=127) then
  759. s:=s+'const8'
  760. else
  761. s:=s+'const16';
  762. end;
  763. s:=s+')';
  764. end;
  765. else
  766. ;
  767. end;
  768. else
  769. ;
  770. end;
  771. end;
  772. end;
  773. GetString:=s+']';
  774. end;
  775. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  776. begin
  777. case getregtype(r) of
  778. R_INTREGISTER :
  779. result:=taicpu.op_reg_ref(A_LD,r,ref)
  780. else
  781. internalerror(200401041);
  782. end;
  783. end;
  784. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  785. begin
  786. case getregtype(r) of
  787. R_INTREGISTER :
  788. result:=taicpu.op_ref_reg(A_LD,ref,r);
  789. else
  790. internalerror(200401041);
  791. end;
  792. end;
  793. function is_ref_addr16(const ref: treference): Boolean;
  794. begin
  795. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  796. end;
  797. function is_ref_bc(const ref: treference): Boolean;
  798. begin
  799. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  800. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  801. (ref.offset=0) and (ref.scalefactor<=1) and
  802. (ref.symbol=nil) and (ref.relsymbol=nil);
  803. end;
  804. function is_ref_de(const ref: treference): Boolean;
  805. begin
  806. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  807. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  808. (ref.offset=0) and (ref.scalefactor<=1) and
  809. (ref.symbol=nil) and (ref.relsymbol=nil);
  810. end;
  811. function is_ref_hl(const ref: treference): Boolean;
  812. begin
  813. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  814. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  815. (ref.offset=0) and (ref.scalefactor<=1) and
  816. (ref.symbol=nil) and (ref.relsymbol=nil);
  817. end;
  818. function is_ref_sp(const ref: treference): Boolean;
  819. begin
  820. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  821. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  822. (ref.offset=0) and (ref.scalefactor<=1) and
  823. (ref.symbol=nil) and (ref.relsymbol=nil);
  824. end;
  825. function is_ref_ix(const ref: treference): Boolean;
  826. begin
  827. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  828. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  829. (ref.offset=0) and (ref.scalefactor<=1) and
  830. (ref.symbol=nil) and (ref.relsymbol=nil);
  831. end;
  832. function is_ref_iy(const ref: treference): Boolean;
  833. begin
  834. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  835. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  836. (ref.offset=0) and (ref.scalefactor<=1) and
  837. (ref.symbol=nil) and (ref.relsymbol=nil);
  838. end;
  839. function is_ref_ix_d(const ref: treference): Boolean;
  840. begin
  841. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  842. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  843. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  844. (ref.symbol=nil) and (ref.relsymbol=nil);
  845. end;
  846. function is_ref_iy_d(const ref: treference): Boolean;
  847. begin
  848. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  849. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  850. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  851. (ref.symbol=nil) and (ref.relsymbol=nil);
  852. end;
  853. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  854. begin
  855. case opertype of
  856. OT_REF_ADDR16:
  857. result:=is_ref_addr16(ref);
  858. OT_REF_BC:
  859. result:=is_ref_bc(ref);
  860. OT_REF_DE:
  861. result:=is_ref_de(ref);
  862. OT_REF_HL:
  863. result:=is_ref_hl(ref);
  864. OT_REF_SP:
  865. result:=is_ref_sp(ref);
  866. OT_REF_IX:
  867. result:=is_ref_ix(ref);
  868. OT_REF_IY:
  869. result:=is_ref_iy(ref);
  870. OT_REF_IX_d:
  871. result:=is_ref_ix_d(ref);
  872. OT_REF_IY_d:
  873. result:=is_ref_iy_d(ref);
  874. else
  875. internalerror(2020041801);
  876. end;
  877. end;
  878. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  879. var
  880. ot: trefoperandtype;
  881. begin
  882. result:=true;
  883. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  884. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  885. exit;
  886. result:=false;
  887. end;
  888. {****************************************************************************
  889. Instruction table
  890. *****************************************************************************}
  891. procedure BuildInsTabCache;
  892. var
  893. i : longint;
  894. begin
  895. new(instabcache);
  896. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  897. i:=0;
  898. while (i<InsTabEntries) do
  899. begin
  900. if InsTabCache^[InsTab[i].OPcode]=-1 then
  901. InsTabCache^[InsTab[i].OPcode]:=i;
  902. inc(i);
  903. end;
  904. end;
  905. procedure InitAsm;
  906. begin
  907. if not assigned(instabcache) then
  908. BuildInsTabCache;
  909. end;
  910. procedure DoneAsm;
  911. begin
  912. if assigned(instabcache) then
  913. begin
  914. dispose(instabcache);
  915. instabcache:=nil;
  916. end;
  917. end;
  918. begin
  919. cai_cpu:=taicpu;
  920. cai_align:=tai_align;
  921. end.