aasmcpu.pas 45 KB

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