agz80vasm.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm 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. { This unit implements the assembler writer for the vasm assembler:
  18. http://sun.hasenbraten.de/vasm/
  19. }
  20. unit agz80vasm;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,systems,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. assemble,
  27. cpubase;
  28. type
  29. { TZ80vasm }
  30. TZ80Vasm=class(TExternalAssembler)
  31. private
  32. procedure WriteDecodedSleb128(a: int64);
  33. procedure WriteDecodedUleb128(a: qword);
  34. procedure WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  35. function sectionattrs(atype:TAsmSectiontype):string;
  36. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  37. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;
  38. secflags:TSectionFlags=[];secprogbits:TSectionProgbits=SPB_None);
  39. procedure WriteInstruction(hp: taicpu);
  40. procedure WriteOper(const o:toper; opcode: tasmop;ops:longint;dest : boolean);
  41. procedure WriteOper_jmp(const o:toper; ai : taicpu);
  42. procedure WriteExternals;
  43. public
  44. procedure WriteTree(p : TAsmList); override;
  45. procedure WriteAsmList;override;
  46. function MakeCmdLine: TCmdStr; override;
  47. end;
  48. implementation
  49. uses
  50. cutils,globals,verbose,
  51. cpuinfo,
  52. cgbase,cgutils,
  53. {$ifdef FPC_SOFT_FPUX80}
  54. sfpux80,
  55. {$endif FPC_SOFT_FPUX80}
  56. finput;
  57. const
  58. line_length = 70;
  59. max_tokens : longint = 25;
  60. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  61. #9''#9,#9'FIXMEDQ'#9,#9'FIXMEDD'#9,#9'.word'#9,#9'.byte'#9,
  62. #9'FIXMESLEB',#9'FIXEMEULEB',
  63. #9'FIXMEDD RVA'#9,#9'FIXMEDD SECREL32'#9,
  64. #9'FIXME',#9'FIXME',#9'FIXME',#9'FIXME',
  65. #9'.uahalf'#9,#9'FIXMEDD'#9,#9'FIXMEDQ'#9
  66. );
  67. procedure TZ80vasm.WriteDecodedSleb128(a: int64);
  68. var
  69. i,len : longint;
  70. buf : array[0..255] of byte;
  71. begin
  72. writer.AsmWrite(#9'.byte'#9);
  73. len:=EncodeSleb128(a,buf,0);
  74. for i:=0 to len-1 do
  75. begin
  76. if (i > 0) then
  77. writer.AsmWrite(',');
  78. writer.AsmWrite(tostr(buf[i]));
  79. end;
  80. writer.AsmWriteLn(#9'; sleb '+tostr(a));
  81. end;
  82. procedure TZ80vasm.WriteDecodedUleb128(a: qword);
  83. var
  84. i,len : longint;
  85. buf : array[0..63] of byte;
  86. begin
  87. writer.AsmWrite(#9'.byte'#9);
  88. len:=EncodeUleb128(a,buf,0);
  89. for i:=0 to len-1 do
  90. begin
  91. if (i > 0) then
  92. writer.AsmWrite(',');
  93. writer.AsmWrite(tostr(buf[i]));
  94. end;
  95. writer.AsmWriteLn(#9'; uleb '+tostr(a));
  96. end;
  97. procedure TZ80vasm.WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  98. var
  99. pdata: pbyte;
  100. index, step, swapmask, count: longint;
  101. ssingle: single;
  102. ddouble: double;
  103. ccomp: comp;
  104. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  105. eextended: extended;
  106. {$else}
  107. {$ifdef FPC_SOFT_FPUX80}
  108. eextended: floatx80;
  109. {$endif}
  110. {$endif cpuextended}
  111. begin
  112. if do_line then
  113. begin
  114. case tai_realconst(hp).realtyp of
  115. aitrealconst_s32bit:
  116. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  117. aitrealconst_s64bit:
  118. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  119. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  120. { can't write full 80 bit floating point constants yet on non-x86 }
  121. aitrealconst_s80bit:
  122. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  123. {$else}
  124. {$ifdef FPC_SOFT_FPUX80}
  125. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  126. aitrealconst_s80bit:
  127. begin
  128. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  129. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  130. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  131. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  132. else
  133. internalerror(2017091901);
  134. end;
  135. {$pop}
  136. {$endif}
  137. {$endif cpuextended}
  138. aitrealconst_s64comp:
  139. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  140. else
  141. internalerror(2014050604);
  142. end;
  143. end;
  144. writer.AsmWrite(dbdir);
  145. { generic float writing code: get start address of value, then write
  146. byte by byte. Can't use fields directly, because e.g ts64comp is
  147. defined as extended on x86 }
  148. case tai_realconst(hp).realtyp of
  149. aitrealconst_s32bit:
  150. begin
  151. ssingle:=single(tai_realconst(hp).value.s32val);
  152. pdata:=@ssingle;
  153. end;
  154. aitrealconst_s64bit:
  155. begin
  156. ddouble:=double(tai_realconst(hp).value.s64val);
  157. pdata:=@ddouble;
  158. end;
  159. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  160. { can't write full 80 bit floating point constants yet on non-x86 }
  161. aitrealconst_s80bit:
  162. begin
  163. eextended:=extended(tai_realconst(hp).value.s80val);
  164. pdata:=@eextended;
  165. end;
  166. {$else}
  167. {$ifdef FPC_SOFT_FPUX80}
  168. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  169. aitrealconst_s80bit:
  170. begin
  171. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  172. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  173. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  174. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  175. else
  176. internalerror(2017091901);
  177. pdata:=@eextended;
  178. end;
  179. {$pop}
  180. {$endif}
  181. {$endif cpuextended}
  182. aitrealconst_s64comp:
  183. begin
  184. ccomp:=comp(tai_realconst(hp).value.s64compval);
  185. pdata:=@ccomp;
  186. end;
  187. else
  188. internalerror(2014051001);
  189. end;
  190. count:=tai_realconst(hp).datasize;
  191. { write bytes in inverse order if source and target endianess don't
  192. match }
  193. if source_info.endian<>target_info.endian then
  194. begin
  195. { go from back to front }
  196. index:=count-1;
  197. step:=-1;
  198. end
  199. else
  200. begin
  201. index:=0;
  202. step:=1;
  203. end;
  204. {$ifdef ARM}
  205. { ARM-specific: low and high dwords of a double may be swapped }
  206. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  207. begin
  208. { only supported for double }
  209. if tai_realconst(hp).datasize<>8 then
  210. internalerror(2014050605);
  211. { switch bit of the index so that the words are written in
  212. the opposite order }
  213. swapmask:=4;
  214. end
  215. else
  216. {$endif ARM}
  217. swapmask:=0;
  218. repeat
  219. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  220. inc(index,step);
  221. dec(count);
  222. if count<>0 then
  223. writer.AsmWrite(',');
  224. until count=0;
  225. { padding }
  226. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  227. writer.AsmWrite(',0');
  228. writer.AsmLn;
  229. end;
  230. function TZ80vasm.sectionattrs(atype:TAsmSectiontype):string;
  231. begin
  232. case atype of
  233. sec_code, sec_fpc, sec_init, sec_fini:
  234. result:='acrx';
  235. sec_data, sec_rodata, sec_rodata_norel, sec_bss, sec_threadvar:
  236. result:='adrw';
  237. sec_stab, sec_stabstr:
  238. result:='dr';
  239. else
  240. result:='';
  241. end;
  242. end;
  243. function TZ80vasm.sectionname(atype: TAsmSectiontype;
  244. const aname: string; aorder: TAsmSectionOrder): string;
  245. const
  246. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  247. '.text',
  248. '.data',
  249. '.data',
  250. '.rodata',
  251. '.bss',
  252. '.threadvar',
  253. '.pdata',
  254. '', { stubs }
  255. '__DATA,__nl_symbol_ptr',
  256. '__DATA,__la_symbol_ptr',
  257. '__DATA,__mod_init_func',
  258. '__DATA,__mod_term_func',
  259. '.stab',
  260. '.stabstr',
  261. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  262. '.eh_frame',
  263. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  264. '.fpc',
  265. '.toc',
  266. '.init',
  267. '.fini',
  268. '.objc_class',
  269. '.objc_meta_class',
  270. '.objc_cat_cls_meth',
  271. '.objc_cat_inst_meth',
  272. '.objc_protocol',
  273. '.objc_string_object',
  274. '.objc_cls_meth',
  275. '.objc_inst_meth',
  276. '.objc_cls_refs',
  277. '.objc_message_refs',
  278. '.objc_symbols',
  279. '.objc_category',
  280. '.objc_class_vars',
  281. '.objc_instance_vars',
  282. '.objc_module_info',
  283. '.objc_class_names',
  284. '.objc_meth_var_types',
  285. '.objc_meth_var_names',
  286. '.objc_selector_strs',
  287. '.objc_protocol_ext',
  288. '.objc_class_ext',
  289. '.objc_property',
  290. '.objc_image_info',
  291. '.objc_cstring_object',
  292. '.objc_sel_fixup',
  293. '__DATA,__objc_data',
  294. '__DATA,__objc_const',
  295. '.objc_superrefs',
  296. '__DATA, __datacoal_nt,coalesced',
  297. '.objc_classlist',
  298. '.objc_nlclasslist',
  299. '.objc_catlist',
  300. '.obcj_nlcatlist',
  301. '.objc_protolist',
  302. '.stack',
  303. '.heap',
  304. '.gcc_except_table',
  305. '.ARM.attributes'
  306. );
  307. var
  308. sep: string[3];
  309. begin
  310. result:=secnames[atype];
  311. if (aname<>'') then
  312. begin
  313. case aorder of
  314. secorder_begin :
  315. sep:='.b_';
  316. secorder_end :
  317. sep:='.z_';
  318. else
  319. sep:='.n_';
  320. end;
  321. result:=result+sep+aname;
  322. end;
  323. { else
  324. result:=secname;}
  325. end;
  326. procedure TZ80vasm.WriteSection(atype: TAsmSectiontype;
  327. const aname: string; aorder: TAsmSectionOrder; secalign: longint;
  328. secflags: TSectionFlags; secprogbits: TSectionProgbits);
  329. var
  330. s : string;
  331. secflag: TSectionFlag;
  332. sectionprogbits,
  333. sectionflags: boolean;
  334. begin
  335. writer.AsmLn;
  336. sectionflags:=false;
  337. sectionprogbits:=false;
  338. writer.AsmWrite(#9'.section ');
  339. { sectionname may rename those sections, so we do not write flags/progbits for them,
  340. the assembler will ignore them/spite out a warning anyways }
  341. if not(atype in [sec_data,sec_rodata,sec_rodata_norel]) then
  342. begin
  343. sectionflags:=true;
  344. sectionprogbits:=true;
  345. end;
  346. s:=sectionname(atype,aname,aorder);
  347. writer.AsmWrite(s);
  348. s:=sectionattrs(atype);
  349. if (s<>'') then
  350. writer.AsmWrite(',"'+s+'"');
  351. writer.AsmLn;
  352. LastSecType:=atype;
  353. end;
  354. procedure TZ80vasm.WriteInstruction(hp: taicpu);
  355. var
  356. i: Integer;
  357. begin
  358. writer.AsmWrite(#9#9+std_op2str[hp.opcode]);
  359. if (taicpu(hp).ops<>0) or (hp.condition<>C_None) then
  360. begin
  361. writer.AsmWrite(#9);
  362. if hp.condition<>C_None then
  363. begin
  364. writer.AsmWrite(uppercond2str[hp.condition]);
  365. if taicpu(hp).ops<>0 then
  366. writer.AsmWrite(',');
  367. end;
  368. for i:=0 to taicpu(hp).ops-1 do
  369. begin
  370. if i<>0 then
  371. writer.AsmWrite(',');
  372. if is_calljmp(hp.opcode) then
  373. WriteOper_jmp(taicpu(hp).oper[i]^,hp)
  374. else
  375. WriteOper(taicpu(hp).oper[i]^,hp.opcode,taicpu(hp).ops,(i=2));
  376. end;
  377. end;
  378. writer.AsmLn;
  379. end;
  380. procedure TZ80vasm.WriteOper(const o: toper; opcode: tasmop; ops: longint; dest: boolean);
  381. var
  382. need_plus: Boolean;
  383. begin
  384. case o.typ of
  385. top_reg :
  386. writer.AsmWrite(std_regname(o.reg));
  387. top_const :
  388. begin
  389. writer.AsmWrite(tostr(longint(o.val)));
  390. end;
  391. top_ref:
  392. begin
  393. if assigned(o.ref^.symbol) and (o.ref^.refaddr in [addr_lo8,addr_hi8,addr_full]) then
  394. begin
  395. {if SmartAsm then
  396. AddSymbol(o.ref^.symbol.name,false);}
  397. if (o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO) then
  398. internalerror(2020041101);
  399. // writer.AsmWrite('#');
  400. case o.ref^.refaddr of
  401. addr_lo8:
  402. writer.AsmWrite('<');
  403. addr_hi8:
  404. writer.AsmWrite('>');
  405. addr_full:
  406. {nothing};
  407. else
  408. ;
  409. end;
  410. if o.ref^.offset<>0 then
  411. writer.AsmWrite('('+ApplyAsmSymbolRestrictions(o.ref^.symbol.name)+'+'+tostr(o.ref^.offset)+')')
  412. else
  413. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  414. end
  415. else if not assigned(o.ref^.symbol) and
  416. ((o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO)) and
  417. (o.ref^.offset<>0) then
  418. begin
  419. //writer.AsmWrite(tostr(o.ref^.offset));
  420. writer.AsmWrite('(');
  421. if o.ref^.base<>NR_NO then
  422. begin
  423. if o.ref^.index<>NR_NO then
  424. internalerror(2020040201);
  425. writer.AsmWrite(std_regname(o.ref^.base));
  426. end
  427. else if o.ref^.index<>NR_NO then
  428. begin
  429. if o.ref^.scalefactor>1 then
  430. internalerror(2020040202);
  431. writer.AsmWrite(std_regname(o.ref^.index));
  432. end;
  433. if o.ref^.offset > 0 then
  434. writer.AsmWrite('+'+tostr(o.ref^.offset))
  435. else
  436. if o.ref^.offset < 0 then
  437. writer.AsmWrite(tostr(o.ref^.offset));
  438. writer.AsmWrite(')');
  439. end
  440. else
  441. begin
  442. writer.AsmWrite('(');
  443. need_plus:=false;
  444. if o.ref^.base<>NR_NO then
  445. begin
  446. if o.ref^.index<>NR_NO then
  447. internalerror(2020040201);
  448. writer.AsmWrite(std_regname(o.ref^.base));
  449. need_plus:=true;
  450. end
  451. else if o.ref^.index<>NR_NO then
  452. begin
  453. if o.ref^.scalefactor>1 then
  454. internalerror(2020040202);
  455. writer.AsmWrite(std_regname(o.ref^.index));
  456. need_plus:=true;
  457. end;
  458. if assigned(o.ref^.symbol) then
  459. begin
  460. {if SmartAsm then
  461. AddSymbol(o.ref^.symbol.name,false);}
  462. if need_plus then
  463. writer.AsmWrite('+');
  464. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  465. need_plus:=true;
  466. end;
  467. if o.ref^.offset<>0 then
  468. begin
  469. if need_plus and (o.ref^.offset>0) then
  470. writer.AsmWrite('+');
  471. writer.AsmWrite(tostr(o.ref^.offset));
  472. need_plus:=true;
  473. end;
  474. if not need_plus then
  475. writer.AsmWrite('0');
  476. writer.AsmWrite(')');
  477. end;
  478. end;
  479. else
  480. internalerror(10001);
  481. end;
  482. end;
  483. procedure TZ80vasm.WriteOper_jmp(const o: toper; ai: taicpu);
  484. begin
  485. case o.typ of
  486. top_reg :
  487. writer.AsmWrite(std_regname(o.reg));
  488. top_const :
  489. begin
  490. writer.AsmWrite(tostr(longint(o.val)));
  491. end;
  492. top_ref:
  493. begin
  494. if o.ref^.refaddr=addr_no then
  495. begin
  496. writer.AsmWrite('TODO:indirect jump ref');
  497. //WriteReference(o.ref^);
  498. end
  499. else
  500. begin
  501. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  502. //if SmartAsm then
  503. // AddSymbol(o.ref^.symbol.name,false);
  504. if o.ref^.offset>0 then
  505. writer.AsmWrite('+'+tostr(o.ref^.offset))
  506. else
  507. if o.ref^.offset<0 then
  508. writer.AsmWrite(tostr(o.ref^.offset));
  509. end;
  510. end;
  511. else
  512. internalerror(10001);
  513. end;
  514. end;
  515. procedure TZ80vasm.WriteExternals;
  516. var
  517. sym : TAsmSymbol;
  518. i : longint;
  519. begin
  520. writer.AsmWriteln('; Begin externals');
  521. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  522. begin
  523. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  524. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  525. writer.AsmWriteln(#9'.globl'#9+ApplyAsmSymbolRestrictions(sym.name));
  526. end;
  527. writer.AsmWriteln('; End externals');
  528. end;
  529. procedure TZ80vasm.WriteTree(p: TAsmList);
  530. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  531. var
  532. i: longint;
  533. alignment64 : int64;
  534. begin
  535. last_align:=alignment;
  536. if alignment>1 then
  537. writer.AsmWriteLn(#9'.balign '+tostr(alignment));
  538. end;
  539. var
  540. lasthp,
  541. hp: tai;
  542. s, LastSecName: string;
  543. counter,lines,i,j,l,tokens,pos,last_align: longint;
  544. quoted, do_line: Boolean;
  545. consttype: taiconst_type;
  546. ch: Char;
  547. InlineLevel : longint;
  548. prevfileinfo : tfileposinfo;
  549. previnfile : tinputfile;
  550. LastAlign: Integer;
  551. LastSecOrder: TAsmSectionOrder;
  552. begin
  553. if not assigned(p) then
  554. exit;
  555. InlineLevel:=0;
  556. last_align:=1;
  557. lasthp:=nil;
  558. { lineinfo is only needed for al_procedures (PFV) }
  559. do_line:=(cs_asm_source in current_settings.globalswitches) or
  560. ((cs_lineinfo in current_settings.moduleswitches)
  561. and (p=current_asmdata.asmlists[al_procedures]));
  562. hp:=tai(p.first);
  563. while assigned(hp) do
  564. begin
  565. prefetch(pointer(hp.next)^);
  566. if not(hp.typ in SkipLineInfo) then
  567. begin
  568. previnfile:=lastinfile;
  569. prevfileinfo:=lastfileinfo;
  570. current_filepos:=tailineinfo(hp).fileinfo;
  571. { no line info for inlined code }
  572. if do_line and (inlinelevel=0) then
  573. WriteSourceLine(hp as tailineinfo);
  574. (*if (lastfileinfo.line<>prevfileinfo.line) or
  575. (previnfile<>lastinfile) then
  576. begin
  577. { +0 postfix means no line increment per assembler instruction }
  578. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  579. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  580. writer.AsmWriteLn(' '+lastinfile.name)
  581. else
  582. writer.AsmLn;
  583. NewObject:=false;
  584. end;*)
  585. end;
  586. case hp.typ of
  587. ait_comment :
  588. begin
  589. writer.AsmWrite(asminfo^.comment);
  590. writer.AsmWritePChar(tai_comment(hp).str);
  591. writer.AsmLn;
  592. end;
  593. ait_regalloc :
  594. begin
  595. if (cs_asm_regalloc in current_settings.globalswitches) then
  596. writer.AsmWriteLn(#9#9+asminfo^.comment+'Register '+std_regname(tai_regalloc(hp).reg)+' '+
  597. regallocstr[tai_regalloc(hp).ratype]);
  598. end;
  599. ait_tempalloc :
  600. begin
  601. if (cs_asm_tempalloc in current_settings.globalswitches) then
  602. WriteTempalloc(tai_tempalloc(hp));
  603. end;
  604. ait_section :
  605. begin
  606. if tai_section(hp).sectype<>sec_none then
  607. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  608. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  609. else
  610. begin
  611. {$ifdef EXTDEBUG}
  612. writer.AsmWrite(asminfo^.comment);
  613. writer.AsmWriteln(' sec_none');
  614. {$endif EXTDEBUG}
  615. end;
  616. end;
  617. ait_align :
  618. begin
  619. doalign(tai_align_abstract(hp).aligntype,tai_align_abstract(hp).use_op,tai_align_abstract(hp).fillop,tai_align_abstract(hp).maxbytes,last_align,lasthp);
  620. end;
  621. ait_label :
  622. begin
  623. if tai_label(hp).labsym.is_used then
  624. begin
  625. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
  626. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  627. writer.AsmWriteLn(':')
  628. else
  629. writer.AsmWriteLn(':');
  630. end;
  631. end;
  632. ait_symbol :
  633. begin
  634. if not(tai_symbol(hp).has_value) then
  635. begin
  636. if tai_symbol(hp).is_global then
  637. writer.AsmWriteLn(#9'.globl '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  638. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + ':');
  639. end
  640. else
  641. begin
  642. if tai_symbol(hp).is_global then
  643. writer.AsmWriteLn(#9'.globl '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  644. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '=' + tostr(tai_symbol(hp).value));
  645. end;
  646. end;
  647. ait_symbol_end :
  648. begin
  649. end;
  650. ait_datablock :
  651. begin
  652. if tai_datablock(hp).is_global or SmartAsm then
  653. writer.AsmWriteLn(#9'.globl '+ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  654. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name) + ':');
  655. {if SmartAsm then
  656. AddSymbol(tai_datablock(hp).sym.name,true);}
  657. writer.AsmWriteLn(#9'.zero'#9+tostr(tai_datablock(hp).size));
  658. end;
  659. ait_realconst:
  660. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  661. ait_const:
  662. begin
  663. consttype:=tai_const(hp).consttype;
  664. case consttype of
  665. aitconst_uleb128bit:
  666. WriteDecodedUleb128(qword(tai_const(hp).value));
  667. aitconst_sleb128bit:
  668. WriteDecodedSleb128(int64(tai_const(hp).value));
  669. aitconst_64bit,
  670. aitconst_64bit_unaligned,
  671. aitconst_32bit,
  672. aitconst_32bit_unaligned:
  673. begin
  674. writer.AsmWrite(#9'.uahalf'#9);
  675. l:=0;
  676. tokens:=1;
  677. repeat
  678. if assigned(tai_const(hp).sym) then
  679. begin
  680. if assigned(tai_const(hp).endsym) then
  681. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  682. else
  683. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  684. if tai_const(hp).value<>0 then
  685. s:=s+tostr_with_plus(tai_const(hp).value);
  686. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  687. s:=s+',0,0,0'
  688. else
  689. s:=s+',0';
  690. end
  691. else
  692. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  693. s:=tostr(Word(tai_const(hp).value)) +','+tostr(Word(tai_const(hp).value shr 16))+','+
  694. tostr(Word(tai_const(hp).value shr 32))+','+tostr(Word(tai_const(hp).value shr 48))
  695. else
  696. s:=tostr(Word(tai_const(hp).value))+','+tostr(Word(tai_const(hp).value shr 16));
  697. writer.AsmWrite(s);
  698. inc(l,length(s));
  699. inc(tokens);
  700. if (l>line_length) or
  701. (tokens>max_tokens) or
  702. (hp.next=nil) or
  703. (tai(hp.next).typ<>ait_const) or
  704. (tai_const(hp.next).consttype<>consttype) then
  705. break;
  706. hp:=tai(hp.next);
  707. writer.AsmWrite(',');
  708. until false;
  709. { Substract section start for secrel32 type }
  710. {if consttype=aitconst_secrel32_symbol then
  711. writer.AsmWrite(' - $$');}
  712. writer.AsmLn;
  713. end;
  714. {aitconst_128bit,}
  715. aitconst_16bit,
  716. aitconst_8bit,
  717. aitconst_16bit_unaligned{,
  718. aitconst_rva_symbol,
  719. aitconst_secrel32_symbol} :
  720. begin
  721. writer.AsmWrite(ait_const2str[consttype]);
  722. l:=0;
  723. tokens:=1;
  724. repeat
  725. if assigned(tai_const(hp).sym) then
  726. begin
  727. if assigned(tai_const(hp).endsym) then
  728. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  729. else
  730. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  731. if tai_const(hp).value<>0 then
  732. s:=s+tostr_with_plus(tai_const(hp).value);
  733. end
  734. else
  735. s:=tostr(tai_const(hp).value);
  736. writer.AsmWrite(s);
  737. inc(l,length(s));
  738. inc(tokens);
  739. if (l>line_length) or
  740. (tokens>max_tokens) or
  741. (hp.next=nil) or
  742. (tai(hp.next).typ<>ait_const) or
  743. (tai_const(hp.next).consttype<>consttype) then
  744. break;
  745. hp:=tai(hp.next);
  746. writer.AsmWrite(',');
  747. until false;
  748. { Substract section start for secrel32 type }
  749. if consttype=aitconst_secrel32_symbol then
  750. writer.AsmWrite(' - $$');
  751. writer.AsmLn;
  752. end;
  753. else
  754. begin
  755. writer.AsmWrite(asminfo^.comment);
  756. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  757. Str(consttype,s);
  758. writer.AsmWriteLn(s);
  759. end;
  760. end;
  761. end;
  762. ait_string :
  763. begin
  764. pos:=0;
  765. for i:=1 to tai_string(hp).len do
  766. begin
  767. if pos=0 then
  768. begin
  769. writer.AsmWrite(#9'.ascii'#9'"');
  770. pos:=20;
  771. end;
  772. ch:=tai_string(hp).str[i-1];
  773. case ch of
  774. #0, {This can't be done by range, because a bug in FPC}
  775. #1..#31,
  776. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  777. '"' : s:='\"';
  778. '\' : s:='\\';
  779. else
  780. s:=ch;
  781. end;
  782. writer.AsmWrite(s);
  783. inc(pos,length(s));
  784. if (pos>line_length) or (i=tai_string(hp).len) then
  785. begin
  786. writer.AsmWriteLn('"');
  787. pos:=0;
  788. end;
  789. end;
  790. end;
  791. ait_instruction :
  792. begin
  793. WriteInstruction(taicpu(hp));
  794. end;
  795. ait_directive :
  796. begin
  797. case tai_directive(hp).directive of
  798. asd_cpu :
  799. writer.AsmWriteLn('; CPU '+tai_directive(hp).name);
  800. else
  801. begin
  802. writer.AsmWrite(asminfo^.comment);
  803. writer.AsmWrite('WARNING: not yet implemented in assembler output: ait_directive.');
  804. Str(tai_directive(hp).directive,s);
  805. writer.AsmWriteLn(s);
  806. end;
  807. end;
  808. end;
  809. ait_cutobject :
  810. begin
  811. if SmartAsm then
  812. begin
  813. { only reset buffer if nothing has changed }
  814. if not writer.ClearIfEmpty then
  815. begin
  816. {if SmartAsm then
  817. begin
  818. WriteSmartExternals;
  819. FreeExternChainList;
  820. end;
  821. WriteGroups;}
  822. writer.AsmClose;
  823. DoAssemble;
  824. writer.AsmCreate(tai_cutobject(hp).place);
  825. {ResetSectionsList;
  826. WriteHeader;}
  827. end;
  828. { avoid empty files }
  829. LastSecType:=sec_none;
  830. LastSecName:='';
  831. LastSecOrder:=secorder_default;
  832. LastAlign:=1;
  833. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  834. begin
  835. if tai(hp.next).typ=ait_section then
  836. begin
  837. LastSecType:=tai_section(hp.next).sectype;
  838. LastSecName:=tai_section(hp.next).name^;
  839. LastSecOrder:=tai_section(hp.next).secorder;
  840. LastAlign:=tai_section(hp.next).secalign;
  841. end;
  842. hp:=tai(hp.next);
  843. end;
  844. if LastSecType<>sec_none then
  845. WriteSection(LastSecType,LastSecName,LastSecOrder,LastAlign);
  846. writer.MarkEmpty;
  847. //NewObject:=true;
  848. end;
  849. end;
  850. ait_marker :
  851. if tai_marker(hp).kind=mark_NoLineInfoStart then
  852. inc(InlineLevel)
  853. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  854. dec(InlineLevel);
  855. ait_stab,
  856. ait_force_line,
  857. ait_function_name : ;
  858. else
  859. begin
  860. writer.AsmWrite(asminfo^.comment);
  861. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  862. Str(hp.typ,s);
  863. writer.AsmWriteLn(s);
  864. end;
  865. end;
  866. lasthp:=hp;
  867. hp:=tai(hp.next);
  868. end;
  869. end;
  870. procedure TZ80vasm.WriteAsmList;
  871. var
  872. hal: TAsmListType;
  873. begin
  874. WriteExternals;
  875. for hal:=low(TasmlistType) to high(TasmlistType) do
  876. begin
  877. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  878. writetree(current_asmdata.asmlists[hal]);
  879. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  880. end;
  881. end;
  882. function TZ80vasm.MakeCmdLine: TCmdStr;
  883. begin
  884. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  885. end;
  886. const
  887. as_z80_vasm_info : tasminfo =
  888. (
  889. id : as_z80_vasm;
  890. idtxt : 'VASM';
  891. asmbin : 'vasmz80_std';
  892. asmcmd : '-quiet -Fvobj -o $OBJ $EXTRAOPT $ASM';
  893. supported_targets : [system_z80_embedded, system_z80_zxspectrum, system_z80_msxdos];
  894. flags : [af_needar,af_smartlink_sections];
  895. labelprefix : '.L';
  896. labelmaxlen : -1;
  897. comment : '; ';
  898. dollarsign: '$';
  899. );
  900. begin
  901. RegisterAssembler(as_z80_vasm_info,TZ80vasm);
  902. end.