agz80vasm.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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(2017091906);
  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(2014050602);
  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(2017091907);
  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(2014051003);
  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(2014050609);
  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. if atype=sec_user then
  311. result:=aname
  312. else
  313. result:=secnames[atype];
  314. if (aname<>'') then
  315. begin
  316. case aorder of
  317. secorder_begin :
  318. sep:='.b_';
  319. secorder_end :
  320. sep:='.z_';
  321. else
  322. sep:='.n_';
  323. end;
  324. result:=result+sep+aname;
  325. end;
  326. { else
  327. result:=secname;}
  328. end;
  329. procedure TZ80vasm.WriteSection(atype: TAsmSectiontype;
  330. const aname: string; aorder: TAsmSectionOrder; secalign: longint;
  331. secflags: TSectionFlags; secprogbits: TSectionProgbits);
  332. var
  333. s : string;
  334. secflag: TSectionFlag;
  335. sectionprogbits,
  336. sectionflags: boolean;
  337. begin
  338. writer.AsmLn;
  339. sectionflags:=false;
  340. sectionprogbits:=false;
  341. writer.AsmWrite(#9'.section ');
  342. { sectionname may rename those sections, so we do not write flags/progbits for them,
  343. the assembler will ignore them/spite out a warning anyways }
  344. if not(atype in [sec_data,sec_rodata,sec_rodata_norel]) then
  345. begin
  346. sectionflags:=true;
  347. sectionprogbits:=true;
  348. end;
  349. s:=sectionname(atype,aname,aorder);
  350. writer.AsmWrite(s);
  351. s:=sectionattrs(atype);
  352. if (s<>'') then
  353. writer.AsmWrite(',"'+s+'"');
  354. writer.AsmLn;
  355. LastSecType:=atype;
  356. end;
  357. procedure TZ80vasm.WriteInstruction(hp: taicpu);
  358. var
  359. i: Integer;
  360. begin
  361. if hp.opcode=A_JRJP then
  362. writer.AsmWrite(#9#9'jp')
  363. else
  364. writer.AsmWrite(#9#9+std_op2str[hp.opcode]);
  365. if (taicpu(hp).ops<>0) or (hp.condition<>C_None) then
  366. begin
  367. writer.AsmWrite(#9);
  368. if hp.condition<>C_None then
  369. begin
  370. writer.AsmWrite(uppercond2str[hp.condition]);
  371. if taicpu(hp).ops<>0 then
  372. writer.AsmWrite(',');
  373. end;
  374. for i:=0 to taicpu(hp).ops-1 do
  375. begin
  376. if i<>0 then
  377. writer.AsmWrite(',');
  378. if is_calljmp(hp.opcode) then
  379. WriteOper_jmp(taicpu(hp).oper[i]^,hp)
  380. else
  381. WriteOper(taicpu(hp).oper[i]^,hp.opcode,taicpu(hp).ops,(i=2));
  382. end;
  383. end;
  384. writer.AsmLn;
  385. end;
  386. procedure TZ80vasm.WriteOper(const o: toper; opcode: tasmop; ops: longint; dest: boolean);
  387. var
  388. need_plus: Boolean;
  389. begin
  390. case o.typ of
  391. top_reg :
  392. writer.AsmWrite(std_regname(o.reg));
  393. top_const :
  394. begin
  395. writer.AsmWrite(tostr(longint(o.val)));
  396. end;
  397. top_ref:
  398. begin
  399. if assigned(o.ref^.symbol) and (o.ref^.refaddr in [addr_lo8,addr_hi8,addr_full]) then
  400. begin
  401. {if SmartAsm then
  402. AddSymbol(o.ref^.symbol.name,false);}
  403. if (o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO) then
  404. internalerror(2020041102);
  405. // writer.AsmWrite('#');
  406. case o.ref^.refaddr of
  407. addr_lo8:
  408. writer.AsmWrite('<');
  409. addr_hi8:
  410. writer.AsmWrite('>');
  411. addr_full:
  412. {nothing};
  413. else
  414. ;
  415. end;
  416. if o.ref^.offset<>0 then
  417. writer.AsmWrite('('+ApplyAsmSymbolRestrictions(o.ref^.symbol.name)+'+'+tostr(o.ref^.offset)+')')
  418. else
  419. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  420. end
  421. else if not assigned(o.ref^.symbol) and
  422. ((o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO)) and
  423. (o.ref^.offset<>0) then
  424. begin
  425. //writer.AsmWrite(tostr(o.ref^.offset));
  426. writer.AsmWrite('(');
  427. if o.ref^.base<>NR_NO then
  428. begin
  429. if o.ref^.index<>NR_NO then
  430. internalerror(2020040204);
  431. writer.AsmWrite(std_regname(o.ref^.base));
  432. end
  433. else if o.ref^.index<>NR_NO then
  434. begin
  435. if o.ref^.scalefactor>1 then
  436. internalerror(2020040207);
  437. writer.AsmWrite(std_regname(o.ref^.index));
  438. end;
  439. if o.ref^.offset > 0 then
  440. writer.AsmWrite('+'+tostr(o.ref^.offset))
  441. else
  442. if o.ref^.offset < 0 then
  443. writer.AsmWrite(tostr(o.ref^.offset));
  444. writer.AsmWrite(')');
  445. end
  446. else
  447. begin
  448. writer.AsmWrite('(');
  449. need_plus:=false;
  450. if o.ref^.base<>NR_NO then
  451. begin
  452. if o.ref^.index<>NR_NO then
  453. internalerror(2020040205);
  454. writer.AsmWrite(std_regname(o.ref^.base));
  455. need_plus:=true;
  456. end
  457. else if o.ref^.index<>NR_NO then
  458. begin
  459. if o.ref^.scalefactor>1 then
  460. internalerror(2020040208);
  461. writer.AsmWrite(std_regname(o.ref^.index));
  462. need_plus:=true;
  463. end;
  464. if assigned(o.ref^.symbol) then
  465. begin
  466. {if SmartAsm then
  467. AddSymbol(o.ref^.symbol.name,false);}
  468. if need_plus then
  469. writer.AsmWrite('+');
  470. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  471. need_plus:=true;
  472. end;
  473. if o.ref^.offset<>0 then
  474. begin
  475. if need_plus and (o.ref^.offset>0) then
  476. writer.AsmWrite('+');
  477. writer.AsmWrite(tostr(o.ref^.offset));
  478. need_plus:=true;
  479. end;
  480. if not need_plus then
  481. writer.AsmWrite('0');
  482. writer.AsmWrite(')');
  483. end;
  484. end;
  485. else
  486. internalerror(2020100805);
  487. end;
  488. end;
  489. procedure TZ80vasm.WriteOper_jmp(const o: toper; ai: taicpu);
  490. begin
  491. case o.typ of
  492. top_reg :
  493. writer.AsmWrite(std_regname(o.reg));
  494. top_const :
  495. begin
  496. writer.AsmWrite(tostr(longint(o.val)));
  497. end;
  498. top_ref:
  499. begin
  500. if o.ref^.refaddr=addr_no then
  501. begin
  502. writer.AsmWrite('TODO:indirect jump ref');
  503. //WriteReference(o.ref^);
  504. end
  505. else
  506. begin
  507. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  508. //if SmartAsm then
  509. // AddSymbol(o.ref^.symbol.name,false);
  510. if o.ref^.offset>0 then
  511. writer.AsmWrite('+'+tostr(o.ref^.offset))
  512. else
  513. if o.ref^.offset<0 then
  514. writer.AsmWrite(tostr(o.ref^.offset));
  515. end;
  516. end;
  517. else
  518. internalerror(2020100806);
  519. end;
  520. end;
  521. procedure TZ80vasm.WriteExternals;
  522. var
  523. sym : TAsmSymbol;
  524. i : longint;
  525. begin
  526. writer.AsmWriteln('; Begin externals');
  527. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  528. begin
  529. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  530. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  531. writer.AsmWriteln(#9'.globl'#9+ApplyAsmSymbolRestrictions(sym.name));
  532. end;
  533. writer.AsmWriteln('; End externals');
  534. end;
  535. procedure TZ80vasm.WriteTree(p: TAsmList);
  536. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  537. var
  538. i: longint;
  539. alignment64 : int64;
  540. begin
  541. last_align:=alignment;
  542. if alignment>1 then
  543. writer.AsmWriteLn(#9'.balign '+tostr(alignment));
  544. end;
  545. var
  546. lasthp,
  547. hp: tai;
  548. s, LastSecName: string;
  549. counter,lines,i,j,l,tokens,pos,last_align: longint;
  550. quoted, do_line: Boolean;
  551. consttype: taiconst_type;
  552. ch: Char;
  553. InlineLevel : longint;
  554. prevfileinfo : tfileposinfo;
  555. previnfile : tinputfile;
  556. LastAlign: Integer;
  557. LastSecOrder: TAsmSectionOrder;
  558. begin
  559. if not assigned(p) then
  560. exit;
  561. InlineLevel:=0;
  562. last_align:=1;
  563. lasthp:=nil;
  564. { lineinfo is only needed for al_procedures (PFV) }
  565. do_line:=(cs_asm_source in current_settings.globalswitches) or
  566. ((cs_lineinfo in current_settings.moduleswitches)
  567. and (p=current_asmdata.asmlists[al_procedures]));
  568. hp:=tai(p.first);
  569. while assigned(hp) do
  570. begin
  571. prefetch(pointer(hp.next)^);
  572. if not(hp.typ in SkipLineInfo) then
  573. begin
  574. previnfile:=lastinfile;
  575. prevfileinfo:=lastfileinfo;
  576. current_filepos:=tailineinfo(hp).fileinfo;
  577. { no line info for inlined code }
  578. if do_line and (inlinelevel=0) then
  579. WriteSourceLine(hp as tailineinfo);
  580. (*if (lastfileinfo.line<>prevfileinfo.line) or
  581. (previnfile<>lastinfile) then
  582. begin
  583. { +0 postfix means no line increment per assembler instruction }
  584. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  585. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  586. writer.AsmWriteLn(' '+lastinfile.name)
  587. else
  588. writer.AsmLn;
  589. NewObject:=false;
  590. end;*)
  591. end;
  592. case hp.typ of
  593. ait_section :
  594. begin
  595. if tai_section(hp).sectype<>sec_none then
  596. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  597. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  598. else
  599. begin
  600. {$ifdef EXTDEBUG}
  601. writer.AsmWrite(asminfo^.comment);
  602. writer.AsmWriteln(' sec_none');
  603. {$endif EXTDEBUG}
  604. end;
  605. end;
  606. ait_align :
  607. begin
  608. 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);
  609. end;
  610. ait_label :
  611. begin
  612. if tai_label(hp).labsym.is_used then
  613. begin
  614. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
  615. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  616. writer.AsmWriteLn(':')
  617. else
  618. writer.AsmWriteLn(':');
  619. end;
  620. end;
  621. ait_symbol :
  622. begin
  623. if not(tai_symbol(hp).has_value) then
  624. begin
  625. if tai_symbol(hp).is_global then
  626. writer.AsmWriteLn(#9'.globl '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  627. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + ':');
  628. end
  629. else
  630. begin
  631. if tai_symbol(hp).is_global then
  632. writer.AsmWriteLn(#9'.globl '+ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name));
  633. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '=' + tostr(tai_symbol(hp).value));
  634. end;
  635. end;
  636. ait_symbol_end :
  637. begin
  638. end;
  639. ait_datablock :
  640. begin
  641. if tai_datablock(hp).is_global or SmartAsm then
  642. writer.AsmWriteLn(#9'.globl '+ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name));
  643. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name) + ':');
  644. {if SmartAsm then
  645. AddSymbol(tai_datablock(hp).sym.name,true);}
  646. writer.AsmWriteLn(#9'.zero'#9+tostr(tai_datablock(hp).size));
  647. end;
  648. ait_realconst:
  649. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  650. ait_const:
  651. begin
  652. consttype:=tai_const(hp).consttype;
  653. case consttype of
  654. aitconst_uleb128bit:
  655. WriteDecodedUleb128(qword(tai_const(hp).value));
  656. aitconst_sleb128bit:
  657. WriteDecodedSleb128(int64(tai_const(hp).value));
  658. aitconst_64bit,
  659. aitconst_64bit_unaligned,
  660. aitconst_32bit,
  661. aitconst_32bit_unaligned:
  662. begin
  663. writer.AsmWrite(#9'.uahalf'#9);
  664. l:=0;
  665. tokens:=1;
  666. repeat
  667. if assigned(tai_const(hp).sym) then
  668. begin
  669. if assigned(tai_const(hp).endsym) then
  670. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  671. else
  672. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  673. if tai_const(hp).value<>0 then
  674. s:=s+tostr_with_plus(tai_const(hp).value);
  675. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  676. s:=s+',0,0,0'
  677. else
  678. s:=s+',0';
  679. end
  680. else
  681. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  682. s:=tostr(Word(tai_const(hp).value)) +','+tostr(Word(tai_const(hp).value shr 16))+','+
  683. tostr(Word(tai_const(hp).value shr 32))+','+tostr(Word(tai_const(hp).value shr 48))
  684. else
  685. s:=tostr(Word(tai_const(hp).value))+','+tostr(Word(tai_const(hp).value shr 16));
  686. writer.AsmWrite(s);
  687. inc(l,length(s));
  688. inc(tokens);
  689. if (l>line_length) or
  690. (tokens>max_tokens) or
  691. (hp.next=nil) or
  692. (tai(hp.next).typ<>ait_const) or
  693. (tai_const(hp.next).consttype<>consttype) then
  694. break;
  695. hp:=tai(hp.next);
  696. writer.AsmWrite(',');
  697. until false;
  698. { Substract section start for secrel32 type }
  699. {if consttype=aitconst_secrel32_symbol then
  700. writer.AsmWrite(' - $$');}
  701. writer.AsmLn;
  702. end;
  703. {aitconst_128bit,}
  704. aitconst_16bit,
  705. aitconst_8bit,
  706. aitconst_16bit_unaligned{,
  707. aitconst_rva_symbol,
  708. aitconst_secrel32_symbol} :
  709. begin
  710. writer.AsmWrite(ait_const2str[consttype]);
  711. l:=0;
  712. tokens:=1;
  713. repeat
  714. if assigned(tai_const(hp).sym) then
  715. begin
  716. if assigned(tai_const(hp).endsym) then
  717. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  718. else
  719. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  720. if tai_const(hp).value<>0 then
  721. s:=s+tostr_with_plus(tai_const(hp).value);
  722. end
  723. else
  724. s:=tostr(tai_const(hp).value);
  725. writer.AsmWrite(s);
  726. inc(l,length(s));
  727. inc(tokens);
  728. if (l>line_length) or
  729. (tokens>max_tokens) or
  730. (hp.next=nil) or
  731. (tai(hp.next).typ<>ait_const) or
  732. (tai_const(hp.next).consttype<>consttype) then
  733. break;
  734. hp:=tai(hp.next);
  735. writer.AsmWrite(',');
  736. until false;
  737. { Substract section start for secrel32 type }
  738. if consttype=aitconst_secrel32_symbol then
  739. writer.AsmWrite(' - $$');
  740. writer.AsmLn;
  741. end;
  742. else
  743. begin
  744. writer.AsmWrite(asminfo^.comment);
  745. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  746. Str(consttype,s);
  747. writer.AsmWriteLn(s);
  748. end;
  749. end;
  750. end;
  751. ait_string :
  752. begin
  753. pos:=0;
  754. for i:=1 to tai_string(hp).len do
  755. begin
  756. if pos=0 then
  757. begin
  758. writer.AsmWrite(#9'.ascii'#9'"');
  759. pos:=20;
  760. end;
  761. ch:=tai_string(hp).str[i-1];
  762. case ch of
  763. #0, {This can't be done by range, because a bug in FPC}
  764. #1..#31,
  765. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  766. '"' : s:='\"';
  767. '\' : s:='\\';
  768. else
  769. s:=ch;
  770. end;
  771. writer.AsmWrite(s);
  772. inc(pos,length(s));
  773. if (pos>line_length) or (i=tai_string(hp).len) then
  774. begin
  775. writer.AsmWriteLn('"');
  776. pos:=0;
  777. end;
  778. end;
  779. end;
  780. ait_instruction :
  781. begin
  782. WriteInstruction(taicpu(hp));
  783. end;
  784. ait_directive :
  785. begin
  786. case tai_directive(hp).directive of
  787. asd_cpu :
  788. writer.AsmWriteLn('; CPU '+tai_directive(hp).name);
  789. else
  790. begin
  791. writer.AsmWrite(asminfo^.comment);
  792. writer.AsmWrite('WARNING: not yet implemented in assembler output: ait_directive.');
  793. Str(tai_directive(hp).directive,s);
  794. writer.AsmWriteLn(s);
  795. end;
  796. end;
  797. end;
  798. ait_cutobject :
  799. begin
  800. if SmartAsm then
  801. begin
  802. { only reset buffer if nothing has changed }
  803. if not writer.ClearIfEmpty then
  804. begin
  805. {if SmartAsm then
  806. begin
  807. WriteSmartExternals;
  808. FreeExternChainList;
  809. end;
  810. WriteGroups;}
  811. writer.AsmClose;
  812. DoAssemble;
  813. writer.AsmCreate(tai_cutobject(hp).place);
  814. {ResetSectionsList;
  815. WriteHeader;}
  816. end;
  817. { avoid empty files }
  818. LastSecType:=sec_none;
  819. LastSecName:='';
  820. LastSecOrder:=secorder_default;
  821. LastAlign:=1;
  822. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  823. begin
  824. if tai(hp.next).typ=ait_section then
  825. begin
  826. LastSecType:=tai_section(hp.next).sectype;
  827. LastSecName:=tai_section(hp.next).name^;
  828. LastSecOrder:=tai_section(hp.next).secorder;
  829. LastAlign:=tai_section(hp.next).secalign;
  830. end;
  831. hp:=tai(hp.next);
  832. end;
  833. if LastSecType<>sec_none then
  834. WriteSection(LastSecType,LastSecName,LastSecOrder,LastAlign);
  835. writer.MarkEmpty;
  836. //NewObject:=true;
  837. end;
  838. end;
  839. ait_marker :
  840. if tai_marker(hp).kind=mark_NoLineInfoStart then
  841. inc(InlineLevel)
  842. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  843. dec(InlineLevel);
  844. ait_stab,
  845. ait_force_line,
  846. ait_function_name : ;
  847. else
  848. if not WriteComments(hp) then
  849. begin
  850. writer.AsmWrite(asminfo^.comment);
  851. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  852. Str(hp.typ,s);
  853. writer.AsmWriteLn(s);
  854. end;
  855. end;
  856. lasthp:=hp;
  857. hp:=tai(hp.next);
  858. end;
  859. end;
  860. procedure TZ80vasm.WriteAsmList;
  861. var
  862. hal: TAsmListType;
  863. begin
  864. WriteExternals;
  865. for hal:=low(TasmlistType) to high(TasmlistType) do
  866. begin
  867. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  868. writetree(current_asmdata.asmlists[hal]);
  869. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  870. end;
  871. end;
  872. function TZ80vasm.MakeCmdLine: TCmdStr;
  873. begin
  874. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  875. end;
  876. const
  877. as_z80_vasm_info : tasminfo =
  878. (
  879. id : as_z80_vasm;
  880. idtxt : 'VASM';
  881. asmbin : 'vasmz80_std';
  882. asmcmd : '-quiet -Fvobj -o $OBJ $EXTRAOPT $ASM';
  883. supported_targets : [system_z80_embedded, system_z80_zxspectrum, system_z80_msxdos];
  884. flags : [af_needar,af_smartlink_sections];
  885. labelprefix : '.L';
  886. labelmaxlen : -1;
  887. comment : '; ';
  888. dollarsign: '$';
  889. );
  890. begin
  891. RegisterAssembler(as_z80_vasm_info,TZ80vasm);
  892. end.