agsdas6500.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. This unit implements an asm for the MOS Technology 6502
  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-sdas6502 assembler:
  18. http://sdcc.sourceforge.net/
  19. }
  20. unit agsdas6500;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,systems,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. assemble,
  27. cpubase;
  28. type
  29. { TSdccSdas6500Assembler }
  30. TSdccSdas6500Assembler=class(TExternalAssembler)
  31. private
  32. procedure WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  33. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  34. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;
  35. secflags:TSectionFlags=[];secprogbits:TSectionProgbits=SPB_None);
  36. procedure WriteInstruction(hp: taicpu);
  37. procedure WriteOper(const o:toper; opcode: tasmop;ops:longint;dest : boolean);
  38. procedure WriteOper_jmp(const o:toper; ai : taicpu);
  39. procedure WriteExternals;
  40. public
  41. procedure WriteTree(p : TAsmList); override;
  42. procedure WriteAsmList;override;
  43. function MakeCmdLine: TCmdStr; override;
  44. end;
  45. implementation
  46. uses
  47. cutils,globals,verbose,
  48. cpuinfo,
  49. cgbase,cgutils,
  50. {$ifdef FPC_SOFT_FPUX80}
  51. sfpux80,
  52. {$endif FPC_SOFT_FPUX80}
  53. finput;
  54. const
  55. line_length = 70;
  56. max_tokens : longint = 25;
  57. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  58. #9''#9,#9'FIXMEDQ'#9,#9'FIXMEDD'#9,#9'.dw'#9,#9'.db'#9,
  59. #9'FIXMESLEB',#9'FIXEMEULEB',
  60. #9'FIXMEDD RVA'#9,#9'FIXMEDD SECREL32'#9,
  61. #9'FIXME',#9'FIXME',#9'FIXME',#9'FIXME',
  62. #9'.dw'#9,#9'FIXMEDD'#9,#9'FIXMEDQ'#9
  63. );
  64. procedure TSdccSdas6500Assembler.WriteRealConstAsBytes(hp: tai_realconst; const dbdir: string; do_line: boolean);
  65. var
  66. pdata: pbyte;
  67. index, step, swapmask, count: longint;
  68. ssingle: single;
  69. ddouble: double;
  70. ccomp: comp;
  71. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  72. eextended: extended;
  73. {$else}
  74. {$ifdef FPC_SOFT_FPUX80}
  75. eextended: floatx80;
  76. {$endif}
  77. {$endif cpuextended}
  78. begin
  79. if do_line then
  80. begin
  81. case tai_realconst(hp).realtyp of
  82. aitrealconst_s32bit:
  83. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  84. aitrealconst_s64bit:
  85. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  86. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  87. { can't write full 80 bit floating point constants yet on non-x86 }
  88. aitrealconst_s80bit:
  89. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  90. {$else}
  91. {$ifdef FPC_SOFT_FPUX80}
  92. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  93. aitrealconst_s80bit:
  94. begin
  95. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  96. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s80val))
  97. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  98. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s80val))
  99. else
  100. internalerror(2017091904);
  101. end;
  102. {$pop}
  103. {$endif}
  104. {$endif cpuextended}
  105. aitrealconst_s64comp:
  106. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  107. else
  108. internalerror(2014050601);
  109. end;
  110. end;
  111. writer.AsmWrite(dbdir);
  112. { generic float writing code: get start address of value, then write
  113. byte by byte. Can't use fields directly, because e.g ts64comp is
  114. defined as extended on x86 }
  115. case tai_realconst(hp).realtyp of
  116. aitrealconst_s32bit:
  117. begin
  118. ssingle:=single(tai_realconst(hp).value.s32val);
  119. pdata:=@ssingle;
  120. end;
  121. aitrealconst_s64bit:
  122. begin
  123. ddouble:=double(tai_realconst(hp).value.s64val);
  124. pdata:=@ddouble;
  125. end;
  126. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  127. { can't write full 80 bit floating point constants yet on non-x86 }
  128. aitrealconst_s80bit:
  129. begin
  130. eextended:=extended(tai_realconst(hp).value.s80val);
  131. pdata:=@eextended;
  132. end;
  133. {$else}
  134. {$ifdef FPC_SOFT_FPUX80}
  135. {$push}{$warn 6018 off} { Unreachable code due to compile time evaluation }
  136. aitrealconst_s80bit:
  137. begin
  138. if sizeof(tai_realconst(hp).value.s80val) = sizeof(double) then
  139. eextended:=float64_to_floatx80(float64(double(tai_realconst(hp).value.s80val)))
  140. else if sizeof(tai_realconst(hp).value.s80val) = sizeof(single) then
  141. eextended:=float32_to_floatx80(float32(single(tai_realconst(hp).value.s80val)))
  142. else
  143. internalerror(2017091905);
  144. pdata:=@eextended;
  145. end;
  146. {$pop}
  147. {$endif}
  148. {$endif cpuextended}
  149. aitrealconst_s64comp:
  150. begin
  151. ccomp:=comp(tai_realconst(hp).value.s64compval);
  152. pdata:=@ccomp;
  153. end;
  154. else
  155. internalerror(2014051002);
  156. end;
  157. count:=tai_realconst(hp).datasize;
  158. { write bytes in inverse order if source and target endianess don't
  159. match }
  160. if source_info.endian<>target_info.endian then
  161. begin
  162. { go from back to front }
  163. index:=count-1;
  164. step:=-1;
  165. end
  166. else
  167. begin
  168. index:=0;
  169. step:=1;
  170. end;
  171. {$ifdef ARM}
  172. { ARM-specific: low and high dwords of a double may be swapped }
  173. if tai_realconst(hp).formatoptions=fo_hiloswapped then
  174. begin
  175. { only supported for double }
  176. if tai_realconst(hp).datasize<>8 then
  177. internalerror(2014050607);
  178. { switch bit of the index so that the words are written in
  179. the opposite order }
  180. swapmask:=4;
  181. end
  182. else
  183. {$endif ARM}
  184. swapmask:=0;
  185. repeat
  186. writer.AsmWrite(tostr(pdata[index xor swapmask]));
  187. inc(index,step);
  188. dec(count);
  189. if count<>0 then
  190. writer.AsmWrite(',');
  191. until count=0;
  192. { padding }
  193. for count:=tai_realconst(hp).datasize+1 to tai_realconst(hp).savesize do
  194. writer.AsmWrite(',0');
  195. writer.AsmLn;
  196. end;
  197. function TSdccSdas6500Assembler.sectionname(atype: TAsmSectiontype;
  198. const aname: string; aorder: TAsmSectionOrder): string;
  199. const
  200. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  201. '_CODE',
  202. '_DATA',
  203. '_DATA',
  204. '_DATA',
  205. '_BSS',
  206. '.threadvar',
  207. '.pdata',
  208. '', { stubs }
  209. '__DATA,__nl_symbol_ptr',
  210. '__DATA,__la_symbol_ptr',
  211. '__DATA,__mod_init_func',
  212. '__DATA,__mod_term_func',
  213. '.stab',
  214. '.stabstr',
  215. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  216. '.eh_frame',
  217. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges','.debug_loc','.debug_loclists',
  218. '.fpc',
  219. '.toc',
  220. '.init',
  221. '.fini',
  222. '.objc_class',
  223. '.objc_meta_class',
  224. '.objc_cat_cls_meth',
  225. '.objc_cat_inst_meth',
  226. '.objc_protocol',
  227. '.objc_string_object',
  228. '.objc_cls_meth',
  229. '.objc_inst_meth',
  230. '.objc_cls_refs',
  231. '.objc_message_refs',
  232. '.objc_symbols',
  233. '.objc_category',
  234. '.objc_class_vars',
  235. '.objc_instance_vars',
  236. '.objc_module_info',
  237. '.objc_class_names',
  238. '.objc_meth_var_types',
  239. '.objc_meth_var_names',
  240. '.objc_selector_strs',
  241. '.objc_protocol_ext',
  242. '.objc_class_ext',
  243. '.objc_property',
  244. '.objc_image_info',
  245. '.objc_cstring_object',
  246. '.objc_sel_fixup',
  247. '__DATA,__objc_data',
  248. '__DATA,__objc_const',
  249. '.objc_superrefs',
  250. '__DATA, __datacoal_nt,coalesced',
  251. '.objc_classlist',
  252. '.objc_nlclasslist',
  253. '.objc_catlist',
  254. '.obcj_nlcatlist',
  255. '.objc_protolist',
  256. '_STACK',
  257. '_HEAP',
  258. '.gcc_except_table',
  259. '.ARM.attributes'
  260. );
  261. begin
  262. if atype=sec_user then
  263. result:=aname
  264. else
  265. result:=secnames[atype];
  266. end;
  267. procedure TSdccSdas6500Assembler.WriteSection(atype: TAsmSectiontype;
  268. const aname: string; aorder: TAsmSectionOrder; secalign: longint;
  269. secflags: TSectionFlags; secprogbits: TSectionProgbits);
  270. var
  271. s : string;
  272. secflag: TSectionFlag;
  273. sectionprogbits,
  274. sectionflags: boolean;
  275. begin
  276. writer.AsmLn;
  277. sectionflags:=false;
  278. sectionprogbits:=false;
  279. writer.AsmWrite(#9'.area ');
  280. { sectionname may rename those sections, so we do not write flags/progbits for them,
  281. the assembler will ignore them/spite out a warning anyways }
  282. if not(atype in [sec_data,sec_rodata,sec_rodata_norel]) then
  283. begin
  284. sectionflags:=true;
  285. sectionprogbits:=true;
  286. end;
  287. s:=sectionname(atype,aname,aorder);
  288. writer.AsmWrite(s);
  289. writer.AsmLn;
  290. LastSecType:=atype;
  291. end;
  292. procedure TSdccSdas6500Assembler.WriteInstruction(hp: taicpu);
  293. var
  294. i: Integer;
  295. begin
  296. if hp.opcode=A_Bxx then
  297. begin
  298. if hp.condition=C_None then
  299. internalerror(2024040702);
  300. writer.AsmWrite(#9#9'b'+cond2str[hp.condition]);
  301. end
  302. else
  303. begin
  304. if hp.condition<>C_None then
  305. internalerror(2024040703);
  306. writer.AsmWrite(#9#9+std_op2str[hp.opcode]);
  307. end;
  308. if taicpu(hp).ops<>0 then
  309. begin
  310. writer.AsmWrite(#9);
  311. for i:=0 to taicpu(hp).ops-1 do
  312. begin
  313. if i<>0 then
  314. writer.AsmWrite(',');
  315. if is_calljmp(hp.opcode) then
  316. WriteOper_jmp(taicpu(hp).oper[i]^,hp)
  317. else
  318. WriteOper(taicpu(hp).oper[i]^,hp.opcode,taicpu(hp).ops,(i=2));
  319. end;
  320. end;
  321. writer.AsmLn;
  322. end;
  323. procedure TSdccSdas6500Assembler.WriteOper(const o: toper; opcode: tasmop; ops: longint; dest: boolean);
  324. var
  325. need_plus: Boolean;
  326. begin
  327. case o.typ of
  328. top_reg :
  329. writer.AsmWrite(std_regname(o.reg));
  330. top_const :
  331. begin
  332. writer.AsmWrite('#'+tostr(longint(o.val)));
  333. end;
  334. top_ref:
  335. begin
  336. //if assigned(o.ref^.symbol) and (o.ref^.refaddr in [addr_lo8,addr_hi8,addr_full]) then
  337. // begin
  338. // {if SmartAsm then
  339. // AddSymbol(o.ref^.symbol.name,false);}
  340. // if (o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO) then
  341. // internalerror(2020041101);
  342. // writer.AsmWrite('#');
  343. // case o.ref^.refaddr of
  344. // addr_lo8:
  345. // writer.AsmWrite('<');
  346. // addr_hi8:
  347. // writer.AsmWrite('>');
  348. // addr_full:
  349. // {nothing};
  350. // else
  351. // ;
  352. // end;
  353. // if o.ref^.offset<>0 then
  354. // writer.AsmWrite('('+ApplyAsmSymbolRestrictions(o.ref^.symbol.name)+'+'+tostr(o.ref^.offset)+')')
  355. // else
  356. // writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  357. // end
  358. //else if not assigned(o.ref^.symbol) and
  359. // ((o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO)) and
  360. // (o.ref^.offset<>0) then
  361. // begin
  362. // { sdasz80 doesn't range check the offset d in the (IX+d) and
  363. // (IY+d) addressing modes, but instead truncates it to
  364. // shortint, introducing silent bugs, and prevents us from
  365. // catching bugs in the code generator during compilation }
  366. // if ((o.ref^.base<>NR_NO) or (o.ref^.index<>NR_NO)) and
  367. // ((o.ref^.offset<-128) or (o.ref^.offset>127)) then
  368. // internalerror(2020042805);
  369. // writer.AsmWrite(tostr(o.ref^.offset));
  370. // writer.AsmWrite(' (');
  371. // if o.ref^.base<>NR_NO then
  372. // begin
  373. // if o.ref^.index<>NR_NO then
  374. // internalerror(2020040201);
  375. // writer.AsmWrite(std_regname(o.ref^.base));
  376. // end
  377. // else if o.ref^.index<>NR_NO then
  378. // begin
  379. // if o.ref^.scalefactor>1 then
  380. // internalerror(2020040202);
  381. // writer.AsmWrite(std_regname(o.ref^.index));
  382. // end;
  383. // writer.AsmWrite(')');
  384. // end
  385. //else
  386. // begin
  387. // writer.AsmWrite('(');
  388. // need_plus:=false;
  389. // if o.ref^.base<>NR_NO then
  390. // begin
  391. // if o.ref^.index<>NR_NO then
  392. // internalerror(2020040203);
  393. // writer.AsmWrite(std_regname(o.ref^.base));
  394. // need_plus:=true;
  395. // end
  396. // else if o.ref^.index<>NR_NO then
  397. // begin
  398. // if o.ref^.scalefactor>1 then
  399. // internalerror(2020040206);
  400. // writer.AsmWrite(std_regname(o.ref^.index));
  401. // need_plus:=true;
  402. // end;
  403. // if assigned(o.ref^.symbol) then
  404. // begin
  405. // {if SmartAsm then
  406. // AddSymbol(o.ref^.symbol.name,false);}
  407. // if need_plus then
  408. // writer.AsmWrite('+');
  409. // writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  410. // need_plus:=true;
  411. // end;
  412. // if o.ref^.offset<>0 then
  413. // begin
  414. // if need_plus and (o.ref^.offset>0) then
  415. // writer.AsmWrite('+');
  416. // writer.AsmWrite(tostr(o.ref^.offset));
  417. // need_plus:=true;
  418. // end;
  419. // if not need_plus then
  420. // writer.AsmWrite('0');
  421. // writer.AsmWrite(')');
  422. // end;
  423. end;
  424. else
  425. internalerror(2020100803);
  426. end;
  427. end;
  428. procedure TSdccSdas6500Assembler.WriteOper_jmp(const o: toper; ai: taicpu);
  429. begin
  430. case o.typ of
  431. top_reg :
  432. writer.AsmWrite(std_regname(o.reg));
  433. top_const :
  434. begin
  435. writer.AsmWrite('#'+tostr(longint(o.val)));
  436. end;
  437. top_ref:
  438. begin
  439. if o.ref^.refaddr=addr_no then
  440. begin
  441. writer.AsmWrite('TODO:indirect jump ref');
  442. //WriteReference(o.ref^);
  443. end
  444. else
  445. begin
  446. writer.AsmWrite(ApplyAsmSymbolRestrictions(o.ref^.symbol.name));
  447. //if SmartAsm then
  448. // AddSymbol(o.ref^.symbol.name,false);
  449. if o.ref^.offset>0 then
  450. writer.AsmWrite('+'+tostr(o.ref^.offset))
  451. else
  452. if o.ref^.offset<0 then
  453. writer.AsmWrite(tostr(o.ref^.offset));
  454. end;
  455. end;
  456. else
  457. internalerror(2020100804);
  458. end;
  459. end;
  460. procedure TSdccSdas6500Assembler.WriteExternals;
  461. var
  462. sym : TAsmSymbol;
  463. i : longint;
  464. begin
  465. writer.AsmWriteln('; Begin externals');
  466. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  467. begin
  468. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  469. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  470. writer.AsmWriteln(#9'.globl'#9+ApplyAsmSymbolRestrictions(sym.name));
  471. end;
  472. writer.AsmWriteln('; End externals');
  473. end;
  474. procedure TSdccSdas6500Assembler.WriteTree(p: TAsmList);
  475. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  476. var
  477. i: longint;
  478. alignment64 : int64;
  479. begin
  480. last_align:=alignment;
  481. if alignment>1 then
  482. writer.AsmWriteLn(#9'.bndry '+tostr(alignment));
  483. end;
  484. var
  485. lasthp,
  486. hp: tai;
  487. s, sb, LastSecName: string;
  488. counter,lines,i,j,l,tokens,pos,last_align: longint;
  489. quoted, do_line: Boolean;
  490. consttype: taiconst_type;
  491. ch: Char;
  492. InlineLevel : longint;
  493. prevfileinfo : tfileposinfo;
  494. previnfile : tinputfile;
  495. LastAlign: Integer;
  496. LastSecOrder: TAsmSectionOrder;
  497. begin
  498. if not assigned(p) then
  499. exit;
  500. InlineLevel:=0;
  501. last_align:=1;
  502. lasthp:=nil;
  503. { lineinfo is only needed for al_procedures (PFV) }
  504. do_line:=(cs_asm_source in current_settings.globalswitches) or
  505. ((cs_lineinfo in current_settings.moduleswitches)
  506. and (p=current_asmdata.asmlists[al_procedures]));
  507. hp:=tai(p.first);
  508. while assigned(hp) do
  509. begin
  510. prefetch(pointer(hp.next)^);
  511. if not(hp.typ in SkipLineInfo) then
  512. begin
  513. previnfile:=lastinfile;
  514. prevfileinfo:=lastfileinfo;
  515. current_filepos:=tailineinfo(hp).fileinfo;
  516. { no line info for inlined code }
  517. if do_line and (inlinelevel=0) then
  518. WriteSourceLine(hp as tailineinfo);
  519. (*if (lastfileinfo.line<>prevfileinfo.line) or
  520. (previnfile<>lastinfile) then
  521. begin
  522. { +0 postfix means no line increment per assembler instruction }
  523. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  524. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  525. writer.AsmWriteLn(' '+lastinfile.name)
  526. else
  527. writer.AsmLn;
  528. NewObject:=false;
  529. end;*)
  530. end;
  531. case hp.typ of
  532. ait_section :
  533. begin
  534. ResetSourceLines;
  535. if tai_section(hp).sectype<>sec_none then
  536. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  537. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  538. else
  539. begin
  540. {$ifdef EXTDEBUG}
  541. writer.AsmWrite(asminfo^.comment);
  542. writer.AsmWriteln(' sec_none');
  543. {$endif EXTDEBUG}
  544. end;
  545. end;
  546. ait_align :
  547. begin
  548. 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);
  549. end;
  550. ait_label :
  551. begin
  552. if tai_label(hp).labsym.is_used then
  553. begin
  554. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_label(hp).labsym.name));
  555. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  556. writer.AsmWriteLn('::')
  557. else
  558. writer.AsmWriteLn(':');
  559. end;
  560. end;
  561. ait_symbol :
  562. begin
  563. if not(tai_symbol(hp).has_value) then
  564. begin
  565. if tai_symbol(hp).is_global then
  566. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '::')
  567. else
  568. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + ':');
  569. end
  570. else
  571. begin
  572. if tai_symbol(hp).is_global then
  573. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '==' + tostr(tai_symbol(hp).value))
  574. else
  575. writer.AsmWriteLn(ApplyAsmSymbolRestrictions(tai_symbol(hp).sym.name) + '=' + tostr(tai_symbol(hp).value));
  576. end;
  577. end;
  578. ait_symbol_end :
  579. begin
  580. end;
  581. ait_datablock :
  582. begin
  583. if tai_datablock(hp).is_global or SmartAsm then
  584. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name) + '::')
  585. else
  586. writer.AsmWrite(ApplyAsmSymbolRestrictions(tai_datablock(hp).sym.name) + ':');
  587. {if SmartAsm then
  588. AddSymbol(tai_datablock(hp).sym.name,true);}
  589. writer.AsmWriteLn(#9'.rs'#9+tostr(tai_datablock(hp).size));
  590. end;
  591. ait_realconst:
  592. WriteRealConstAsBytes(tai_realconst(hp),#9'.db'#9,do_line);
  593. ait_const:
  594. begin
  595. consttype:=tai_const(hp).consttype;
  596. case consttype of
  597. aitconst_uleb128bit:
  598. writer.AsmWriteLn(ait_const2str[aitconst_8bit]+uleb128tostr(qword(tai_const(hp).value)));
  599. aitconst_sleb128bit:
  600. writer.AsmWriteLn(ait_const2str[aitconst_8bit]+sleb128tostr(tai_const(hp).value));
  601. aitconst_64bit,
  602. aitconst_64bit_unaligned,
  603. aitconst_32bit,
  604. aitconst_32bit_unaligned:
  605. begin
  606. writer.AsmWrite(#9'.dw'#9);
  607. l:=0;
  608. tokens:=1;
  609. repeat
  610. if assigned(tai_const(hp).sym) then
  611. begin
  612. if assigned(tai_const(hp).endsym) then
  613. s:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  614. else
  615. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  616. if tai_const(hp).value<>0 then
  617. s:=s+tostr_with_plus(tai_const(hp).value);
  618. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  619. s:=s+',0,0,0'
  620. else
  621. s:=s+',0';
  622. end
  623. else
  624. if consttype in [aitconst_64bit,aitconst_64bit_unaligned] then
  625. s:=tostr(Word(tai_const(hp).value)) +','+tostr(Word(tai_const(hp).value shr 16))+','+
  626. tostr(Word(tai_const(hp).value shr 32))+','+tostr(Word(tai_const(hp).value shr 48))
  627. else
  628. s:=tostr(Word(tai_const(hp).value))+','+tostr(Word(tai_const(hp).value shr 16));
  629. writer.AsmWrite(s);
  630. inc(l,length(s));
  631. inc(tokens);
  632. if (l>line_length) or
  633. (tokens>max_tokens) or
  634. (hp.next=nil) or
  635. (tai(hp.next).typ<>ait_const) or
  636. (tai_const(hp.next).consttype<>consttype) then
  637. break;
  638. hp:=tai(hp.next);
  639. writer.AsmWrite(',');
  640. until false;
  641. { Substract section start for secrel32 type }
  642. {if consttype=aitconst_secrel32_symbol then
  643. writer.AsmWrite(' - $$');}
  644. writer.AsmLn;
  645. end;
  646. {aitconst_128bit,}
  647. aitconst_16bit,
  648. aitconst_8bit,
  649. aitconst_16bit_unaligned{,
  650. aitconst_rva_symbol,
  651. aitconst_secrel32_symbol} :
  652. begin
  653. writer.AsmWrite(ait_const2str[consttype]);
  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:=ApplyAsmSymbolRestrictions(tai_const(hp).endsym.name)+'-'+ApplyAsmSymbolRestrictions(tai_const(hp).sym.name)
  661. else
  662. s:=ApplyAsmSymbolRestrictions(tai_const(hp).sym.name);
  663. if tai_const(hp).value<>0 then
  664. s:=s+tostr_with_plus(tai_const(hp).value);
  665. end
  666. else
  667. s:=tostr(tai_const(hp).value);
  668. writer.AsmWrite(s);
  669. inc(l,length(s));
  670. inc(tokens);
  671. if (l>line_length) or
  672. (tokens>max_tokens) or
  673. (hp.next=nil) or
  674. (tai(hp.next).typ<>ait_const) or
  675. (tai_const(hp.next).consttype<>consttype) then
  676. break;
  677. hp:=tai(hp.next);
  678. writer.AsmWrite(',');
  679. until false;
  680. { Substract section start for secrel32 type }
  681. if consttype=aitconst_secrel32_symbol then
  682. writer.AsmWrite(' - $$');
  683. writer.AsmLn;
  684. end;
  685. else
  686. begin
  687. writer.AsmWrite(asminfo^.comment);
  688. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  689. Str(consttype,s);
  690. writer.AsmWriteLn(s);
  691. end;
  692. end;
  693. end;
  694. ait_string :
  695. begin
  696. pos:=0;
  697. s:='';
  698. sb:='';
  699. for i:=1 to tai_string(hp).len do
  700. begin
  701. ch:=tai_string(hp).str[i-1];
  702. if ch in [#32..chr(ord('"')-1),char(ord('"')+1)..#127] then
  703. begin
  704. if sb<>'' then
  705. begin
  706. writer.AsmWriteln(sb);
  707. sb:='';
  708. pos:=0;
  709. end;
  710. if pos=0 then
  711. begin
  712. s:=#9'.ascii'#9'"'+ch;
  713. pos:=20;
  714. end
  715. else
  716. begin
  717. s:=s+ch;
  718. inc(pos);
  719. end;
  720. end
  721. else
  722. begin
  723. if s<>'' then
  724. begin
  725. writer.AsmWriteln(s+'"');
  726. s:='';
  727. pos:=0;
  728. end;
  729. if pos=0 then
  730. begin
  731. sb:=#9'.byte'#9+tostr(ord(ch));
  732. pos:=15;
  733. end
  734. else
  735. begin
  736. sb:=sb+','+tostr(ord(ch));
  737. inc(pos,4);
  738. end;
  739. end;
  740. if (pos>line_length) or (i=tai_string(hp).len) then
  741. begin
  742. if s<>'' then
  743. begin
  744. writer.AsmWriteLn(s+'"');
  745. s:='';
  746. end
  747. else if sb<>'' then
  748. begin
  749. writer.AsmWriteLn(sb);
  750. sb:='';
  751. end;
  752. pos:=0;
  753. end;
  754. end;
  755. end;
  756. ait_instruction :
  757. begin
  758. WriteInstruction(taicpu(hp));
  759. end;
  760. ait_directive :
  761. begin
  762. case tai_directive(hp).directive of
  763. asd_cpu :
  764. writer.AsmWriteLn('; CPU '+tai_directive(hp).name);
  765. else
  766. begin
  767. writer.AsmWrite(asminfo^.comment);
  768. writer.AsmWrite('WARNING: not yet implemented in assembler output: ait_directive.');
  769. Str(tai_directive(hp).directive,s);
  770. writer.AsmWriteLn(s);
  771. end;
  772. end;
  773. end;
  774. ait_cutobject :
  775. begin
  776. if SmartAsm then
  777. begin
  778. { only reset buffer if nothing has changed }
  779. if not writer.ClearIfEmpty then
  780. begin
  781. {if SmartAsm then
  782. begin
  783. WriteSmartExternals;
  784. FreeExternChainList;
  785. end;
  786. WriteGroups;}
  787. writer.AsmClose;
  788. DoAssemble;
  789. writer.AsmCreate(tai_cutobject(hp).place);
  790. {ResetSectionsList;
  791. WriteHeader;}
  792. end;
  793. { avoid empty files }
  794. LastSecType:=sec_none;
  795. LastSecName:='';
  796. LastSecOrder:=secorder_default;
  797. LastAlign:=1;
  798. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  799. begin
  800. if tai(hp.next).typ=ait_section then
  801. begin
  802. LastSecType:=tai_section(hp.next).sectype;
  803. LastSecName:=tai_section(hp.next).name^;
  804. LastSecOrder:=tai_section(hp.next).secorder;
  805. LastAlign:=tai_section(hp.next).secalign;
  806. end;
  807. hp:=tai(hp.next);
  808. end;
  809. if LastSecType<>sec_none then
  810. WriteSection(LastSecType,LastSecName,LastSecOrder,LastAlign);
  811. writer.MarkEmpty;
  812. //NewObject:=true;
  813. end;
  814. end;
  815. ait_marker :
  816. if tai_marker(hp).kind=mark_NoLineInfoStart then
  817. inc(InlineLevel)
  818. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  819. dec(InlineLevel);
  820. ait_stab,
  821. ait_force_line,
  822. ait_function_name : ;
  823. else
  824. if not WriteComments(hp) then
  825. begin
  826. writer.AsmWrite(asminfo^.comment);
  827. writer.AsmWrite('WARNING: not yet implemented in assembler output: ');
  828. Str(hp.typ,s);
  829. writer.AsmWriteLn(s);
  830. end;
  831. end;
  832. lasthp:=hp;
  833. hp:=tai(hp.next);
  834. end;
  835. end;
  836. procedure TSdccSdas6500Assembler.WriteAsmList;
  837. var
  838. hal: TAsmListType;
  839. begin
  840. WriteExternals;
  841. for hal:=low(TasmlistType) to high(TasmlistType) do
  842. begin
  843. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  844. writetree(current_asmdata.asmlists[hal]);
  845. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  846. end;
  847. end;
  848. function TSdccSdas6500Assembler.MakeCmdLine: TCmdStr;
  849. begin
  850. result := {'-mmcu='+lower(cputypestr[current_settings.cputype])+' '+}inherited MakeCmdLine;
  851. end;
  852. const
  853. as_sdcc_sdas6500_asm_info : tasminfo =
  854. (
  855. id : as_sdcc_sdas6500;
  856. idtxt : 'SDCC-SDAS6500';
  857. asmbin : 'sdas6500';
  858. asmcmd : '-g -o $EXTRAOPT $OBJ $ASM';
  859. supported_targets : [system_mos6502_embedded];
  860. flags : [af_needar];
  861. labelprefix : '.L';
  862. labelmaxlen : 79;
  863. comment : '; ';
  864. dollarsign: '$';
  865. );
  866. begin
  867. RegisterAssembler(as_sdcc_sdas6500_asm_info,TSdccSdas6500Assembler);
  868. end.