agz80vasm.pas 33 KB

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