agsdasz80.pas 31 KB

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