aasmcpu.pas 43 KB

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