agsdasz80.pas 34 KB

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