agsdasz80.pas 34 KB

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