agsdasz80.pas 32 KB

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