aasmcpu.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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. function EvalMaskCode(const maskcode: string): byte;
  447. var
  448. i: Integer;
  449. begin
  450. case maskcode of
  451. 'dd':
  452. for i:=0 to insentry^.ops-1 do
  453. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_SP then
  454. begin
  455. if oper[i]^.typ<>top_reg then
  456. internalerror(2020050410);
  457. case oper[i]^.reg of
  458. NR_BC:
  459. result:=0;
  460. NR_DE:
  461. result:=1;
  462. NR_HL:
  463. result:=2;
  464. NR_SP:
  465. result:=3;
  466. else
  467. internalerror(2020050411);
  468. end;
  469. end;
  470. 'qq':
  471. for i:=0 to insentry^.ops-1 do
  472. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_AF then
  473. begin
  474. if oper[i]^.typ<>top_reg then
  475. internalerror(2020050412);
  476. case oper[i]^.reg of
  477. NR_BC:
  478. result:=0;
  479. NR_DE:
  480. result:=1;
  481. NR_HL:
  482. result:=2;
  483. NR_AF:
  484. result:=3;
  485. else
  486. internalerror(2020050413);
  487. end;
  488. end;
  489. 'pp':
  490. for i:=0 to insentry^.ops-1 do
  491. if insentry^.optypes[i]=OT_REG16_BC_DE_IX_SP then
  492. begin
  493. if oper[i]^.typ<>top_reg then
  494. internalerror(2020050414);
  495. case oper[i]^.reg of
  496. NR_BC:
  497. result:=0;
  498. NR_DE:
  499. result:=1;
  500. NR_IX:
  501. result:=2;
  502. NR_SP:
  503. result:=3;
  504. else
  505. internalerror(2020050415);
  506. end;
  507. end;
  508. 'rr':
  509. for i:=0 to insentry^.ops-1 do
  510. if insentry^.optypes[i]=OT_REG16_BC_DE_IY_SP then
  511. begin
  512. if oper[i]^.typ<>top_reg then
  513. internalerror(2020050416);
  514. case oper[i]^.reg of
  515. NR_BC:
  516. result:=0;
  517. NR_DE:
  518. result:=1;
  519. NR_IY:
  520. result:=2;
  521. NR_SP:
  522. result:=3;
  523. else
  524. internalerror(2020050417);
  525. end;
  526. end;
  527. 'rrr':
  528. for i:=0 to insentry^.ops-1 do
  529. if insentry^.optypes[i]=OT_REG8 then
  530. begin
  531. if oper[i]^.typ<>top_reg then
  532. internalerror(2020050418);
  533. case oper[i]^.reg of
  534. NR_A:
  535. result:=7;
  536. NR_B:
  537. result:=0;
  538. NR_C:
  539. result:=1;
  540. NR_D:
  541. result:=2;
  542. NR_E:
  543. result:=3;
  544. NR_H:
  545. result:=4;
  546. NR_L:
  547. result:=5;
  548. else
  549. internalerror(2020050419);
  550. end;
  551. end;
  552. 'rrrRRR':
  553. begin
  554. if ops<>2 then
  555. internalerror(2020050420);
  556. if (insentry^.optypes[0]<>OT_REG8) or (insentry^.optypes[1]<>OT_REG8) then
  557. internalerror(2020050421);
  558. if (oper[0]^.typ<>top_reg) or (oper[1]^.typ<>top_reg) then
  559. internalerror(2020050422);
  560. case oper[0]^.reg of
  561. NR_A:
  562. result:=7 shl 3;
  563. NR_B:
  564. result:=0 shl 3;
  565. NR_C:
  566. result:=1 shl 3;
  567. NR_D:
  568. result:=2 shl 3;
  569. NR_E:
  570. result:=3 shl 3;
  571. NR_H:
  572. result:=4 shl 3;
  573. NR_L:
  574. result:=5 shl 3;
  575. else
  576. internalerror(2020050419);
  577. end;
  578. case oper[1]^.reg of
  579. NR_A:
  580. result:=result or 7;
  581. NR_B:
  582. result:=result or 0;
  583. NR_C:
  584. result:=result or 1;
  585. NR_D:
  586. result:=result or 2;
  587. NR_E:
  588. result:=result or 3;
  589. NR_H:
  590. result:=result or 4;
  591. NR_L:
  592. result:=result or 5;
  593. else
  594. internalerror(2020050419);
  595. end;
  596. end;
  597. else
  598. internalerror(2020050409);
  599. end;
  600. end;
  601. procedure HandlePercent(token: string);
  602. var
  603. bincode: string;
  604. maskcode: string;
  605. i, valcode, shiftcount: integer;
  606. b: Byte;
  607. begin
  608. bincode:='';
  609. maskcode:='';
  610. for i:=1 to length(token) do
  611. case token[i] of
  612. '%':
  613. bincode:=bincode+'%';
  614. '0':
  615. begin
  616. bincode:=bincode+'0';
  617. maskcode:=maskcode+'0';
  618. end;
  619. '1':
  620. begin
  621. bincode:=bincode+'1';
  622. maskcode:=maskcode+'0';
  623. end;
  624. 'p','d','r','q':
  625. begin
  626. bincode:=bincode+'0';
  627. maskcode:=maskcode+token[i];
  628. end;
  629. '''':
  630. begin
  631. if (maskcode='') or (maskcode[length(maskcode)]<>'r') then
  632. internalerror(2020050408);
  633. maskcode[length(maskcode)]:='R';
  634. end;
  635. else
  636. internalerror(2020050405);
  637. end;
  638. Val(bincode,b,valcode);
  639. while maskcode[1]='0' do
  640. delete(maskcode,1,1);
  641. shiftcount:=0;
  642. while maskcode[length(maskcode)]='0' do
  643. begin
  644. delete(maskcode,length(maskcode),1);
  645. Inc(shiftcount);
  646. end;
  647. b:=b or (EvalMaskCode(maskcode) shl shiftcount);
  648. objdata.writebytes(b,1);
  649. end;
  650. var
  651. i: Integer;
  652. ch: Char;
  653. b: Byte;
  654. valcode: integer;
  655. code: string;
  656. token: string;
  657. begin
  658. { safety check }
  659. if objdata.currobjsec.size<>longword(insoffset) then
  660. internalerror(2020050401);
  661. code:=insentry^.code;
  662. //Writeln('>',code,'<');
  663. i:=1;
  664. token:='';
  665. while i<=length(code) do
  666. begin
  667. ch:=code[i];
  668. Inc(i);
  669. if ch<>',' then
  670. token:=token+ch;
  671. if (ch=',') or (i>length(code)) then
  672. begin
  673. if token='' then
  674. internalerror(2020050402);
  675. if token[1]='$' then
  676. begin
  677. Val(token,b,valcode);
  678. WriteByte(b);
  679. end
  680. else if token[1]='%' then
  681. begin
  682. HandlePercent(token);
  683. end
  684. else if token='nn' then
  685. WriteNN;
  686. token:='';
  687. end;
  688. end;
  689. end;
  690. constructor taicpu.op_none(op : tasmop);
  691. begin
  692. inherited create(op);
  693. end;
  694. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  695. begin
  696. inherited create(op);
  697. ops:=1;
  698. loadreg(0,_op1);
  699. end;
  700. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  701. begin
  702. inherited create(op);
  703. ops:=1;
  704. loadref(0,_op1);
  705. end;
  706. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  707. begin
  708. inherited create(op);
  709. ops:=1;
  710. loadconst(0,_op1);
  711. end;
  712. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  713. begin
  714. inherited create(op);
  715. ops:=2;
  716. loadreg(0,_op1);
  717. loadreg(1,_op2);
  718. end;
  719. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  720. begin
  721. inherited create(op);
  722. ops:=2;
  723. loadreg(0,_op1);
  724. loadconst(1,_op2);
  725. end;
  726. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  727. begin
  728. inherited create(op);
  729. ops:=2;
  730. loadconst(0,_op1);
  731. loadreg(1,_op2);
  732. end;
  733. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  734. begin
  735. inherited create(op);
  736. ops:=2;
  737. loadreg(0,_op1);
  738. loadref(1,_op2);
  739. end;
  740. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  741. begin
  742. inherited create(op);
  743. ops:=2;
  744. loadref(0,_op1);
  745. loadreg(1,_op2);
  746. end;
  747. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  748. begin
  749. inherited create(op);
  750. ops:=2;
  751. loadref(0,_op1);
  752. loadconst(1,_op2);
  753. end;
  754. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  755. begin
  756. inherited create(op);
  757. is_jmp:=op in jmp_instructions;
  758. condition:=cond;
  759. ops:=1;
  760. loadsymbol(0,_op1,0);
  761. end;
  762. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  763. begin
  764. inherited create(op);
  765. is_jmp:=op in jmp_instructions;
  766. ops:=1;
  767. loadsymbol(0,_op1,0);
  768. end;
  769. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  770. begin
  771. inherited create(op);
  772. ops:=1;
  773. loadsymbol(0,_op1,_op1ofs);
  774. end;
  775. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  776. begin
  777. result:=(
  778. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  779. ) and
  780. (ops=2) and
  781. (oper[0]^.typ=top_reg) and
  782. (oper[1]^.typ=top_reg) and
  783. (oper[0]^.reg=oper[1]^.reg);
  784. end;
  785. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  786. begin
  787. result:=operand_read;
  788. case opcode of
  789. A_LD,
  790. A_POP:
  791. if opnr=0 then
  792. result:=operand_write;
  793. A_PUSH,
  794. A_BIT,
  795. A_CP,
  796. A_DJNZ,
  797. A_JR,
  798. A_JP,
  799. A_CALL,
  800. A_RET,
  801. A_RETI,
  802. A_RETN,
  803. A_RST,
  804. A_IM:
  805. ;
  806. A_SET,
  807. A_RES:
  808. if opnr=1 then
  809. result:=operand_readwrite;
  810. A_EX:
  811. result:=operand_readwrite;
  812. else
  813. begin
  814. if opnr=0 then
  815. result:=operand_readwrite;
  816. end;
  817. end;
  818. end;
  819. procedure taicpu.ResetPass1;
  820. begin
  821. { we need to reset everything here, because the choosen insentry
  822. can be invalid for a new situation where the previously optimized
  823. insentry is not correct }
  824. InsEntry:=nil;
  825. InsSize:=0;
  826. LastInsOffset:=-1;
  827. end;
  828. procedure taicpu.ResetPass2;
  829. begin
  830. { we are here in a second pass, check if the instruction can be optimized }
  831. {if assigned(InsEntry) and
  832. (IF_PASS2 in InsEntry^.flags) then
  833. begin
  834. InsEntry:=nil;
  835. InsSize:=0;
  836. end;}
  837. LastInsOffset:=-1;
  838. end;
  839. function taicpu.Pass1(objdata: TObjData): longint;
  840. begin
  841. Pass1:=0;
  842. { Save the old offset and set the new offset }
  843. InsOffset:=ObjData.CurrObjSec.Size;
  844. { Error? }
  845. if (Insentry=nil) and (InsSize=-1) then
  846. exit;
  847. { set the file postion }
  848. current_filepos:=fileinfo;
  849. { Get InsEntry }
  850. if FindInsEntry(ObjData) then
  851. begin
  852. { Calculate instruction size }
  853. InsSize:=calcsize(insentry);
  854. LastInsOffset:=InsOffset;
  855. Pass1:=InsSize;
  856. exit;
  857. end;
  858. LastInsOffset:=-1;
  859. end;
  860. procedure taicpu.Pass2(objdata: TObjData);
  861. begin
  862. { error in pass1 ? }
  863. if insentry=nil then
  864. exit;
  865. current_filepos:=fileinfo;
  866. { Generate the instruction }
  867. GenCode(objdata);
  868. end;
  869. function taicpu.CheckIfValid: boolean;
  870. begin
  871. result:=FindInsEntry(nil);
  872. end;
  873. function taicpu.GetString: string;
  874. var
  875. i : longint;
  876. s : string;
  877. first: Boolean;
  878. begin
  879. s:='['+std_op2str[opcode];
  880. for i:=0 to ops-1 do
  881. begin
  882. with oper[i]^ do
  883. begin
  884. if i=0 then
  885. begin
  886. s:=s+' ';
  887. if condition<>C_None then
  888. s:=s+cond2str[condition]+',';
  889. end
  890. else
  891. s:=s+',';
  892. case typ of
  893. top_reg:
  894. s:=s+std_regname(reg);
  895. top_const:
  896. s:=s+'const';
  897. top_ref:
  898. case ref^.refaddr of
  899. addr_full:
  900. s:=s+'addr16';
  901. addr_lo8:
  902. s:=s+'addr_lo8';
  903. addr_hi8:
  904. s:=s+'addr_hi8';
  905. addr_no:
  906. begin
  907. s:=s+'(';
  908. first:=true;
  909. if ref^.base<>NR_NO then
  910. begin
  911. first:=false;
  912. s:=s+std_regname(ref^.base);
  913. end;
  914. if ref^.index<>NR_NO then
  915. begin
  916. if not first then
  917. s:=s+'+';
  918. first:=false;
  919. s:=s+std_regname(ref^.index);
  920. end;
  921. if assigned(ref^.symbol) then
  922. begin
  923. if not first then
  924. s:=s+'+';
  925. first:=false;
  926. s:=s+'addr16';
  927. end;
  928. if ref^.offset<>0 then
  929. begin
  930. if not first then
  931. s:=s+'+';
  932. if (ref^.offset>=-128) and (ref^.offset<=127) then
  933. s:=s+'const8'
  934. else
  935. s:=s+'const16';
  936. end;
  937. s:=s+')';
  938. end;
  939. else
  940. ;
  941. end;
  942. else
  943. ;
  944. end;
  945. end;
  946. end;
  947. GetString:=s+']';
  948. end;
  949. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  950. begin
  951. case getregtype(r) of
  952. R_INTREGISTER :
  953. result:=taicpu.op_reg_ref(A_LD,r,ref)
  954. else
  955. internalerror(200401041);
  956. end;
  957. end;
  958. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  959. begin
  960. case getregtype(r) of
  961. R_INTREGISTER :
  962. result:=taicpu.op_ref_reg(A_LD,ref,r);
  963. else
  964. internalerror(200401041);
  965. end;
  966. end;
  967. function is_ref_addr16(const ref: treference): Boolean;
  968. begin
  969. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  970. end;
  971. function is_ref_bc(const ref: treference): Boolean;
  972. begin
  973. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  974. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  975. (ref.offset=0) and (ref.scalefactor<=1) and
  976. (ref.symbol=nil) and (ref.relsymbol=nil);
  977. end;
  978. function is_ref_de(const ref: treference): Boolean;
  979. begin
  980. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  981. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  982. (ref.offset=0) and (ref.scalefactor<=1) and
  983. (ref.symbol=nil) and (ref.relsymbol=nil);
  984. end;
  985. function is_ref_hl(const ref: treference): Boolean;
  986. begin
  987. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  988. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  989. (ref.offset=0) and (ref.scalefactor<=1) and
  990. (ref.symbol=nil) and (ref.relsymbol=nil);
  991. end;
  992. function is_ref_sp(const ref: treference): Boolean;
  993. begin
  994. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  995. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  996. (ref.offset=0) and (ref.scalefactor<=1) and
  997. (ref.symbol=nil) and (ref.relsymbol=nil);
  998. end;
  999. function is_ref_ix(const ref: treference): Boolean;
  1000. begin
  1001. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1002. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1003. (ref.offset=0) and (ref.scalefactor<=1) and
  1004. (ref.symbol=nil) and (ref.relsymbol=nil);
  1005. end;
  1006. function is_ref_iy(const ref: treference): Boolean;
  1007. begin
  1008. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1009. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1010. (ref.offset=0) and (ref.scalefactor<=1) and
  1011. (ref.symbol=nil) and (ref.relsymbol=nil);
  1012. end;
  1013. function is_ref_ix_d(const ref: treference): Boolean;
  1014. begin
  1015. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1016. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1017. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1018. (ref.symbol=nil) and (ref.relsymbol=nil);
  1019. end;
  1020. function is_ref_iy_d(const ref: treference): Boolean;
  1021. begin
  1022. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1023. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1024. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1025. (ref.symbol=nil) and (ref.relsymbol=nil);
  1026. end;
  1027. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  1028. begin
  1029. case opertype of
  1030. OT_REF_ADDR16:
  1031. result:=is_ref_addr16(ref);
  1032. OT_REF_BC:
  1033. result:=is_ref_bc(ref);
  1034. OT_REF_DE:
  1035. result:=is_ref_de(ref);
  1036. OT_REF_HL:
  1037. result:=is_ref_hl(ref);
  1038. OT_REF_SP:
  1039. result:=is_ref_sp(ref);
  1040. OT_REF_IX:
  1041. result:=is_ref_ix(ref);
  1042. OT_REF_IY:
  1043. result:=is_ref_iy(ref);
  1044. OT_REF_IX_d:
  1045. result:=is_ref_ix_d(ref);
  1046. OT_REF_IY_d:
  1047. result:=is_ref_iy_d(ref);
  1048. else
  1049. internalerror(2020041801);
  1050. end;
  1051. end;
  1052. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  1053. var
  1054. ot: trefoperandtype;
  1055. begin
  1056. result:=true;
  1057. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  1058. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  1059. exit;
  1060. result:=false;
  1061. end;
  1062. {****************************************************************************
  1063. Instruction table
  1064. *****************************************************************************}
  1065. procedure BuildInsTabCache;
  1066. var
  1067. i : longint;
  1068. begin
  1069. new(instabcache);
  1070. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1071. i:=0;
  1072. while (i<InsTabEntries) do
  1073. begin
  1074. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1075. InsTabCache^[InsTab[i].OPcode]:=i;
  1076. inc(i);
  1077. end;
  1078. end;
  1079. procedure InitAsm;
  1080. begin
  1081. if not assigned(instabcache) then
  1082. BuildInsTabCache;
  1083. end;
  1084. procedure DoneAsm;
  1085. begin
  1086. if assigned(instabcache) then
  1087. begin
  1088. dispose(instabcache);
  1089. instabcache:=nil;
  1090. end;
  1091. end;
  1092. begin
  1093. cai_cpu:=taicpu;
  1094. cai_align:=tai_align;
  1095. end.