agsdasz80.pas 33 KB

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