agsdas6500.pas 35 KB

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