agsdasz80.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  35. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;
  36. secflags:TSectionFlags=[];secprogbits:TSectionProgbits=SPB_None);
  37. procedure WriteInstruction(hp: taicpu);
  38. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  39. procedure WriteOper_jmp(const o:toper; ai : taicpu);
  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. finput;
  51. const
  52. line_length = 70;
  53. max_tokens : longint = 25;
  54. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  55. #9''#9,#9'FIXMEDQ'#9,#9'FIXMEDD'#9,#9'.dw'#9,#9'.db'#9,
  56. #9'FIXMESLEB',#9'FIXEMEULEB',
  57. #9'FIXMEDD RVA'#9,#9'FIXMEDD SECREL32'#9,
  58. #9'FIXME',#9'FIXME',#9'FIXME',#9'FIXME',
  59. #9'.dw'#9,#9'FIXMEDD'#9,#9'FIXMEDQ'#9
  60. );
  61. procedure TSdccSdasZ80Assembler.WriteDecodedSleb128(a: int64);
  62. var
  63. i,len : longint;
  64. buf : array[0..255] of byte;
  65. begin
  66. writer.AsmWrite(#9'.db'#9);
  67. len:=EncodeSleb128(a,buf,0);
  68. for i:=0 to len-1 do
  69. begin
  70. if (i > 0) then
  71. writer.AsmWrite(',');
  72. writer.AsmWrite(tostr(buf[i]));
  73. end;
  74. writer.AsmWriteLn(#9'; sleb '+tostr(a));
  75. end;
  76. procedure TSdccSdasZ80Assembler.WriteDecodedUleb128(a: qword);
  77. var
  78. i,len : longint;
  79. buf : array[0..63] of byte;
  80. begin
  81. writer.AsmWrite(#9'.db'#9);
  82. len:=EncodeUleb128(a,buf,0);
  83. for i:=0 to len-1 do
  84. begin
  85. if (i > 0) then
  86. writer.AsmWrite(',');
  87. writer.AsmWrite(tostr(buf[i]));
  88. end;
  89. writer.AsmWriteLn(#9'; uleb '+tostr(a));
  90. end;
  91. function TSdccSdasZ80Assembler.sectionname(atype: TAsmSectiontype;
  92. const aname: string; aorder: TAsmSectionOrder): string;
  93. const
  94. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  95. '_CODE',
  96. '_DATA',
  97. '_DATA',
  98. '.rodata',
  99. '.bss',
  100. '.threadvar',
  101. '.pdata',
  102. '', { stubs }
  103. '__DATA,__nl_symbol_ptr',
  104. '__DATA,__la_symbol_ptr',
  105. '__DATA,__mod_init_func',
  106. '__DATA,__mod_term_func',
  107. '.stab',
  108. '.stabstr',
  109. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  110. '.eh_frame',
  111. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  112. '.fpc',
  113. '.toc',
  114. '.init',
  115. '.fini',
  116. '.objc_class',
  117. '.objc_meta_class',
  118. '.objc_cat_cls_meth',
  119. '.objc_cat_inst_meth',
  120. '.objc_protocol',
  121. '.objc_string_object',
  122. '.objc_cls_meth',
  123. '.objc_inst_meth',
  124. '.objc_cls_refs',
  125. '.objc_message_refs',
  126. '.objc_symbols',
  127. '.objc_category',
  128. '.objc_class_vars',
  129. '.objc_instance_vars',
  130. '.objc_module_info',
  131. '.objc_class_names',
  132. '.objc_meth_var_types',
  133. '.objc_meth_var_names',
  134. '.objc_selector_strs',
  135. '.objc_protocol_ext',
  136. '.objc_class_ext',
  137. '.objc_property',
  138. '.objc_image_info',
  139. '.objc_cstring_object',
  140. '.objc_sel_fixup',
  141. '__DATA,__objc_data',
  142. '__DATA,__objc_const',
  143. '.objc_superrefs',
  144. '__DATA, __datacoal_nt,coalesced',
  145. '.objc_classlist',
  146. '.objc_nlclasslist',
  147. '.objc_catlist',
  148. '.obcj_nlcatlist',
  149. '.objc_protolist',
  150. '.stack',
  151. '.heap',
  152. '.gcc_except_table',
  153. '.ARM.attributes'
  154. );
  155. begin
  156. result:=secnames[atype];
  157. end;
  158. procedure TSdccSdasZ80Assembler.WriteSection(atype: TAsmSectiontype;
  159. const aname: string; aorder: TAsmSectionOrder; secalign: longint;
  160. secflags: TSectionFlags; secprogbits: TSectionProgbits);
  161. var
  162. s : string;
  163. secflag: TSectionFlag;
  164. sectionprogbits,
  165. sectionflags: boolean;
  166. begin
  167. writer.AsmLn;
  168. sectionflags:=false;
  169. sectionprogbits:=false;
  170. writer.AsmWrite(#9'.area ');
  171. { sectionname may rename those sections, so we do not write flags/progbits for them,
  172. the assembler will ignore them/spite out a warning anyways }
  173. if not(atype in [sec_data,sec_rodata,sec_rodata_norel]) then
  174. begin
  175. sectionflags:=true;
  176. sectionprogbits:=true;
  177. end;
  178. s:=sectionname(atype,aname,aorder);
  179. writer.AsmWrite(s);
  180. { flags explicitly defined? }
  181. (*if (sectionflags or sectionprogbits) and
  182. ((secflags<>[]) or
  183. (secprogbits<>SPB_None)) then
  184. begin
  185. if sectionflags then
  186. begin
  187. s:=',"';
  188. for secflag in secflags do
  189. case secflag of
  190. SF_A:
  191. s:=s+'a';
  192. SF_W:
  193. s:=s+'w';
  194. SF_X:
  195. s:=s+'x';
  196. end;
  197. writer.AsmWrite(s+'"');
  198. end;
  199. if sectionprogbits then
  200. begin
  201. case secprogbits of
  202. SPB_PROGBITS:
  203. writer.AsmWrite(',%progbits');
  204. SPB_NOBITS:
  205. writer.AsmWrite(',%nobits');
  206. SPB_NOTE:
  207. writer.AsmWrite(',%note');
  208. SPB_None:
  209. ;
  210. else
  211. InternalError(2019100801);
  212. end;
  213. end;
  214. end
  215. else
  216. case atype of
  217. sec_fpc :
  218. if aname = 'resptrs' then
  219. writer.AsmWrite(', "a", @progbits');
  220. sec_stub :
  221. begin
  222. case target_info.system of
  223. { there are processor-independent shortcuts available }
  224. { for this, namely .symbol_stub and .picsymbol_stub, but }
  225. { they don't work and gcc doesn't use them either... }
  226. system_powerpc_darwin,
  227. system_powerpc64_darwin:
  228. if (cs_create_pic in current_settings.moduleswitches) then
  229. writer.AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  230. else
  231. writer.AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  232. system_i386_darwin,
  233. system_i386_iphonesim:
  234. writer.AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  235. system_arm_darwin:
  236. if (cs_create_pic in current_settings.moduleswitches) then
  237. writer.AsmWriteln('__TEXT,__picsymbolstub4,symbol_stubs,none,16')
  238. else
  239. writer.AsmWriteln('__TEXT,__symbol_stub4,symbol_stubs,none,12')
  240. { darwin/(x86-64/AArch64) uses PC-based GOT addressing, no
  241. explicit symbol stubs }
  242. else
  243. internalerror(2006031101);
  244. end;
  245. end;
  246. else
  247. { GNU AS won't recognize '.text.n_something' section name as belonging
  248. to '.text' and assigns default attributes to it, which is not
  249. always correct. We have to fix it.
  250. TODO: This likely applies to all systems which smartlink without
  251. creating libraries }
  252. begin
  253. if is_smart_section(atype) and (aname<>'') then
  254. begin
  255. s:=sectionattrs(atype);
  256. if (s<>'') then
  257. writer.AsmWrite(',"'+s+'"');
  258. end;
  259. if target_info.system in systems_aix then
  260. begin
  261. s:=sectionalignment_aix(atype,secalign);
  262. if s<>'' then
  263. writer.AsmWrite(','+s);
  264. end;
  265. end;
  266. end;*)
  267. writer.AsmLn;
  268. LastSecType:=atype;
  269. end;
  270. procedure TSdccSdasZ80Assembler.WriteInstruction(hp: taicpu);
  271. var
  272. i: Integer;
  273. begin
  274. writer.AsmWrite(#9#9+std_op2str[hp.opcode]);
  275. if taicpu(hp).ops<>0 then
  276. begin
  277. for i:=0 to taicpu(hp).ops-1 do
  278. begin
  279. if i=0 then
  280. writer.AsmWrite(#9)
  281. else
  282. writer.AsmWrite(',');
  283. if hp.is_jmp then
  284. WriteOper_jmp(taicpu(hp).oper[i]^,hp)
  285. else
  286. WriteOper(taicpu(hp).oper[i]^,S_B{taicpu(hp).opsize},hp.opcode,taicpu(hp).ops,(i=2));
  287. end;
  288. end;
  289. writer.AsmLn;
  290. end;
  291. procedure TSdccSdasZ80Assembler.WriteOper(const o: toper; s: topsize;
  292. opcode: tasmop; ops: longint; dest: boolean);
  293. var
  294. need_plus: Boolean;
  295. begin
  296. case o.typ of
  297. top_reg :
  298. writer.AsmWrite(std_regname(o.reg));
  299. top_const :
  300. begin
  301. { if (ops=1) and (opcode<>A_RET) then
  302. writer.AsmWrite(sizestr(s,dest));}
  303. writer.AsmWrite('#'+tostr(longint(o.val)));
  304. end;
  305. top_ref:
  306. begin
  307. if not assigned(o.ref^.symbol) and
  308. ((o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO)) and
  309. (o.ref^.offset<>0) then
  310. begin
  311. writer.AsmWrite(tostr(o.ref^.offset));
  312. writer.AsmWrite(' (');
  313. if o.ref^.base<>NR_NO then
  314. begin
  315. if o.ref^.index<>NR_NO then
  316. internalerror(2020040201);
  317. writer.AsmWrite(std_regname(o.ref^.base));
  318. end
  319. else if o.ref^.index<>NR_NO then
  320. begin
  321. if o.ref^.scalefactor>1 then
  322. internalerror(2020040202);
  323. writer.AsmWrite(std_regname(o.ref^.index));
  324. end;
  325. writer.AsmWrite(')');
  326. end
  327. else
  328. begin
  329. writer.AsmWrite('(');
  330. need_plus:=false;
  331. if o.ref^.base<>NR_NO then
  332. begin
  333. if o.ref^.index<>NR_NO then
  334. internalerror(2020040201);
  335. writer.AsmWrite(std_regname(o.ref^.base));
  336. need_plus:=true;
  337. end
  338. else if o.ref^.index<>NR_NO then
  339. begin
  340. if o.ref^.scalefactor>1 then
  341. internalerror(2020040202);
  342. writer.AsmWrite(std_regname(o.ref^.index));
  343. need_plus:=true;
  344. end;
  345. if assigned(o.ref^.symbol) then
  346. begin
  347. {if SmartAsm then
  348. AddSymbol(o.ref^.symbol.name,false);}
  349. if need_plus then
  350. writer.AsmWrite('+');
  351. writer.AsmWrite(o.ref^.symbol.name);
  352. need_plus:=true;
  353. end;
  354. if o.ref^.offset<>0 then
  355. begin
  356. if need_plus and (o.ref^.offset>0) then
  357. writer.AsmWrite('+');
  358. writer.AsmWrite(tostr(o.ref^.offset));
  359. need_plus:=true;
  360. end;
  361. if not need_plus then
  362. writer.AsmWrite('0');
  363. writer.AsmWrite(')');
  364. end;
  365. end;
  366. else
  367. internalerror(10001);
  368. end;
  369. end;
  370. procedure TSdccSdasZ80Assembler.WriteOper_jmp(const o: toper; ai: taicpu);
  371. begin
  372. case o.typ of
  373. top_reg :
  374. writer.AsmWrite(std_regname(o.reg));
  375. top_const :
  376. begin
  377. writer.AsmWrite('#'+tostr(longint(o.val)));
  378. end;
  379. top_ref:
  380. begin
  381. if o.ref^.refaddr=addr_no then
  382. begin
  383. writer.AsmWrite('TODO:indirect jump ref');
  384. //WriteReference(o.ref^);
  385. end
  386. else
  387. begin
  388. writer.AsmWrite(o.ref^.symbol.name);
  389. //if SmartAsm then
  390. // AddSymbol(o.ref^.symbol.name,false);
  391. if o.ref^.offset>0 then
  392. writer.AsmWrite('+'+tostr(o.ref^.offset))
  393. else
  394. if o.ref^.offset<0 then
  395. writer.AsmWrite(tostr(o.ref^.offset));
  396. end;
  397. end;
  398. else
  399. internalerror(10001);
  400. end;
  401. end;
  402. procedure TSdccSdasZ80Assembler.WriteTree(p: TAsmList);
  403. function getreferencestring(var ref : treference) : string;
  404. var
  405. s : string;
  406. begin
  407. s:='';
  408. with ref do
  409. begin
  410. {$ifdef extdebug}
  411. // if base=NR_NO then
  412. // internalerror(200308292);
  413. // if ((index<>NR_NO) or (shiftmode<>SM_None)) and ((offset<>0) or (symbol<>nil)) then
  414. // internalerror(200308293);
  415. {$endif extdebug}
  416. if index<>NR_NO then
  417. internalerror(2011021701)
  418. else if base<>NR_NO then
  419. begin
  420. // if addressmode=AM_PREDRECEMENT then
  421. // s:='-';
  422. //case base of
  423. // NR_R26:
  424. // s:=s+'X';
  425. // NR_R28:
  426. // s:=s+'Y';
  427. // NR_R30:
  428. // s:=s+'Z';
  429. // else
  430. // s:=gas_regname(base);
  431. //end;
  432. //if addressmode=AM_POSTINCREMENT then
  433. // s:=s+'+';
  434. //
  435. //if offset>0 then
  436. // s:=s+'+'+tostr(offset)
  437. //else if offset<0 then
  438. // s:=s+tostr(offset)
  439. end
  440. else if assigned(symbol) or (offset<>0) then
  441. begin
  442. //if assigned(symbol) then
  443. // s:=ReplaceForbiddenAsmSymbolChars(symbol.name);
  444. //
  445. //if offset<0 then
  446. // s:=s+tostr(offset)
  447. //else if offset>0 then
  448. // s:=s+'+'+tostr(offset);
  449. //case refaddr of
  450. // addr_hi8:
  451. // s:='hi8('+s+')';
  452. // addr_hi8_gs:
  453. // s:='hi8(gs('+s+'))';
  454. // addr_lo8:
  455. // s:='lo8('+s+')';
  456. // addr_lo8_gs:
  457. // s:='lo8(gs('+s+'))';
  458. // else
  459. // s:='('+s+')';
  460. //end;
  461. end;
  462. end;
  463. getreferencestring:=s;
  464. end;
  465. function getopstr(const o:toper) : string;
  466. var
  467. hs : string;
  468. first : boolean;
  469. r : tsuperregister;
  470. begin
  471. //case o.typ of
  472. // top_reg:
  473. // getopstr:=gas_regname(o.reg);
  474. // top_const:
  475. // getopstr:=tostr(longint(o.val));
  476. // top_ref:
  477. // if o.ref^.refaddr=addr_full then
  478. // begin
  479. // hs:=ReplaceForbiddenAsmSymbolChars(o.ref^.symbol.name);
  480. // if o.ref^.offset>0 then
  481. // hs:=hs+'+'+tostr(o.ref^.offset)
  482. // else
  483. // if o.ref^.offset<0 then
  484. // hs:=hs+tostr(o.ref^.offset);
  485. // getopstr:=hs;
  486. // end
  487. // else
  488. // getopstr:=getreferencestring(o.ref^);
  489. // else
  490. // internalerror(2002070604);
  491. //end;
  492. end;
  493. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  494. var
  495. i: longint;
  496. alignment64 : int64;
  497. begin
  498. last_align:=alignment;
  499. if alignment>1 then
  500. writer.AsmWriteLn(#9'.bndry '+tostr(alignment));
  501. end;
  502. //var op: TAsmOp;
  503. // s: string;
  504. // i: byte;
  505. // sep: string[3];
  506. var
  507. lasthp,
  508. hp: tai;
  509. s: string;
  510. counter,lines,i,j,l,tokens,pos,last_align: longint;
  511. quoted, do_line: Boolean;
  512. consttype: taiconst_type;
  513. ch: Char;
  514. InlineLevel : longint;
  515. prevfileinfo : tfileposinfo;
  516. previnfile : tinputfile;
  517. begin
  518. if not assigned(p) then
  519. exit;
  520. InlineLevel:=0;
  521. last_align:=1;
  522. lasthp:=nil;
  523. { lineinfo is only needed for al_procedures (PFV) }
  524. do_line:=(cs_asm_source in current_settings.globalswitches) or
  525. ((cs_lineinfo in current_settings.moduleswitches)
  526. and (p=current_asmdata.asmlists[al_procedures]));
  527. hp:=tai(p.first);
  528. while assigned(hp) do
  529. begin
  530. prefetch(pointer(hp.next)^);
  531. if not(hp.typ in SkipLineInfo) then
  532. begin
  533. previnfile:=lastinfile;
  534. prevfileinfo:=lastfileinfo;
  535. current_filepos:=tailineinfo(hp).fileinfo;
  536. { no line info for inlined code }
  537. if do_line and (inlinelevel=0) then
  538. WriteSourceLine(hp as tailineinfo);
  539. (*if (lastfileinfo.line<>prevfileinfo.line) or
  540. (previnfile<>lastinfile) then
  541. begin
  542. { +0 postfix means no line increment per assembler instruction }
  543. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  544. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  545. writer.AsmWriteLn(' '+lastinfile.name)
  546. else
  547. writer.AsmLn;
  548. NewObject:=false;
  549. end;*)
  550. end;
  551. case hp.typ of
  552. ait_comment :
  553. begin
  554. writer.AsmWrite(asminfo^.comment);
  555. writer.AsmWritePChar(tai_comment(hp).str);
  556. writer.AsmLn;
  557. end;
  558. ait_regalloc :
  559. begin
  560. if (cs_asm_regalloc in current_settings.globalswitches) then
  561. writer.AsmWriteLn(#9#9+asminfo^.comment+'Register '+std_regname(tai_regalloc(hp).reg)+' '+
  562. regallocstr[tai_regalloc(hp).ratype]);
  563. end;
  564. ait_tempalloc :
  565. begin
  566. if (cs_asm_tempalloc in current_settings.globalswitches) then
  567. WriteTempalloc(tai_tempalloc(hp));
  568. end;
  569. ait_section :
  570. begin
  571. if tai_section(hp).sectype<>sec_none then
  572. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  573. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  574. else
  575. begin
  576. {$ifdef EXTDEBUG}
  577. writer.AsmWrite(asminfo^.comment);
  578. writer.AsmWriteln(' sec_none');
  579. {$endif EXTDEBUG}
  580. end;
  581. end;
  582. ait_align :
  583. begin
  584. 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);
  585. end;
  586. ait_label :
  587. begin
  588. if tai_label(hp).labsym.is_used then
  589. begin
  590. writer.AsmWrite(tai_label(hp).labsym.name);
  591. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  592. writer.AsmWriteLn('::')
  593. else
  594. writer.AsmWriteLn(':');
  595. end;
  596. end;
  597. ait_symbol :
  598. begin
  599. if not(tai_symbol(hp).has_value) then
  600. begin
  601. if tai_symbol(hp).is_global then
  602. writer.AsmWriteLn(tai_symbol(hp).sym.name + '::')
  603. else
  604. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  605. end
  606. else
  607. begin
  608. if tai_symbol(hp).is_global then
  609. writer.AsmWriteLn(tai_symbol(hp).sym.name + '==' + tostr(tai_symbol(hp).value))
  610. else
  611. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  612. end;
  613. end;
  614. ait_symbol_end :
  615. begin
  616. end;
  617. ait_datablock :
  618. begin
  619. if tai_datablock(hp).is_global or SmartAsm then
  620. writer.AsmWrite(tai_datablock(hp).sym.name + '::')
  621. else
  622. writer.AsmWrite(tai_datablock(hp).sym.name + ':');
  623. {if SmartAsm then
  624. AddSymbol(tai_datablock(hp).sym.name,true);}
  625. writer.AsmWriteLn(#9'.rs'#9+tostr(tai_datablock(hp).size));
  626. end;
  627. ait_const:
  628. begin
  629. consttype:=tai_const(hp).consttype;
  630. case consttype of
  631. aitconst_uleb128bit:
  632. WriteDecodedUleb128(qword(tai_const(hp).value));
  633. aitconst_sleb128bit:
  634. WriteDecodedSleb128(int64(tai_const(hp).value));
  635. aitconst_64bit,
  636. aitconst_64bit_unaligned,
  637. aitconst_32bit,
  638. aitconst_32bit_unaligned:
  639. begin
  640. writer.AsmWrite(#9'.dw'#9);
  641. l:=0;
  642. tokens:=1;
  643. repeat
  644. if assigned(tai_const(hp).sym) then
  645. begin
  646. if assigned(tai_const(hp).endsym) then
  647. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  648. else
  649. s:=tai_const(hp).sym.name;
  650. if tai_const(hp).value<>0 then
  651. s:=s+tostr_with_plus(tai_const(hp).value);
  652. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  653. s:=s+',0,0,0'
  654. else
  655. s:=s+',0';
  656. end
  657. else
  658. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  659. s:=tostr(Word(tai_const(hp).value)) +','+tostr(Word(tai_const(hp).value shr 16))+','+
  660. tostr(Word(tai_const(hp).value shr 32))+','+tostr(Word(tai_const(hp).value shr 48))
  661. else
  662. s:=tostr(Word(tai_const(hp).value))+','+tostr(Word(tai_const(hp).value shr 16));
  663. writer.AsmWrite(s);
  664. inc(l,length(s));
  665. inc(tokens);
  666. if (l>line_length) or
  667. (tokens>max_tokens) or
  668. (hp.next=nil) or
  669. (tai(hp.next).typ<>ait_const) or
  670. (tai_const(hp.next).consttype<>consttype) then
  671. break;
  672. hp:=tai(hp.next);
  673. writer.AsmWrite(',');
  674. until false;
  675. { Substract section start for secrel32 type }
  676. {if consttype=aitconst_secrel32_symbol then
  677. writer.AsmWrite(' - $$');}
  678. writer.AsmLn;
  679. end;
  680. {aitconst_128bit,}
  681. aitconst_16bit,
  682. aitconst_8bit,
  683. aitconst_16bit_unaligned{,
  684. aitconst_rva_symbol,
  685. aitconst_secrel32_symbol} :
  686. begin
  687. writer.AsmWrite(ait_const2str[consttype]);
  688. l:=0;
  689. tokens:=1;
  690. repeat
  691. if assigned(tai_const(hp).sym) then
  692. begin
  693. if assigned(tai_const(hp).endsym) then
  694. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  695. else
  696. s:=tai_const(hp).sym.name;
  697. if tai_const(hp).value<>0 then
  698. s:=s+tostr_with_plus(tai_const(hp).value);
  699. end
  700. else
  701. s:=tostr(tai_const(hp).value);
  702. writer.AsmWrite(s);
  703. inc(l,length(s));
  704. inc(tokens);
  705. if (l>line_length) or
  706. (tokens>max_tokens) or
  707. (hp.next=nil) or
  708. (tai(hp.next).typ<>ait_const) or
  709. (tai_const(hp.next).consttype<>consttype) then
  710. break;
  711. hp:=tai(hp.next);
  712. writer.AsmWrite(',');
  713. until false;
  714. { Substract section start for secrel32 type }
  715. if consttype=aitconst_secrel32_symbol then
  716. writer.AsmWrite(' - $$');
  717. writer.AsmLn;
  718. end;
  719. else
  720. begin
  721. writer.AsmWrite(asminfo^.comment);
  722. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  723. Str(consttype,s);
  724. writer.AsmWriteLn(s);
  725. end;
  726. end;
  727. end;
  728. ait_string :
  729. begin
  730. pos:=0;
  731. for i:=1 to tai_string(hp).len do
  732. begin
  733. if pos=0 then
  734. begin
  735. writer.AsmWrite(#9'.ascii'#9'"');
  736. pos:=20;
  737. end;
  738. ch:=tai_string(hp).str[i-1];
  739. case ch of
  740. #0, {This can't be done by range, because a bug in FPC}
  741. #1..#31,
  742. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  743. '"' : s:='\"';
  744. '\' : s:='\\';
  745. else
  746. s:=ch;
  747. end;
  748. writer.AsmWrite(s);
  749. inc(pos,length(s));
  750. if (pos>line_length) or (i=tai_string(hp).len) then
  751. begin
  752. writer.AsmWriteLn('"');
  753. pos:=0;
  754. end;
  755. end;
  756. end;
  757. ait_instruction :
  758. begin
  759. WriteInstruction(taicpu(hp));
  760. end;
  761. ait_stab,
  762. ait_force_line,
  763. ait_function_name : ;
  764. else
  765. begin
  766. writer.AsmWrite(asminfo^.comment);
  767. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  768. Str(hp.typ,s);
  769. writer.AsmWriteLn(s);
  770. end;
  771. end;
  772. lasthp:=hp;
  773. hp:=tai(hp.next);
  774. end;
  775. //op:=taicpu(hp).opcode;
  776. //s:=#9+gas_op2str[op]+cond2str[taicpu(hp).condition];
  777. //if taicpu(hp).ops<>0 then
  778. // begin
  779. // sep:=#9;
  780. // for i:=0 to taicpu(hp).ops-1 do
  781. // begin
  782. // s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  783. // sep:=',';
  784. // end;
  785. // end;
  786. //owner.writer.AsmWriteLn(s);
  787. end;
  788. procedure TSdccSdasZ80Assembler.WriteAsmList;
  789. var
  790. hal: TAsmListType;
  791. begin
  792. for hal:=low(TasmlistType) to high(TasmlistType) do
  793. begin
  794. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  795. writetree(current_asmdata.asmlists[hal]);
  796. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  797. end;
  798. end;
  799. function TSdccSdasZ80Assembler.MakeCmdLine: TCmdStr;
  800. begin
  801. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  802. end;
  803. const
  804. as_sdcc_sdasZ80_asm_info : tasminfo =
  805. (
  806. id : as_sdcc_sdasz80;
  807. idtxt : 'SDCC-SDASZ80';
  808. asmbin : 'sdcc-sdasz80';
  809. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  810. supported_targets : [system_Z80_embedded];
  811. flags : [af_needar,af_smartlink_sections];
  812. labelprefix : '.L';
  813. comment : '; ';
  814. dollarsign: 's';
  815. );
  816. begin
  817. RegisterAssembler(as_sdcc_sdasZ80_asm_info,TSdccSdasZ80Assembler);
  818. end.