aasmcpu.pas 45 KB

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