agsdasz80.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. );
  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, sb, 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. ResetSourceLines;
  533. if tai_section(hp).sectype<>sec_none then
  534. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  535. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  536. else
  537. begin
  538. {$ifdef EXTDEBUG}
  539. writer.AsmWrite(asminfo^.comment);
  540. writer.AsmWriteln(' sec_none');
  541. {$endif EXTDEBUG}
  542. end;
  543. end;
  544. ait_align :
  545. begin
  546. 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);
  547. end;
  548. ait_label :
  549. begin
  550. if tai_label(hp).labsym.is_used then
  551. begin
  552. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
  553. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  554. writer.AsmWriteLn('::')
  555. else
  556. writer.AsmWriteLn(':');
  557. end;
  558. end;
  559. ait_symbol :
  560. begin
  561. if not(tai_symbol(hp).has_value) then
  562. begin
  563. if tai_symbol(hp).is_global then
  564. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '::')
  565. else
  566. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + ':');
  567. end
  568. else
  569. begin
  570. if tai_symbol(hp).is_global then
  571. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '==' + tostr(tai_symbol(hp).value))
  572. else
  573. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '=' + tostr(tai_symbol(hp).value));
  574. end;
  575. end;
  576. ait_symbol_end :
  577. begin
  578. end;
  579. ait_datablock :
  580. begin
  581. if tai_datablock(hp).is_global or SmartAsm then
  582. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name) + '::')
  583. else
  584. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name) + ':');
  585. {if SmartAsm then
  586. AddSymbol(tai_datablock(hp).sym.name,true);}
  587. writer.AsmWriteLn(#9'.rs'#9+tostr(tai_datablock(hp).size));
  588. end;
  589. ait_realconst:
  590. WriteRealConstAsBytes(tai_realconst(hp),#9'.db'#9,do_line);
  591. ait_const:
  592. begin
  593. consttype:=tai_const(hp).consttype;
  594. case consttype of
  595. aitconst_uleb128bit:
  596. writer.AsmWriteLn(ait_const2str[aitconst_8bit]+uleb128tostr(qword(tai_const(hp).value)));
  597. aitconst_sleb128bit:
  598. writer.AsmWriteLn(ait_const2str[aitconst_8bit]+sleb128tostr(tai_const(hp).value));
  599. aitconst_64bit,
  600. aitconst_64bit_unaligned,
  601. aitconst_32bit,
  602. aitconst_32bit_unaligned:
  603. begin
  604. writer.AsmWrite(#9'.dw'#9);
  605. l:=0;
  606. tokens:=1;
  607. repeat
  608. if assigned(tai_const(hp).sym) then
  609. begin
  610. if assigned(tai_const(hp).endsym) then
  611. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  612. else
  613. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  614. if tai_const(hp).value<>0 then
  615. s:=s+tostr_with_plus(tai_const(hp).value);
  616. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  617. s:=s+',0,0,0'
  618. else
  619. s:=s+',0';
  620. end
  621. else
  622. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  623. s:=tostr(Word(tai_const(hp).value)) +','+tostr(Word(tai_const(hp).value shr 16))+','+
  624. tostr(Word(tai_const(hp).value shr 32))+','+tostr(Word(tai_const(hp).value shr 48))
  625. else
  626. s:=tostr(Word(tai_const(hp).value))+','+tostr(Word(tai_const(hp).value shr 16));
  627. writer.AsmWrite(s);
  628. inc(l,length(s));
  629. inc(tokens);
  630. if (l>line_length) or
  631. (tokens>max_tokens) or
  632. (hp.next=nil) or
  633. (tai(hp.next).typ<>ait_const) or
  634. (tai_const(hp.next).consttype<>consttype) then
  635. break;
  636. hp:=tai(hp.next);
  637. writer.AsmWrite(',');
  638. until false;
  639. { Substract section start for secrel32 type }
  640. {if consttype=aitconst_secrel32_symbol then
  641. writer.AsmWrite(' - $$');}
  642. writer.AsmLn;
  643. end;
  644. {aitconst_128bit,}
  645. aitconst_16bit,
  646. aitconst_8bit,
  647. aitconst_16bit_unaligned{,
  648. aitconst_rva_symbol,
  649. aitconst_secrel32_symbol} :
  650. begin
  651. writer.AsmWrite(ait_const2str[consttype]);
  652. l:=0;
  653. tokens:=1;
  654. repeat
  655. if assigned(tai_const(hp).sym) then
  656. begin
  657. if assigned(tai_const(hp).endsym) then
  658. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  659. else
  660. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  661. if tai_const(hp).value<>0 then
  662. s:=s+tostr_with_plus(tai_const(hp).value);
  663. end
  664. else
  665. s:=tostr(tai_const(hp).value);
  666. writer.AsmWrite(s);
  667. inc(l,length(s));
  668. inc(tokens);
  669. if (l>line_length) or
  670. (tokens>max_tokens) or
  671. (hp.next=nil) or
  672. (tai(hp.next).typ<>ait_const) or
  673. (tai_const(hp.next).consttype<>consttype) then
  674. break;
  675. hp:=tai(hp.next);
  676. writer.AsmWrite(',');
  677. until false;
  678. { Substract section start for secrel32 type }
  679. if consttype=aitconst_secrel32_symbol then
  680. writer.AsmWrite(' - $$');
  681. writer.AsmLn;
  682. end;
  683. else
  684. begin
  685. writer.AsmWrite(asminfo^.comment);
  686. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  687. Str(consttype,s);
  688. writer.AsmWriteLn(s);
  689. end;
  690. end;
  691. end;
  692. ait_string :
  693. begin
  694. pos:=0;
  695. s:='';
  696. sb:='';
  697. for i:=1 to tai_string(hp).len do
  698. begin
  699. ch:=tai_string(hp).str[i-1];
  700. if ch in [#32..chr(ord('"')-1),char(ord('"')+1)..#127] then
  701. begin
  702. if sb<>'' then
  703. begin
  704. writer.AsmWriteln(sb);
  705. sb:='';
  706. pos:=0;
  707. end;
  708. if pos=0 then
  709. begin
  710. s:=#9'.ascii'#9'"'+ch;
  711. pos:=20;
  712. end
  713. else
  714. begin
  715. s:=s+ch;
  716. inc(pos);
  717. end;
  718. end
  719. else
  720. begin
  721. if s<>'' then
  722. begin
  723. writer.AsmWriteln(s+'"');
  724. s:='';
  725. pos:=0;
  726. end;
  727. if pos=0 then
  728. begin
  729. sb:=#9'.byte'#9+tostr(ord(ch));
  730. pos:=15;
  731. end
  732. else
  733. begin
  734. sb:=sb+','+tostr(ord(ch));
  735. inc(pos,4);
  736. end;
  737. end;
  738. if (pos>line_length) or (i=tai_string(hp).len) then
  739. begin
  740. if s<>'' then
  741. begin
  742. writer.AsmWriteLn(s+'"');
  743. s:='';
  744. end
  745. else if sb<>'' then
  746. begin
  747. writer.AsmWriteLn(sb);
  748. sb:='';
  749. end;
  750. pos:=0;
  751. end;
  752. end;
  753. end;
  754. ait_instruction :
  755. begin
  756. WriteInstruction(taicpu(hp));
  757. end;
  758. ait_directive :
  759. begin
  760. case tai_directive(hp).directive of
  761. asd_cpu :
  762. writer.AsmWriteLn('; CPU '+tai_directive(hp).name);
  763. else
  764. begin
  765. writer.AsmWrite(asminfo^.comment);
  766. writer.AsmWrite('WARNING: not yet implemented in assembler output: ait_directive.');
  767. Str(tai_directive(hp).directive,s);
  768. writer.AsmWriteLn(s);
  769. end;
  770. end;
  771. end;
  772. ait_cutobject :
  773. begin
  774. if SmartAsm then
  775. begin
  776. { only reset buffer if nothing has changed }
  777. if not writer.ClearIfEmpty then
  778. begin
  779. {if SmartAsm then
  780. begin
  781. WriteSmartExternals;
  782. FreeExternChainList;
  783. end;
  784. WriteGroups;}
  785. writer.AsmClose;
  786. DoAssemble;
  787. writer.AsmCreate(tai_cutobject(hp).place);
  788. {ResetSectionsList;
  789. WriteHeader;}
  790. end;
  791. { avoid empty files }
  792. LastSecType:=sec_none;
  793. LastSecName:='';
  794. LastSecOrder:=secorder_default;
  795. LastAlign:=1;
  796. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  797. begin
  798. if tai(hp.next).typ=ait_section then
  799. begin
  800. LastSecType:=tai_section(hp.next).sectype;
  801. LastSecName:=tai_section(hp.next).name^;
  802. LastSecOrder:=tai_section(hp.next).secorder;
  803. LastAlign:=tai_section(hp.next).secalign;
  804. end;
  805. hp:=tai(hp.next);
  806. end;
  807. if LastSecType<>sec_none then
  808. WriteSection(LastSecType,LastSecName,LastSecOrder,LastAlign);
  809. writer.MarkEmpty;
  810. //NewObject:=true;
  811. end;
  812. end;
  813. ait_marker :
  814. if tai_marker(hp).kind=mark_NoLineInfoStart then
  815. inc(InlineLevel)
  816. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  817. dec(InlineLevel);
  818. ait_stab,
  819. ait_force_line,
  820. ait_function_name : ;
  821. else
  822. if not WriteComments(hp) then
  823. begin
  824. writer.AsmWrite(asminfo^.comment);
  825. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  826. Str(hp.typ,s);
  827. writer.AsmWriteLn(s);
  828. end;
  829. end;
  830. lasthp:=hp;
  831. hp:=tai(hp.next);
  832. end;
  833. end;
  834. procedure TSdccSdasZ80Assembler.WriteAsmList;
  835. var
  836. hal: TAsmListType;
  837. begin
  838. WriteExternals;
  839. for hal:=low(TasmlistType) to high(TasmlistType) do
  840. begin
  841. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  842. writetree(current_asmdata.asmlists[hal]);
  843. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  844. end;
  845. end;
  846. function TSdccSdasZ80Assembler.MakeCmdLine: TCmdStr;
  847. begin
  848. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  849. end;
  850. const
  851. as_sdcc_sdasZ80_asm_info : tasminfo =
  852. (
  853. id : as_sdcc_sdasz80;
  854. idtxt : 'SDCC-SDASZ80';
  855. asmbin : 'sdasz80';
  856. asmcmd : '-g -o $EXTRAOPT $OBJ $ASM';
  857. supported_targets : [system_Z80_embedded,system_z80_zxspectrum,system_z80_msxdos];
  858. flags : [af_needar];
  859. labelprefix : '.L';
  860. labelmaxlen : 79;
  861. comment : '; ';
  862. dollarsign: '$';
  863. );
  864. begin
  865. RegisterAssembler(as_sdcc_sdasZ80_asm_info,TSdccSdasZ80Assembler);
  866. end.