aasmcpu.pas 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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;objdata:TObjData):boolean;
  100. function FindInsentry(objdata:TObjData):boolean;
  101. function calcsize(p:PInsEntry):shortint;
  102. procedure gencode(objdata:TObjData);
  103. procedure init; { this need to be called by all constructors }
  104. public
  105. constructor op_none(op : tasmop);
  106. constructor op_reg(op : tasmop;_op1 : tregister);
  107. constructor op_const(op : tasmop;_op1 : LongInt);
  108. constructor op_ref(op : tasmop;const _op1 : treference);
  109. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  110. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  111. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  112. constructor op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  113. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  114. constructor op_ref_const(op:tasmop; _op1: treference; _op2: LongInt);
  115. { this is for Jmp instructions }
  116. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  117. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  118. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  119. procedure loadbool(opidx:longint;_b:boolean);
  120. { register allocation }
  121. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  122. { register spilling code }
  123. function spilling_get_operation_type(opnr: longint): topertype;override;
  124. procedure ResetPass1;override;
  125. procedure ResetPass2;override;
  126. function Pass1(objdata:TObjData):longint;override;
  127. procedure Pass2(objdata:TObjData);override;
  128. function CheckIfValid:boolean;
  129. function GetString:string;
  130. end;
  131. tai_align = class(tai_align_abstract)
  132. { nothing to add }
  133. end;
  134. procedure InitAsm;
  135. procedure DoneAsm;
  136. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  137. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  138. function is_ref_addr16(const ref:treference): Boolean;
  139. function is_ref_bc(const ref:treference): Boolean;
  140. function is_ref_de(const ref:treference): Boolean;
  141. function is_ref_hl(const ref:treference): Boolean;
  142. function is_ref_sp(const ref:treference): Boolean;
  143. function is_ref_ix(const ref:treference): Boolean;
  144. function is_ref_iy(const ref:treference): Boolean;
  145. function is_ref_ix_d(const ref:treference): Boolean;
  146. function is_ref_iy_d(const ref:treference): Boolean;
  147. function is_ref_opertype(const ref:treference;opertype:toperandtype): Boolean;
  148. function is_ref_in_opertypes(const ref:treference;const refopertypes:trefoperandtypes): Boolean;
  149. implementation
  150. {****************************************************************************
  151. Instruction table
  152. *****************************************************************************}
  153. type
  154. TInsTabCache=array[TasmOp] of longint;
  155. PInsTabCache=^TInsTabCache;
  156. const
  157. InsTab:array[0..instabentries-1] of TInsEntry={$i z80tab.inc}
  158. var
  159. InsTabCache : PInsTabCache;
  160. {*****************************************************************************
  161. taicpu Constructors
  162. *****************************************************************************}
  163. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  164. begin
  165. if opidx>=ops then
  166. ops:=opidx+1;
  167. with oper[opidx]^ do
  168. begin
  169. if typ=top_ref then
  170. dispose(ref);
  171. b:=_b;
  172. typ:=top_bool;
  173. end;
  174. end;
  175. function taicpu.Matches(p: PInsEntry; objdata:TObjData): boolean;
  176. function OperandsMatch(const oper: toper; const ot: toperandtype): boolean;
  177. var
  178. currsym: TObjSymbol;
  179. l: ASizeInt;
  180. relsize: LongInt;
  181. begin
  182. case ot of
  183. OT_IMM3:
  184. result:=(oper.typ=top_const) and (oper.val>=0) and (oper.val<=7);
  185. OT_IMM8:
  186. result:=((oper.typ=top_const) and (oper.val>=-128) and (oper.val<=255)) or
  187. ((oper.typ=top_ref) and
  188. (oper.ref^.refaddr in [addr_hi8,addr_lo8]) and assigned(oper.ref^.symbol) and
  189. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  190. OT_IMM16:
  191. result:=((oper.typ=top_const) and (oper.val>=-32768) and (oper.val<=65535)) or
  192. ((oper.typ=top_ref) and
  193. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  194. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO));
  195. OT_IMM_VAL0:
  196. result:=(oper.typ=top_const) and (oper.val=0);
  197. OT_IMM_VAL1:
  198. result:=(oper.typ=top_const) and (oper.val=1);
  199. OT_IMM_VAL2:
  200. result:=(oper.typ=top_const) and (oper.val=2);
  201. OT_IMM_RST:
  202. result:=(oper.typ=top_const) and ((oper.val=$00) or (oper.val=$08) or
  203. (oper.val=$10) or (oper.val=$18) or
  204. (oper.val=$20) or (oper.val=$28) or
  205. (oper.val=$30) or (oper.val=$38));
  206. OT_IMM_PORT:
  207. result:=(oper.typ=top_ref) and
  208. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  209. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO) and
  210. (oper.ref^.offset>=0) and (oper.ref^.offset<=255);
  211. OT_REG8:
  212. result:=(oper.typ=top_reg) and ((oper.reg=NR_A) or (oper.reg=NR_B) or
  213. (oper.reg=NR_C) or (oper.reg=NR_D) or
  214. (oper.reg=NR_E) or (oper.reg=NR_H) or
  215. (oper.reg=NR_L));
  216. OT_REG8_A:
  217. result:=(oper.typ=top_reg) and (oper.reg=NR_A);
  218. OT_REG8_I:
  219. result:=(oper.typ=top_reg) and (oper.reg=NR_I);
  220. OT_REG8_R:
  221. result:=(oper.typ=top_reg) and (oper.reg=NR_R);
  222. OT_REG8_C_PORT:
  223. result:=(oper.typ=top_ref) and
  224. (((oper.ref^.base=NR_C) and (oper.ref^.index=NR_NO)) or
  225. ((oper.ref^.base=NR_NO) and (oper.ref^.index=NR_C))) and
  226. (oper.ref^.symbol=nil) and (oper.ref^.relsymbol=nil) and
  227. (oper.ref^.offset=0);
  228. OT_REG16_IX:
  229. result:=(oper.typ=top_reg) and (oper.reg=NR_IX);
  230. OT_REG16_IY:
  231. result:=(oper.typ=top_reg) and (oper.reg=NR_IY);
  232. OT_REG16_SP:
  233. result:=(oper.typ=top_reg) and (oper.reg=NR_SP);
  234. OT_REG16_BC_DE_HL_SP:
  235. 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));
  236. OT_REG16_BC_DE_HL_AF:
  237. 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));
  238. OT_REG16_BC_DE_IX_SP:
  239. 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));
  240. OT_REG16_BC_DE_IY_SP:
  241. 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));
  242. OT_REG16_DE:
  243. result:=(oper.typ=top_reg) and (oper.reg=NR_DE);
  244. OT_REG16_HL:
  245. result:=(oper.typ=top_reg) and (oper.reg=NR_HL);
  246. OT_REG16_AF:
  247. result:=(oper.typ=top_reg) and (oper.reg=NR_AF);
  248. OT_REG16_AF_:
  249. result:=(oper.typ=top_reg) and (oper.reg=NR_AF_);
  250. OT_RELJMP8:
  251. begin
  252. result:=(oper.typ=top_ref) and
  253. (oper.ref^.refaddr=addr_full) and assigned(oper.ref^.symbol) and
  254. (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO);
  255. if result and assigned(objdata) then
  256. begin
  257. currsym:=objdata.symbolref(oper.ref^.symbol);
  258. l:=oper.ref^.offset;
  259. {$push}
  260. {$r-,q-} { disable also overflow as address returns a qword for x86_64 }
  261. if assigned(currsym) then
  262. inc(l,currsym.address);
  263. {$pop}
  264. { when it is a forward jump we need to compensate the
  265. offset of the instruction since the previous time,
  266. because the symbol address is then still using the
  267. 'old-style' addressing.
  268. For backwards jumps this is not required because the
  269. address of the symbol is already adjusted to the
  270. new offset }
  271. if (l>InsOffset) and (LastInsOffset<>-1) then
  272. inc(l,InsOffset-LastInsOffset-1);
  273. { instruction size will then always become 2 (PFV) }
  274. relsize:=l-(InsOffset+2);
  275. result:=(relsize>=-128) and (relsize<=127) and
  276. (
  277. not assigned(currsym) or
  278. (currsym.objsection=objdata.currobjsec)
  279. );
  280. end;
  281. end;
  282. OT_REF_ADDR16,
  283. OT_REF_BC,
  284. OT_REF_DE,
  285. OT_REF_HL,
  286. OT_REF_SP,
  287. OT_REF_IX,
  288. OT_REF_IY,
  289. OT_REF_IX_d,
  290. OT_REF_IY_d:
  291. result:=(oper.typ=top_ref) and is_ref_opertype(oper.ref^,ot);
  292. else
  293. internalerror(2020042901);
  294. end;
  295. end;
  296. var
  297. i: Integer;
  298. begin
  299. result:=false;
  300. { Check the opcode }
  301. if p^.opcode<>opcode then
  302. exit;
  303. { The opcode doesn't support conditions, but we have a condition?
  304. That's an invalid instruction, don't match it against anything. }
  305. if (condition<>C_NONE) and not (opcode in cond_instructions) then
  306. exit;
  307. { if our opcode supports a condition, but our operation doesn't have
  308. one, and we're matching it with an instruction entry 'p' that has a
  309. condition, then it doesn't match }
  310. if (opcode in cond_instructions) and (condition=C_None) and
  311. (p^.ops>0) and (p^.optypes[0] in [OT_COND..OT_COND_NZ]) then
  312. exit;
  313. { instruction has a condition? }
  314. if (opcode in cond_instructions) and (condition<>C_None) then
  315. begin
  316. { Check the operand count }
  317. if p^.ops<>(ops+1) then
  318. exit;
  319. { Check the condition }
  320. case p^.optypes[0] of
  321. OT_COND:
  322. { any condition accepted };
  323. OT_COND_C:
  324. if condition<>C_C then
  325. exit;
  326. OT_COND_NC:
  327. if condition<>C_NC then
  328. exit;
  329. OT_COND_Z:
  330. if condition<>C_Z then
  331. exit;
  332. OT_COND_NZ:
  333. if condition<>C_NZ then
  334. exit;
  335. else
  336. { no condition in 'p'? Then it's not a match! }
  337. exit;
  338. end;
  339. { Check the operands }
  340. for i:=1 to p^.ops-1 do
  341. if not OperandsMatch(oper[i-1]^,p^.optypes[i]) then
  342. exit;
  343. end
  344. else
  345. { no condition }
  346. begin
  347. { Check the operand count }
  348. if p^.ops<>ops then
  349. exit;
  350. { Check the operands }
  351. for i:=0 to p^.ops-1 do
  352. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  353. exit;
  354. end;
  355. result:=true;
  356. end;
  357. function taicpu.FindInsentry(objdata: TObjData): boolean;
  358. var
  359. i : longint;
  360. begin
  361. result:=false;
  362. { Things which may only be done once, not when a second pass is done to
  363. optimize }
  364. if (Insentry=nil) or (opcode=A_JRJP) then
  365. begin
  366. { set the file postion }
  367. current_filepos:=fileinfo;
  368. end
  369. else
  370. begin
  371. { we've already an insentry so it's valid }
  372. result:=true;
  373. exit;
  374. end;
  375. { Lookup opcode in the table }
  376. InsSize:=-1;
  377. i:=instabcache^[opcode];
  378. if i=-1 then
  379. begin
  380. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  381. exit;
  382. end;
  383. insentry:=@instab[i];
  384. while (insentry^.opcode=opcode) do
  385. begin
  386. if matches(insentry,objdata) then
  387. begin
  388. result:=true;
  389. exit;
  390. end;
  391. inc(insentry);
  392. end;
  393. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  394. { No instruction found, set insentry to nil and inssize to -1 }
  395. insentry:=nil;
  396. inssize:=-1;
  397. end;
  398. function taicpu.calcsize(p: PInsEntry): shortint;
  399. var
  400. code, token: string;
  401. i: Integer;
  402. ch: Char;
  403. begin
  404. result:=0;
  405. code:=insentry^.code;
  406. i:=1;
  407. token:='';
  408. while i<=length(code) do
  409. begin
  410. ch:=code[i];
  411. Inc(i);
  412. if ch<>',' then
  413. token:=token+ch;
  414. if (ch=',') or (i>length(code)) then
  415. begin
  416. if token='' then
  417. internalerror(2020050402);
  418. if (token[1]='$') or (token[1]='%') or (token='n') or (token='d') or (token='e') then
  419. Inc(result)
  420. else if token='nn' then
  421. Inc(result,2)
  422. else
  423. internalerror(2020050504);
  424. token:='';
  425. end;
  426. end;
  427. end;
  428. procedure taicpu.gencode(objdata: TObjData);
  429. procedure WriteByte(b: byte);
  430. begin
  431. objdata.writebytes(b,1);
  432. end;
  433. procedure WriteWord(w: word);
  434. var
  435. bytes: array [0..1] of Byte;
  436. begin
  437. bytes[0]:=Byte(w);
  438. bytes[1]:=Byte(w shr 8);
  439. objdata.writebytes(bytes,2);
  440. end;
  441. procedure WriteNN;
  442. var
  443. i, j: Integer;
  444. begin
  445. for j:=0 to insentry^.ops-1 do
  446. begin
  447. if condition=C_NONE then
  448. i:=j
  449. else
  450. i:=j-1;
  451. if insentry^.optypes[j]=OT_IMM16 then
  452. begin
  453. case oper[i]^.typ of
  454. top_const:
  455. begin
  456. WriteWord(Word(oper[i]^.val));
  457. exit;
  458. end;
  459. top_ref:
  460. begin
  461. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  462. internalerror(2020050406);
  463. if Assigned(oper[i]^.ref^.symbol) then
  464. begin
  465. if oper[i]^.ref^.refaddr<>addr_full then
  466. internalerror(2020050407);
  467. objdata.writeReloc(oper[i]^.ref^.offset,2,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE);
  468. exit;
  469. end
  470. else
  471. begin
  472. WriteWord(oper[i]^.ref^.offset);
  473. exit;
  474. end;
  475. end;
  476. else
  477. InternalError(2020050404);
  478. end;
  479. end
  480. else if insentry^.optypes[j]=OT_REF_ADDR16 then
  481. begin
  482. case oper[i]^.typ of
  483. top_ref:
  484. begin
  485. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  486. internalerror(2020050603);
  487. if Assigned(oper[i]^.ref^.symbol) then
  488. begin
  489. if oper[i]^.ref^.refaddr<>addr_no then
  490. internalerror(2020050604);
  491. objdata.writeReloc(oper[i]^.ref^.offset,2,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE);
  492. exit;
  493. end
  494. else
  495. begin
  496. WriteWord(oper[i]^.ref^.offset);
  497. exit;
  498. end;
  499. end;
  500. else
  501. InternalError(2020050602);
  502. end;
  503. end;
  504. end;
  505. InternalError(2020050403);
  506. end;
  507. procedure WriteN;
  508. var
  509. i: Integer;
  510. begin
  511. for i:=0 to insentry^.ops-1 do
  512. begin
  513. if insentry^.optypes[i]=OT_IMM8 then
  514. begin
  515. case oper[i]^.typ of
  516. top_const:
  517. begin
  518. WriteByte(Byte(oper[i]^.val));
  519. exit;
  520. end;
  521. top_ref:
  522. begin
  523. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  524. internalerror(2020050507);
  525. if Assigned(oper[i]^.ref^.symbol) then
  526. begin
  527. case oper[i]^.ref^.refaddr of
  528. addr_hi8:
  529. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE_HI8);
  530. addr_lo8:
  531. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_ABSOLUTE_LO8);
  532. else
  533. internalerror(2020050408);
  534. end;
  535. exit;
  536. end
  537. else
  538. internalerror(2020050409);
  539. end;
  540. else
  541. InternalError(2020050506);
  542. end;
  543. end
  544. else if insentry^.optypes[i]=OT_IMM_PORT then
  545. begin
  546. case oper[i]^.typ of
  547. top_ref:
  548. begin
  549. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) or Assigned(oper[i]^.ref^.symbol) then
  550. internalerror(2020050612);
  551. WriteByte(Byte(oper[i]^.ref^.offset));
  552. exit;
  553. end;
  554. else
  555. InternalError(2020050611);
  556. end;
  557. end;
  558. end;
  559. InternalError(2020050505);
  560. end;
  561. procedure WriteD;
  562. var
  563. i: Integer;
  564. begin
  565. for i:=0 to insentry^.ops-1 do
  566. begin
  567. if insentry^.optypes[i] in [OT_REF_IX_d,OT_REF_IY_d] then
  568. begin
  569. case oper[i]^.typ of
  570. top_ref:
  571. begin
  572. if not is_ref_opertype(oper[i]^.ref^,insentry^.optypes[i]) then
  573. internalerror(2020050510);
  574. WriteByte(Byte(oper[i]^.ref^.offset));
  575. exit;
  576. end;
  577. else
  578. InternalError(2020050511);
  579. end;
  580. end;
  581. end;
  582. InternalError(2020050512);
  583. end;
  584. procedure WriteE;
  585. var
  586. i, j: Integer;
  587. begin
  588. for j:=0 to insentry^.ops-1 do
  589. begin
  590. if condition=C_NONE then
  591. i:=j
  592. else
  593. i:=j-1;
  594. if insentry^.optypes[j]=OT_RELJMP8 then
  595. begin
  596. case oper[i]^.typ of
  597. top_ref:
  598. begin
  599. if (oper[i]^.ref^.base<>NR_NO) or (oper[i]^.ref^.index<>NR_NO) then
  600. internalerror(2020050608);
  601. if Assigned(oper[i]^.ref^.symbol) then
  602. begin
  603. if oper[i]^.ref^.refaddr<>addr_full then
  604. internalerror(2020050609);
  605. objdata.writeReloc(oper[i]^.ref^.offset,1,ObjData.symbolref(oper[i]^.ref^.symbol),RELOC_RELATIVE);
  606. exit;
  607. end
  608. else
  609. internalerror(2020050610);
  610. exit;
  611. end;
  612. else
  613. InternalError(2020050607);
  614. end;
  615. end;
  616. end;
  617. end;
  618. function EvalMaskCode(const maskcode: string): byte;
  619. var
  620. i: Integer;
  621. begin
  622. result:=0;
  623. case maskcode of
  624. 'dd':
  625. for i:=0 to insentry^.ops-1 do
  626. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_SP then
  627. begin
  628. if oper[i]^.typ<>top_reg then
  629. internalerror(2020050410);
  630. case oper[i]^.reg of
  631. NR_BC:
  632. result:=0;
  633. NR_DE:
  634. result:=1;
  635. NR_HL:
  636. result:=2;
  637. NR_SP:
  638. result:=3;
  639. else
  640. internalerror(2020050411);
  641. end;
  642. end;
  643. 'qq':
  644. for i:=0 to insentry^.ops-1 do
  645. if insentry^.optypes[i]=OT_REG16_BC_DE_HL_AF then
  646. begin
  647. if oper[i]^.typ<>top_reg then
  648. internalerror(2020050412);
  649. case oper[i]^.reg of
  650. NR_BC:
  651. result:=0;
  652. NR_DE:
  653. result:=1;
  654. NR_HL:
  655. result:=2;
  656. NR_AF:
  657. result:=3;
  658. else
  659. internalerror(2020050413);
  660. end;
  661. end;
  662. 'pp':
  663. for i:=0 to insentry^.ops-1 do
  664. if insentry^.optypes[i]=OT_REG16_BC_DE_IX_SP then
  665. begin
  666. if oper[i]^.typ<>top_reg then
  667. internalerror(2020050414);
  668. case oper[i]^.reg of
  669. NR_BC:
  670. result:=0;
  671. NR_DE:
  672. result:=1;
  673. NR_IX:
  674. result:=2;
  675. NR_SP:
  676. result:=3;
  677. else
  678. internalerror(2020050415);
  679. end;
  680. end;
  681. 'rr':
  682. for i:=0 to insentry^.ops-1 do
  683. if insentry^.optypes[i]=OT_REG16_BC_DE_IY_SP then
  684. begin
  685. if oper[i]^.typ<>top_reg then
  686. internalerror(2020050416);
  687. case oper[i]^.reg of
  688. NR_BC:
  689. result:=0;
  690. NR_DE:
  691. result:=1;
  692. NR_IY:
  693. result:=2;
  694. NR_SP:
  695. result:=3;
  696. else
  697. internalerror(2020050417);
  698. end;
  699. end;
  700. 'rrr':
  701. for i:=0 to insentry^.ops-1 do
  702. if insentry^.optypes[i]=OT_REG8 then
  703. begin
  704. if oper[i]^.typ<>top_reg then
  705. internalerror(2020050418);
  706. case oper[i]^.reg of
  707. NR_A:
  708. result:=7;
  709. NR_B:
  710. result:=0;
  711. NR_C:
  712. result:=1;
  713. NR_D:
  714. result:=2;
  715. NR_E:
  716. result:=3;
  717. NR_H:
  718. result:=4;
  719. NR_L:
  720. result:=5;
  721. else
  722. internalerror(2020050419);
  723. end;
  724. end;
  725. 'rrrRRR':
  726. begin
  727. if ops<>2 then
  728. internalerror(2020050420);
  729. if (insentry^.optypes[0]<>OT_REG8) or (insentry^.optypes[1]<>OT_REG8) then
  730. internalerror(2020050421);
  731. if (oper[0]^.typ<>top_reg) or (oper[1]^.typ<>top_reg) then
  732. internalerror(2020050422);
  733. case oper[0]^.reg of
  734. NR_A:
  735. result:=7 shl 3;
  736. NR_B:
  737. result:=0 shl 3;
  738. NR_C:
  739. result:=1 shl 3;
  740. NR_D:
  741. result:=2 shl 3;
  742. NR_E:
  743. result:=3 shl 3;
  744. NR_H:
  745. result:=4 shl 3;
  746. NR_L:
  747. result:=5 shl 3;
  748. else
  749. internalerror(2020050427);
  750. end;
  751. case oper[1]^.reg of
  752. NR_A:
  753. result:=result or 7;
  754. NR_B:
  755. result:=result or 0;
  756. NR_C:
  757. result:=result or 1;
  758. NR_D:
  759. result:=result or 2;
  760. NR_E:
  761. result:=result or 3;
  762. NR_H:
  763. result:=result or 4;
  764. NR_L:
  765. result:=result or 5;
  766. else
  767. internalerror(2020050428);
  768. end;
  769. end;
  770. 'ccc':
  771. begin
  772. case condition of
  773. C_NZ:
  774. result:=0;
  775. C_Z:
  776. result:=1;
  777. C_NC:
  778. result:=2;
  779. C_C:
  780. result:=3;
  781. C_PO:
  782. result:=4;
  783. C_PE:
  784. result:=5;
  785. C_P:
  786. result:=6;
  787. C_M:
  788. result:=7;
  789. else
  790. internalerror(2020050605);
  791. end;
  792. end;
  793. 'ppp':
  794. begin
  795. for i:=0 to insentry^.ops-1 do
  796. if insentry^.optypes[i]=OT_IMM_RST then
  797. begin
  798. if oper[i]^.typ<>top_const then
  799. internalerror(2020050606);
  800. result:=Byte(oper[i]^.val shr 3) and $07;
  801. end;
  802. end;
  803. 'bbb':
  804. begin
  805. for i:=0 to insentry^.ops-1 do
  806. if insentry^.optypes[i]=OT_IMM3 then
  807. begin
  808. if oper[i]^.typ<>top_const then
  809. internalerror(2020050614);
  810. result:=Byte(oper[i]^.val) and $07;
  811. end;
  812. end;
  813. 'bbbrrr':
  814. result:=(EvalMaskCode('bbb') shl 3) or EvalMaskCode('rrr');
  815. else
  816. internalerror(2020050426);
  817. end;
  818. end;
  819. procedure HandlePercent(token: string);
  820. var
  821. bincode: string;
  822. maskcode: string;
  823. i, valcode, shiftcount: integer;
  824. b: Byte;
  825. begin
  826. bincode:='';
  827. maskcode:='';
  828. for i:=1 to length(token) do
  829. case token[i] of
  830. '%':
  831. bincode:=bincode+'%';
  832. '0':
  833. begin
  834. bincode:=bincode+'0';
  835. maskcode:=maskcode+'0';
  836. end;
  837. '1':
  838. begin
  839. bincode:=bincode+'1';
  840. maskcode:=maskcode+'0';
  841. end;
  842. 'p','d','r','q','c','b':
  843. begin
  844. bincode:=bincode+'0';
  845. maskcode:=maskcode+token[i];
  846. end;
  847. '''':
  848. begin
  849. if (maskcode='') or (maskcode[length(maskcode)]<>'r') then
  850. internalerror(2020050425);
  851. maskcode[length(maskcode)]:='R';
  852. end;
  853. else
  854. internalerror(2020050405);
  855. end;
  856. Val(bincode,b,valcode);
  857. while maskcode[1]='0' do
  858. delete(maskcode,1,1);
  859. shiftcount:=0;
  860. while maskcode[length(maskcode)]='0' do
  861. begin
  862. delete(maskcode,length(maskcode),1);
  863. Inc(shiftcount);
  864. end;
  865. b:=b or (EvalMaskCode(maskcode) shl shiftcount);
  866. objdata.writebytes(b,1);
  867. end;
  868. var
  869. i: Integer;
  870. ch: Char;
  871. b: Byte;
  872. valcode: integer;
  873. code: string;
  874. token: string;
  875. begin
  876. { safety check }
  877. if objdata.currobjsec.size<>longword(insoffset) then
  878. internalerror(2020050401);
  879. code:=insentry^.code;
  880. i:=1;
  881. token:='';
  882. while i<=length(code) do
  883. begin
  884. ch:=code[i];
  885. Inc(i);
  886. if ch<>',' then
  887. token:=token+ch;
  888. if (ch=',') or (i>length(code)) then
  889. begin
  890. if token='' then
  891. internalerror(2020050424);
  892. if token[1]='$' then
  893. begin
  894. Val(token,b,valcode);
  895. WriteByte(b);
  896. end
  897. else if token[1]='%' then
  898. begin
  899. HandlePercent(token);
  900. end
  901. else if token='nn' then
  902. WriteNN
  903. else if token='n' then
  904. WriteN
  905. else if token='d' then
  906. WriteD
  907. else if token='e' then
  908. WriteE
  909. else
  910. internalerror(2020050503);
  911. token:='';
  912. end;
  913. end;
  914. end;
  915. procedure taicpu.init;
  916. begin
  917. insentry:=nil;
  918. LastInsOffset:=-1;
  919. InsOffset:=0;
  920. InsSize:=0;
  921. end;
  922. constructor taicpu.op_none(op : tasmop);
  923. begin
  924. inherited create(op);
  925. init;
  926. end;
  927. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  928. begin
  929. inherited create(op);
  930. init;
  931. ops:=1;
  932. loadreg(0,_op1);
  933. end;
  934. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  935. begin
  936. inherited create(op);
  937. init;
  938. ops:=1;
  939. loadref(0,_op1);
  940. end;
  941. constructor taicpu.op_const(op : tasmop;_op1 : LongInt);
  942. begin
  943. inherited create(op);
  944. init;
  945. ops:=1;
  946. loadconst(0,_op1);
  947. end;
  948. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  949. begin
  950. inherited create(op);
  951. init;
  952. ops:=2;
  953. loadreg(0,_op1);
  954. loadreg(1,_op2);
  955. end;
  956. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: LongInt);
  957. begin
  958. inherited create(op);
  959. init;
  960. ops:=2;
  961. loadreg(0,_op1);
  962. loadconst(1,_op2);
  963. end;
  964. constructor taicpu.op_const_reg(op:tasmop; _op1: LongInt; _op2: tregister);
  965. begin
  966. inherited create(op);
  967. init;
  968. ops:=2;
  969. loadconst(0,_op1);
  970. loadreg(1,_op2);
  971. end;
  972. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  973. begin
  974. inherited create(op);
  975. init;
  976. ops:=2;
  977. loadreg(0,_op1);
  978. loadref(1,_op2);
  979. end;
  980. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  981. begin
  982. inherited create(op);
  983. init;
  984. ops:=2;
  985. loadref(0,_op1);
  986. loadreg(1,_op2);
  987. end;
  988. constructor taicpu.op_ref_const(op: tasmop; _op1: treference; _op2: LongInt);
  989. begin
  990. inherited create(op);
  991. init;
  992. ops:=2;
  993. loadref(0,_op1);
  994. loadconst(1,_op2);
  995. end;
  996. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  997. begin
  998. inherited create(op);
  999. init;
  1000. is_jmp:=op in jmp_instructions;
  1001. condition:=cond;
  1002. ops:=1;
  1003. loadsymbol(0,_op1,0);
  1004. end;
  1005. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  1006. begin
  1007. inherited create(op);
  1008. init;
  1009. is_jmp:=op in jmp_instructions;
  1010. ops:=1;
  1011. loadsymbol(0,_op1,0);
  1012. end;
  1013. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  1014. begin
  1015. inherited create(op);
  1016. init;
  1017. ops:=1;
  1018. loadsymbol(0,_op1,_op1ofs);
  1019. end;
  1020. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1021. begin
  1022. result:=(
  1023. ((opcode in [A_LD]) and (regtype = R_INTREGISTER))
  1024. ) and
  1025. (ops=2) and
  1026. (oper[0]^.typ=top_reg) and
  1027. (oper[1]^.typ=top_reg) and
  1028. (oper[0]^.reg=oper[1]^.reg);
  1029. end;
  1030. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1031. begin
  1032. result:=operand_read;
  1033. case opcode of
  1034. A_LD,
  1035. A_POP:
  1036. if opnr=0 then
  1037. result:=operand_write;
  1038. A_PUSH,
  1039. A_BIT,
  1040. A_CP,
  1041. A_DJNZ,
  1042. A_JR,
  1043. A_JP,
  1044. A_JRJP,
  1045. A_CALL,
  1046. A_RET,
  1047. A_RETI,
  1048. A_RETN,
  1049. A_RST,
  1050. A_IM:
  1051. ;
  1052. A_SET,
  1053. A_RES:
  1054. if opnr=1 then
  1055. result:=operand_readwrite;
  1056. A_EX:
  1057. result:=operand_readwrite;
  1058. else
  1059. begin
  1060. if opnr=0 then
  1061. result:=operand_readwrite;
  1062. end;
  1063. end;
  1064. end;
  1065. procedure taicpu.ResetPass1;
  1066. begin
  1067. { we need to reset everything here, because the choosen insentry
  1068. can be invalid for a new situation where the previously optimized
  1069. insentry is not correct }
  1070. InsEntry:=nil;
  1071. InsSize:=0;
  1072. LastInsOffset:=-1;
  1073. end;
  1074. procedure taicpu.ResetPass2;
  1075. begin
  1076. { we are here in a second pass, check if the instruction can be optimized }
  1077. if assigned(InsEntry) and (opcode=A_JRJP) then
  1078. begin
  1079. InsEntry:=nil;
  1080. InsSize:=0;
  1081. end;
  1082. LastInsOffset:=-1;
  1083. end;
  1084. function taicpu.Pass1(objdata: TObjData): longint;
  1085. begin
  1086. Pass1:=0;
  1087. { Save the old offset and set the new offset }
  1088. InsOffset:=ObjData.CurrObjSec.Size;
  1089. { Error? }
  1090. if (Insentry=nil) and (InsSize=-1) then
  1091. exit;
  1092. { set the file postion }
  1093. current_filepos:=fileinfo;
  1094. { Get InsEntry }
  1095. if FindInsEntry(ObjData) then
  1096. begin
  1097. { Calculate instruction size }
  1098. InsSize:=calcsize(insentry);
  1099. LastInsOffset:=InsOffset;
  1100. Pass1:=InsSize;
  1101. exit;
  1102. end;
  1103. LastInsOffset:=-1;
  1104. end;
  1105. procedure taicpu.Pass2(objdata: TObjData);
  1106. begin
  1107. { error in pass1 ? }
  1108. if insentry=nil then
  1109. exit;
  1110. current_filepos:=fileinfo;
  1111. { Generate the instruction }
  1112. GenCode(objdata);
  1113. end;
  1114. function taicpu.CheckIfValid: boolean;
  1115. begin
  1116. result:=FindInsEntry(nil);
  1117. end;
  1118. function taicpu.GetString: string;
  1119. var
  1120. i : longint;
  1121. s : string;
  1122. first: Boolean;
  1123. begin
  1124. s:='['+std_op2str[opcode];
  1125. for i:=0 to ops-1 do
  1126. begin
  1127. with oper[i]^ do
  1128. begin
  1129. if i=0 then
  1130. begin
  1131. s:=s+' ';
  1132. if condition<>C_None then
  1133. s:=s+cond2str[condition]+',';
  1134. end
  1135. else
  1136. s:=s+',';
  1137. case typ of
  1138. top_reg:
  1139. s:=s+std_regname(reg);
  1140. top_const:
  1141. s:=s+'const';
  1142. top_ref:
  1143. case ref^.refaddr of
  1144. addr_full:
  1145. s:=s+'addr16';
  1146. addr_lo8:
  1147. s:=s+'addr_lo8';
  1148. addr_hi8:
  1149. s:=s+'addr_hi8';
  1150. addr_no:
  1151. begin
  1152. s:=s+'(';
  1153. first:=true;
  1154. if ref^.base<>NR_NO then
  1155. begin
  1156. first:=false;
  1157. s:=s+std_regname(ref^.base);
  1158. end;
  1159. if ref^.index<>NR_NO then
  1160. begin
  1161. if not first then
  1162. s:=s+'+';
  1163. first:=false;
  1164. s:=s+std_regname(ref^.index);
  1165. end;
  1166. if assigned(ref^.symbol) then
  1167. begin
  1168. if not first then
  1169. s:=s+'+';
  1170. first:=false;
  1171. s:=s+'addr16';
  1172. end;
  1173. if ref^.offset<>0 then
  1174. begin
  1175. if not first then
  1176. s:=s+'+';
  1177. if (ref^.offset>=-128) and (ref^.offset<=127) then
  1178. s:=s+'const8'
  1179. else
  1180. s:=s+'const16';
  1181. end;
  1182. s:=s+')';
  1183. end;
  1184. else
  1185. ;
  1186. end;
  1187. else
  1188. ;
  1189. end;
  1190. end;
  1191. end;
  1192. GetString:=s+']';
  1193. end;
  1194. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1195. begin
  1196. case getregtype(r) of
  1197. R_INTREGISTER :
  1198. result:=taicpu.op_reg_ref(A_LD,r,ref)
  1199. else
  1200. internalerror(200401041);
  1201. end;
  1202. end;
  1203. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1204. begin
  1205. case getregtype(r) of
  1206. R_INTREGISTER :
  1207. result:=taicpu.op_ref_reg(A_LD,ref,r);
  1208. else
  1209. internalerror(2004010403);
  1210. end;
  1211. end;
  1212. function is_ref_addr16(const ref: treference): Boolean;
  1213. begin
  1214. result:=(ref.base=NR_NO) and (ref.index=NR_NO);
  1215. end;
  1216. function is_ref_bc(const ref: treference): Boolean;
  1217. begin
  1218. result:=(((ref.base=NR_BC) and (ref.index=NR_NO)) or
  1219. ((ref.base=NR_NO) and (ref.index=NR_BC))) and
  1220. (ref.offset=0) and (ref.scalefactor<=1) and
  1221. (ref.symbol=nil) and (ref.relsymbol=nil);
  1222. end;
  1223. function is_ref_de(const ref: treference): Boolean;
  1224. begin
  1225. result:=(((ref.base=NR_DE) and (ref.index=NR_NO)) or
  1226. ((ref.base=NR_NO) and (ref.index=NR_DE))) and
  1227. (ref.offset=0) and (ref.scalefactor<=1) and
  1228. (ref.symbol=nil) and (ref.relsymbol=nil);
  1229. end;
  1230. function is_ref_hl(const ref: treference): Boolean;
  1231. begin
  1232. result:=(((ref.base=NR_HL) and (ref.index=NR_NO)) or
  1233. ((ref.base=NR_NO) and (ref.index=NR_HL))) and
  1234. (ref.offset=0) and (ref.scalefactor<=1) and
  1235. (ref.symbol=nil) and (ref.relsymbol=nil);
  1236. end;
  1237. function is_ref_sp(const ref: treference): Boolean;
  1238. begin
  1239. result:=(((ref.base=NR_SP) and (ref.index=NR_NO)) or
  1240. ((ref.base=NR_NO) and (ref.index=NR_SP))) and
  1241. (ref.offset=0) and (ref.scalefactor<=1) and
  1242. (ref.symbol=nil) and (ref.relsymbol=nil);
  1243. end;
  1244. function is_ref_ix(const ref: treference): Boolean;
  1245. begin
  1246. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1247. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1248. (ref.offset=0) and (ref.scalefactor<=1) and
  1249. (ref.symbol=nil) and (ref.relsymbol=nil);
  1250. end;
  1251. function is_ref_iy(const ref: treference): Boolean;
  1252. begin
  1253. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1254. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1255. (ref.offset=0) and (ref.scalefactor<=1) and
  1256. (ref.symbol=nil) and (ref.relsymbol=nil);
  1257. end;
  1258. function is_ref_ix_d(const ref: treference): Boolean;
  1259. begin
  1260. result:=(((ref.base=NR_IX) and (ref.index=NR_NO)) or
  1261. ((ref.base=NR_NO) and (ref.index=NR_IX))) and
  1262. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1263. (ref.symbol=nil) and (ref.relsymbol=nil);
  1264. end;
  1265. function is_ref_iy_d(const ref: treference): Boolean;
  1266. begin
  1267. result:=(((ref.base=NR_IY) and (ref.index=NR_NO)) or
  1268. ((ref.base=NR_NO) and (ref.index=NR_IY))) and
  1269. (ref.offset>=-128) and (ref.offset<=127) and (ref.scalefactor<=1) and
  1270. (ref.symbol=nil) and (ref.relsymbol=nil);
  1271. end;
  1272. function is_ref_opertype(const ref: treference; opertype: toperandtype): Boolean;
  1273. begin
  1274. case opertype of
  1275. OT_REF_ADDR16:
  1276. result:=is_ref_addr16(ref);
  1277. OT_REF_BC:
  1278. result:=is_ref_bc(ref);
  1279. OT_REF_DE:
  1280. result:=is_ref_de(ref);
  1281. OT_REF_HL:
  1282. result:=is_ref_hl(ref);
  1283. OT_REF_SP:
  1284. result:=is_ref_sp(ref);
  1285. OT_REF_IX:
  1286. result:=is_ref_ix(ref);
  1287. OT_REF_IY:
  1288. result:=is_ref_iy(ref);
  1289. OT_REF_IX_d:
  1290. result:=is_ref_ix_d(ref);
  1291. OT_REF_IY_d:
  1292. result:=is_ref_iy_d(ref);
  1293. else
  1294. internalerror(2020041801);
  1295. end;
  1296. end;
  1297. function is_ref_in_opertypes(const ref: treference; const refopertypes: trefoperandtypes): Boolean;
  1298. var
  1299. ot: trefoperandtype;
  1300. begin
  1301. result:=true;
  1302. for ot:=low(trefoperandtypes) to high(trefoperandtypes) do
  1303. if (ot in refopertypes) and is_ref_opertype(ref,ot) then
  1304. exit;
  1305. result:=false;
  1306. end;
  1307. {****************************************************************************
  1308. Instruction table
  1309. *****************************************************************************}
  1310. procedure BuildInsTabCache;
  1311. var
  1312. i : longint;
  1313. begin
  1314. new(instabcache);
  1315. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1316. i:=0;
  1317. while (i<InsTabEntries) do
  1318. begin
  1319. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1320. InsTabCache^[InsTab[i].OPcode]:=i;
  1321. inc(i);
  1322. end;
  1323. end;
  1324. procedure InitAsm;
  1325. begin
  1326. if not assigned(instabcache) then
  1327. BuildInsTabCache;
  1328. end;
  1329. procedure DoneAsm;
  1330. begin
  1331. if assigned(instabcache) then
  1332. begin
  1333. dispose(instabcache);
  1334. instabcache:=nil;
  1335. end;
  1336. end;
  1337. begin
  1338. cai_cpu:=taicpu;
  1339. cai_align:=tai_align;
  1340. end.