agsdasz80.pas 32 KB

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