agsdasz80.pas 34 KB

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