2
0

agsdasz80.pas 32 KB

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