agsdasz80.pas 33 KB

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