agx86nsm.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for the Nasm assembler with
  4. Intel syntax for the i386+
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit agx86nsm;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,cpubase,globtype,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. { TX86NasmAssembler }
  26. TX86NasmAssembler = class(texternalassembler)
  27. strict private
  28. type
  29. { TX86NasmSection }
  30. TX86NasmSection=class(TFPHashObject)
  31. end;
  32. { TX86NasmGroup }
  33. TX86NasmGroup=class(TFPHashObject)
  34. Sections: TFPHashObjectList;
  35. constructor Create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  36. destructor Destroy;override;
  37. end;
  38. private
  39. FSections: TFPHashObjectList;
  40. FGroups: TFPHashObjectList;
  41. using_relative : boolean;
  42. function CodeSectionName(const aname:string): string;
  43. procedure WriteReference(var ref : treference);
  44. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  45. procedure WriteOper_jmp(const o:toper; ai : taicpu);
  46. procedure WriteSection(atype:TAsmSectiontype;const aname:string;alignment : longint);
  47. procedure ResetSectionsList;
  48. procedure AddGroup(const grpname: string);
  49. procedure AddSegmentToGroup(const grpname,segname: string);
  50. procedure WriteGroup(data:TObject;arg:pointer);
  51. procedure WriteGroups;
  52. protected
  53. function single2str(d: single): string; override;
  54. function double2str(d: double): string; override;
  55. function extended2str(e: extended): string; override;
  56. public
  57. destructor Destroy;override;
  58. procedure WriteTree(p:TAsmList);override;
  59. procedure WriteAsmList;override;
  60. procedure WriteExternals;
  61. procedure WriteSmartExternals;
  62. procedure WriteHeader;
  63. function MakeCmdLine: TCmdStr;override;
  64. end;
  65. implementation
  66. uses
  67. cutils,globals,systems,
  68. fmodule,finput,verbose,cpuinfo,cgbase,omfbase
  69. ;
  70. const
  71. line_length = 64;
  72. nasm_regname_table : array[tregisterindex] of string[13] = (
  73. {r386nasm.inc contains the Nasm name of each register.}
  74. {$if defined(x86_64)}
  75. {$i r8664nasm.inc}
  76. {$elseif defined(i386)}
  77. {$i r386nasm.inc}
  78. {$elseif defined(i8086)}
  79. {$i r8086nasm.inc}
  80. {$endif}
  81. );
  82. { nasm 2.13 expects lowercase cpu names }
  83. nasm_cpu_name : array[tcputype] of string = (
  84. {$if defined(x86_64)}
  85. 'ia64', // cpu_none,
  86. 'x64', // cpu_athlon64,
  87. 'ia64', // cpu_core_i,
  88. 'ia64', // cpu_core_avx,
  89. 'ia64' // cpu_core_avx2
  90. {$elseif defined(i386)}
  91. 'ia64', // cpu_none,
  92. '386', // cpu_386,
  93. '486', // cpu_486,
  94. 'pentium', // cpu_Pentium,
  95. 'p2', // cpu_Pentium2,
  96. 'p3', // cpu_Pentium3,
  97. 'p4', // cpu_Pentium4,
  98. 'p4', // cpu_PentiumM,
  99. 'ia64', // cpu_core_i,
  100. 'ia64', // cpu_core_avx,
  101. 'ia64' // cpu_core_avx2
  102. {$elseif defined(i8086)}
  103. 'ia64', // cpu_none
  104. '8086', // cpu_8086
  105. '186', // cpu_186
  106. '286', // cpu_286
  107. '386', // cpu_386
  108. '486', // cpu_486
  109. 'pentium', // cpu_Pentium
  110. 'p2', // cpu_Pentium2
  111. 'p3', // cpu_Pentium3
  112. 'p4', // cpu_Pentium4
  113. 'p4' // cpu_PentiumM
  114. {$endif}
  115. );
  116. function nasm_regname(r:Tregister):string;
  117. var
  118. p : tregisterindex;
  119. begin
  120. p:=findreg_by_number(r);
  121. if p<>0 then
  122. result:=nasm_regname_table[p]
  123. else
  124. result:=generic_regname(r);
  125. end;
  126. function TX86NasmAssembler.single2str(d: single): string;
  127. var
  128. hs : string;
  129. p : longint;
  130. begin
  131. str(d,hs);
  132. { nasm expects a lowercase e }
  133. p:=pos('E',hs);
  134. if p>0 then
  135. hs[p]:='e';
  136. p:=pos('+',hs);
  137. if p>0 then
  138. delete(hs,p,1);
  139. single2str:=lower(hs);
  140. end;
  141. function TX86NasmAssembler.double2str(d: double): string;
  142. var
  143. hs : string;
  144. p : longint;
  145. begin
  146. str(d,hs);
  147. { nasm expects a lowercase e }
  148. p:=pos('E',hs);
  149. if p>0 then
  150. hs[p]:='e';
  151. p:=pos('+',hs);
  152. if p>0 then
  153. delete(hs,p,1);
  154. double2str:=lower(hs);
  155. end;
  156. function TX86NasmAssembler.extended2str(e: extended): string;
  157. var
  158. hs : string;
  159. p : longint;
  160. begin
  161. str(e,hs);
  162. { nasm expects a lowercase e }
  163. p:=pos('E',hs);
  164. if p>0 then
  165. hs[p]:='e';
  166. p:=pos('+',hs);
  167. if p>0 then
  168. delete(hs,p,1);
  169. extended2str:=lower(hs);
  170. end;
  171. destructor TX86NasmAssembler.Destroy;
  172. begin
  173. FSections.Free;
  174. FGroups.Free;
  175. inherited Destroy;
  176. end;
  177. function sizestr(s:topsize;dest:boolean):string;
  178. begin
  179. case s of
  180. S_B : sizestr:='byte ';
  181. S_W : sizestr:='word ';
  182. S_L : sizestr:='dword ';
  183. S_Q : sizestr:='qword ';
  184. S_IS : sizestr:='word ';
  185. S_IL : sizestr:='dword ';
  186. S_IQ : sizestr:='qword ';
  187. S_FS : sizestr:='dword ';
  188. S_FL : sizestr:='qword ';
  189. S_FX : sizestr:='tword ';
  190. S_BW : if dest then
  191. sizestr:='word '
  192. else
  193. sizestr:='byte ';
  194. S_BL : if dest then
  195. sizestr:='dword '
  196. else
  197. sizestr:='byte ';
  198. S_WL : if dest then
  199. sizestr:='dword '
  200. else
  201. sizestr:='word ';
  202. {$ifdef x86_64}
  203. S_BQ : if dest then
  204. sizestr:='qword '
  205. else
  206. sizestr:='byte ';
  207. S_WQ : if dest then
  208. sizestr:='qword '
  209. else
  210. sizestr:='word ';
  211. S_LQ : if dest then
  212. sizestr:='qword '
  213. else
  214. sizestr:='dword ';
  215. { Nothing needed for XMM registers }
  216. S_XMM: sizestr:='';
  217. {$endif x86_64}
  218. else { S_NO }
  219. sizestr:='';
  220. end;
  221. end;
  222. Function PadTabs(const p:string;addch:char):string;
  223. var
  224. s : string;
  225. i : longint;
  226. begin
  227. i:=length(p);
  228. if addch<>#0 then
  229. begin
  230. inc(i);
  231. s:=p+addch;
  232. end
  233. else
  234. s:=p;
  235. if i<8 then
  236. PadTabs:=s+#9#9
  237. else
  238. PadTabs:=s+#9;
  239. end;
  240. {****************************************************************************
  241. TX86NasmAssembler.TX86NasmGroup
  242. ****************************************************************************}
  243. constructor TX86NasmAssembler.TX86NasmGroup.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  244. begin
  245. inherited;
  246. Sections:=TFPHashObjectList.Create;
  247. end;
  248. destructor TX86NasmAssembler.TX86NasmGroup.Destroy;
  249. begin
  250. Sections.Free;
  251. inherited Destroy;
  252. end;
  253. {****************************************************************************
  254. TX86NasmAssembler
  255. ****************************************************************************}
  256. function TX86NasmAssembler.CodeSectionName(const aname:string): string;
  257. begin
  258. {$ifdef i8086}
  259. if current_settings.x86memorymodel in x86_far_code_models then
  260. begin
  261. if cs_huge_code in current_settings.moduleswitches then
  262. result:=aname + '_TEXT'
  263. else
  264. result:=current_module.modulename^ + '_TEXT';
  265. end
  266. else
  267. result:='_TEXT';
  268. {$else i8086}
  269. result:='.text';
  270. {$endif}
  271. end;
  272. procedure TX86NasmAssembler.WriteReference(var ref : treference);
  273. var
  274. first : boolean;
  275. base_done : boolean;
  276. begin
  277. with ref do
  278. begin
  279. writer.AsmWrite('[');
  280. first:=true;
  281. base_done:=false;
  282. if (segment<>NR_NO) then
  283. writer.AsmWrite(nasm_regname(segment)+':');
  284. {$ifdef x86_64}
  285. if (base=NR_RIP) then
  286. begin
  287. { nasm RIP is implicit for pic }
  288. if not (ref.refaddr in [addr_pic,addr_pic_no_got]) and not using_relative then
  289. writer.AsmWrite('$ + ');
  290. base_done:=true;
  291. end;
  292. {$endif x86_64}
  293. if assigned(symbol) then
  294. begin
  295. writer.AsmWrite(symbol.name);
  296. if SmartAsm then
  297. AddSymbol(symbol.name,false);
  298. first:=false;
  299. end;
  300. if (base<>NR_NO) and not base_done then
  301. begin
  302. if not(first) then
  303. writer.AsmWrite('+')
  304. else
  305. first:=false;
  306. writer.AsmWrite(nasm_regname(base))
  307. end;
  308. if (index<>NR_NO) then
  309. begin
  310. if not(first) then
  311. writer.AsmWrite('+')
  312. else
  313. first:=false;
  314. writer.AsmWrite(nasm_regname(index));
  315. if scalefactor<>0 then
  316. writer.AsmWrite('*'+tostr(scalefactor));
  317. end;
  318. if offset<0 then
  319. begin
  320. writer.AsmWrite(tostr(offset));
  321. first:=false;
  322. end
  323. else if (offset>0) then
  324. begin
  325. writer.AsmWrite('+'+tostr(offset));
  326. first:=false;
  327. end;
  328. if first then
  329. writer.AsmWrite('0');
  330. writer.AsmWrite(']');
  331. end;
  332. end;
  333. procedure TX86NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  334. begin
  335. case o.typ of
  336. top_reg :
  337. writer.AsmWrite(nasm_regname(o.reg));
  338. top_const :
  339. begin
  340. if (ops=1) and (opcode<>A_RET) then
  341. writer.AsmWrite(sizestr(s,dest));
  342. writer.AsmWrite(tostr(longint(o.val)));
  343. end;
  344. top_ref :
  345. begin
  346. if o.ref^.refaddr in [addr_no,addr_pic,addr_pic_no_got] then
  347. begin
  348. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  349. (opcode = A_LSS) or (opcode = A_LFS) or
  350. {$ifndef x86_64}
  351. (opcode = A_LES) or (opcode = A_LDS) or
  352. {$endif x86_64}
  353. // (opcode = A_SHR) or (opcode = A_SHL) or
  354. // (opcode = A_SAR) or (opcode = A_SAL) or
  355. (opcode = A_OUT) or (opcode = A_IN)) then
  356. writer.AsmWrite(sizestr(s,dest));
  357. WriteReference(o.ref^);
  358. end
  359. {$ifdef i8086}
  360. else if o.ref^.refaddr=addr_dgroup then
  361. begin
  362. writer.AsmWrite('DGROUP');
  363. { Make sure GROUP DGROUP is generated }
  364. AddGroup('DGROUP');
  365. end
  366. else if o.ref^.refaddr=addr_fardataseg then
  367. begin
  368. writer.AsmWrite(current_module.modulename^+'_DATA');
  369. end
  370. {$endif i8086}
  371. else
  372. begin
  373. {$ifdef x86_64}
  374. if s=S_L then
  375. writer.AsmWrite('dword ')
  376. else
  377. writer.AsmWrite('qword ');
  378. {$endif}
  379. {$ifdef i386}
  380. writer.AsmWrite('dword ');
  381. {$endif i386}
  382. {$ifdef i8086}
  383. if o.ref^.refaddr=addr_seg then
  384. writer.AsmWrite('SEG ')
  385. else
  386. writer.AsmWrite('word ');
  387. {$endif i8086}
  388. if assigned(o.ref^.symbol) then
  389. begin
  390. if SmartAsm then
  391. AddSymbol(o.ref^.symbol.name,false);
  392. writer.AsmWrite(o.ref^.symbol.name);
  393. if o.ref^.offset=0 then
  394. exit;
  395. end;
  396. if o.ref^.offset>0 then
  397. writer.AsmWrite('+');
  398. writer.AsmWrite(tostr(o.ref^.offset));
  399. end;
  400. end;
  401. else
  402. internalerror(10001);
  403. end;
  404. end;
  405. procedure TX86NasmAssembler.WriteOper_jmp(const o:toper; ai : taicpu);
  406. begin
  407. case o.typ of
  408. top_reg :
  409. writer.AsmWrite(nasm_regname(o.reg));
  410. top_ref :
  411. if o.ref^.refaddr=addr_no then
  412. begin
  413. if ai.opsize=S_FAR then
  414. writer.AsmWrite('far ');
  415. WriteReference(o.ref^);
  416. end
  417. else
  418. begin
  419. if ai.opsize=S_FAR then
  420. writer.AsmWrite('far ');
  421. { else
  422. writer.AsmWrite('near ') just disables short branches, increasing code size.
  423. Omitting it does not cause any bad effects, tested with nasm 2.11. }
  424. writer.AsmWrite(o.ref^.symbol.name);
  425. if SmartAsm then
  426. AddSymbol(o.ref^.symbol.name,false);
  427. if o.ref^.offset>0 then
  428. writer.AsmWrite('+'+tostr(o.ref^.offset))
  429. else
  430. if o.ref^.offset<0 then
  431. writer.AsmWrite(tostr(o.ref^.offset));
  432. end;
  433. top_const :
  434. writer.AsmWrite(tostr(aint(o.val)));
  435. else
  436. internalerror(10001);
  437. end;
  438. end;
  439. const
  440. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[30]=(
  441. #9'FIXME_128BIT'#9,#9'DQ'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  442. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  443. #9'RVA'#9,#9'SECREL32'#9,#9'FIXME_darwin_dwarf_delta64'#9,
  444. #9'FIXME_darwin_dwarf_delta32'#9,#9'FIXME_half16bit'#9,#9'FIXME_gs'#9,
  445. #9'DW'#9,#9'DD'#9,#9'FIXME_64BIT_UNALIGNED'#9
  446. );
  447. procedure TX86NasmAssembler.WriteSection(atype : TAsmSectiontype;
  448. const aname : string; alignment : longint);
  449. const
  450. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  451. '.text',
  452. '.data',
  453. '.data',
  454. '.rodata',
  455. '.bss',
  456. '.tbss',
  457. '.pdata',
  458. '.text','.data','.data','.data','.data',
  459. '.stab',
  460. '.stabstr',
  461. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  462. '.eh_frame',
  463. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  464. '.fpc',
  465. '',
  466. '.init',
  467. '.fini',
  468. '.objc_class',
  469. '.objc_meta_class',
  470. '.objc_cat_cls_meth',
  471. '.objc_cat_inst_meth',
  472. '.objc_protocol',
  473. '.objc_string_object',
  474. '.objc_cls_meth',
  475. '.objc_inst_meth',
  476. '.objc_cls_refs',
  477. '.objc_message_refs',
  478. '.objc_symbols',
  479. '.objc_category',
  480. '.objc_class_vars',
  481. '.objc_instance_vars',
  482. '.objc_module_info',
  483. '.objc_class_names',
  484. '.objc_meth_var_types',
  485. '.objc_meth_var_names',
  486. '.objc_selector_strs',
  487. '.objc_protocol_ext',
  488. '.objc_class_ext',
  489. '.objc_property',
  490. '.objc_image_info',
  491. '.objc_cstring_object',
  492. '.objc_sel_fixup',
  493. '__DATA,__objc_data',
  494. '__DATA,__objc_const',
  495. '.objc_superrefs',
  496. '__DATA, __datacoal_nt,coalesced',
  497. '.objc_classlist',
  498. '.objc_nlclasslist',
  499. '.objc_catlist',
  500. '.obcj_nlcatlist',
  501. '.objc_protolist',
  502. '.stack',
  503. '.heap'
  504. );
  505. var
  506. secname,secgroup: string;
  507. begin
  508. writer.AsmLn;
  509. writer.AsmWrite('SECTION ');
  510. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  511. Thus, data which normally goes into .rodata and .rodata_norel sections must
  512. end up in .data section }
  513. if (atype in [sec_rodata,sec_rodata_norel]) and
  514. (target_info.system=system_i386_go32v2) then
  515. writer.AsmWrite('.data')
  516. else if (atype=sec_user) then
  517. writer.AsmWrite(aname)
  518. else if (atype=sec_threadvar) and
  519. (target_info.system in (systems_windows+systems_wince)) then
  520. writer.AsmWrite('.tls'#9'bss')
  521. else if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
  522. begin
  523. if secnames[atype]='.text' then
  524. secname:=CodeSectionName(aname)
  525. else if omf_segclass(atype)='FAR_DATA' then
  526. secname:=current_module.modulename^ + '_DATA'
  527. else
  528. secname:=omf_secnames[atype];
  529. writer.AsmWrite(secname);
  530. { first use of this section in the object file? }
  531. if FSections.Find(secname)=nil then
  532. begin
  533. { yes -> write the section attributes as well }
  534. if atype=sec_stack then
  535. writer.AsmWrite(' stack');
  536. if atype in [sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges] then
  537. writer.AsmWrite(' use32')
  538. else
  539. writer.AsmWrite(' use16');
  540. writer.AsmWrite(' class='+omf_segclass(atype)+
  541. ' align='+tostr(omf_sectiontype2align(atype)));
  542. TX86NasmSection.Create(FSections,secname);
  543. secgroup:=omf_section_primary_group(atype,aname);
  544. if secgroup<>'' then
  545. AddSegmentToGroup(secgroup,secname);
  546. end;
  547. end
  548. else if secnames[atype]='.text' then
  549. writer.AsmWrite(CodeSectionName(aname))
  550. else
  551. writer.AsmWrite(secnames[atype]);
  552. if create_smartlink_sections and
  553. (atype<>sec_bss) and
  554. (aname<>'') then
  555. begin
  556. writer.AsmWrite('.');
  557. writer.AsmWrite(aname);
  558. if atype in [sec_init, sec_fini, sec_stub, sec_code] then
  559. writer.AsmWrite(' code align='+tostr(alignment))
  560. else if atype in [sec_rodata, sec_rodata_norel] then
  561. writer.AsmWrite(' rdata align='+tostr(alignment))
  562. else
  563. writer.AsmWrite(' data align='+tostr(alignment))
  564. end;
  565. writer.AsmLn;
  566. LastSecType:=atype;
  567. end;
  568. procedure TX86NasmAssembler.ResetSectionsList;
  569. begin
  570. FSections.Free;
  571. FSections:=TFPHashObjectList.Create;
  572. FGroups.Free;
  573. FGroups:=TFPHashObjectList.Create;
  574. end;
  575. procedure TX86NasmAssembler.AddGroup(const grpname: string);
  576. begin
  577. if FGroups.Find(grpname)=nil then
  578. TX86NasmGroup.Create(FGroups,grpname);
  579. end;
  580. procedure TX86NasmAssembler.AddSegmentToGroup(const grpname, segname: string);
  581. var
  582. grp: TX86NasmGroup;
  583. begin
  584. grp:=TX86NasmGroup(FGroups.Find(grpname));
  585. if grp=nil then
  586. grp:=TX86NasmGroup.Create(FGroups,grpname);
  587. TX86NasmSection.Create(grp.Sections,segname);
  588. end;
  589. procedure TX86NasmAssembler.WriteGroup(data: TObject; arg: pointer);
  590. var
  591. grp: TX86NasmGroup;
  592. i: Integer;
  593. begin
  594. grp:=TX86NasmGroup(data);
  595. writer.AsmWrite('GROUP '+grp.Name);
  596. for i:=0 to grp.Sections.Count-1 do
  597. writer.AsmWrite(' '+grp.Sections.NameOfIndex(i));
  598. writer.AsmLn;
  599. end;
  600. procedure TX86NasmAssembler.WriteGroups;
  601. {$ifdef i8086}
  602. var
  603. i: Integer;
  604. {$endif i8086}
  605. begin
  606. {$ifdef i8086}
  607. if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
  608. begin
  609. if current_settings.x86memorymodel=mm_huge then
  610. WriteSection(sec_data,'',2);
  611. writer.AsmLn;
  612. FGroups.ForEachCall(@WriteGroup,nil);
  613. end;
  614. {$endif i8086}
  615. end;
  616. procedure TX86NasmAssembler.WriteTree(p:TAsmList);
  617. {$ifdef cpuextended}
  618. type
  619. t80bitarray = array[0..9] of byte;
  620. {$endif cpuextended}
  621. var
  622. s : string;
  623. hp : tai;
  624. counter,
  625. lines,
  626. i,j,l : longint;
  627. InlineLevel : longint;
  628. consttype : taiconst_type;
  629. do_line, SkipNewLine,
  630. quoted : boolean;
  631. fixed_opcode: TAsmOp;
  632. prefix, LastSecName : string;
  633. LastAlign : LongInt;
  634. cpu: tcputype;
  635. prevfileinfo : tfileposinfo;
  636. previnfile : tinputfile;
  637. NewObject : boolean;
  638. begin
  639. if not assigned(p) then
  640. exit;
  641. InlineLevel:=0;
  642. NewObject:=true;
  643. { lineinfo is only needed for al_procedures (PFV) }
  644. do_line:=(cs_asm_source in current_settings.globalswitches) or
  645. ((cs_lineinfo in current_settings.moduleswitches)
  646. and (p=current_asmdata.asmlists[al_procedures]));
  647. hp:=tai(p.first);
  648. while assigned(hp) do
  649. begin
  650. prefetch(pointer(hp.next)^);
  651. if not(hp.typ in SkipLineInfo) then
  652. begin
  653. previnfile:=lastinfile;
  654. prevfileinfo:=lastfileinfo;
  655. current_filepos:=tailineinfo(hp).fileinfo;
  656. { no line info for inlined code }
  657. if do_line and (inlinelevel=0) then
  658. WriteSourceLine(hp as tailineinfo);
  659. if (lastfileinfo.line<>prevfileinfo.line) or
  660. (previnfile<>lastinfile) then
  661. begin
  662. { +0 postfix means no line increment per assembler instruction }
  663. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  664. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  665. writer.AsmWriteLn(' '+lastinfile.name)
  666. else
  667. writer.AsmLn;
  668. NewObject:=false;
  669. end;
  670. end;
  671. case hp.typ of
  672. ait_comment :
  673. Begin
  674. writer.AsmWrite(asminfo^.comment);
  675. writer.AsmWritePChar(tai_comment(hp).str);
  676. writer.AsmLn;
  677. End;
  678. ait_regalloc :
  679. begin
  680. if (cs_asm_regalloc in current_settings.globalswitches) then
  681. writer.AsmWriteLn(#9#9+asminfo^.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  682. regallocstr[tai_regalloc(hp).ratype]);
  683. end;
  684. ait_tempalloc :
  685. begin
  686. if (cs_asm_tempalloc in current_settings.globalswitches) then
  687. WriteTempalloc(tai_tempalloc(hp));
  688. end;
  689. ait_section :
  690. begin
  691. if tai_section(hp).sectype<>sec_none then
  692. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secalign);
  693. LastSecType:=tai_section(hp).sectype;
  694. end;
  695. ait_align :
  696. begin
  697. if (tai_align(hp).aligntype>1) then
  698. begin
  699. if (LastSecType=sec_bss) or (
  700. (LastSecType=sec_threadvar) and
  701. (target_info.system in (systems_windows+systems_wince))
  702. ) then
  703. writer.AsmWriteLn(#9'ALIGNB '+tostr(tai_align(hp).aligntype))
  704. else if tai_align_abstract(hp).use_op then
  705. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB '+tostr(tai_align_abstract(hp).fillop))
  706. else if LastSecType in [sec_code,sec_stub,sec_init,sec_fini] then
  707. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype))
  708. else
  709. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB 0');
  710. end;
  711. end;
  712. ait_datablock :
  713. begin
  714. if tai_datablock(hp).is_global or SmartAsm then
  715. begin
  716. writer.AsmWrite(#9'GLOBAL ');
  717. writer.AsmWriteLn(tai_datablock(hp).sym.name);
  718. end;
  719. writer.AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  720. if SmartAsm then
  721. AddSymbol(tai_datablock(hp).sym.name,true);
  722. writer.AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  723. end;
  724. ait_const:
  725. begin
  726. consttype:=tai_const(hp).consttype;
  727. case consttype of
  728. aitconst_uleb128bit,
  729. aitconst_sleb128bit,
  730. aitconst_128bit:
  731. begin
  732. writer.AsmWriteLn(asminfo^.comment+'Unsupported const type '+
  733. ait_const2str[consttype]);
  734. end;
  735. {$ifdef i8086}
  736. aitconst_farptr:
  737. begin
  738. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  739. if assigned(tai_const(hp).sym) then
  740. begin
  741. if SmartAsm then
  742. AddSymbol(tai_const(hp).sym.name,false);
  743. writer.AsmWrite(tai_const(hp).sym.name);
  744. if tai_const(hp).value<>0 then
  745. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  746. writer.AsmLn;
  747. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  748. writer.AsmWrite('SEG ');
  749. writer.AsmWrite(tai_const(hp).sym.name);
  750. end
  751. else
  752. writer.AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
  753. tostr(hi(longint(tai_const(hp).value))));
  754. writer.AsmLn;
  755. end;
  756. aitconst_seg:
  757. begin
  758. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  759. if assigned(tai_const(hp).sym) then
  760. begin
  761. if SmartAsm then
  762. AddSymbol(tai_const(hp).sym.name,false);
  763. writer.AsmWrite('SEG ');
  764. writer.AsmWrite(tai_const(hp).sym.name);
  765. end
  766. else
  767. internalerror(2015110501);
  768. writer.AsmLn;
  769. end;
  770. aitconst_dgroup:
  771. writer.AsmWriteLn(#9'DW'#9'DGROUP');
  772. aitconst_fardataseg:
  773. writer.AsmWriteLn(#9'DW'#9+current_module.modulename^+'_DATA');
  774. {$endif i8086}
  775. {$ifdef x86_64}
  776. aitconst_rva_symbol,
  777. aitconst_secrel32_symbol: ;
  778. {$endif x86_64}
  779. {$ifdef i386}
  780. aitconst_rva_symbol,
  781. aitconst_secrel32_symbol,
  782. {$endif i386}
  783. aitconst_64bit,
  784. aitconst_32bit,
  785. aitconst_16bit,
  786. aitconst_8bit,
  787. aitconst_16bit_unaligned,
  788. aitconst_32bit_unaligned,
  789. aitconst_64bit_unaligned:
  790. begin
  791. writer.AsmWrite(ait_const2str[tai_const(hp).consttype]);
  792. l:=0;
  793. repeat
  794. if assigned(tai_const(hp).sym) then
  795. begin
  796. if SmartAsm then
  797. begin
  798. AddSymbol(tai_const(hp).sym.name,false);
  799. if assigned(tai_const(hp).endsym) then
  800. AddSymbol(tai_const(hp).endsym.name,false);
  801. end;
  802. if assigned(tai_const(hp).endsym) then
  803. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  804. else
  805. s:=tai_const(hp).sym.name;
  806. if tai_const(hp).value<>0 then
  807. s:=s+tostr_with_plus(tai_const(hp).value);
  808. end
  809. else
  810. s:=tostr(tai_const(hp).value);
  811. writer.AsmWrite(s);
  812. inc(l,length(s));
  813. if (l>line_length) or
  814. (hp.next=nil) or
  815. (tai(hp.next).typ<>ait_const) or
  816. (tai_const(hp.next).consttype<>consttype) then
  817. break;
  818. hp:=tai(hp.next);
  819. writer.AsmWrite(',');
  820. until false;
  821. writer.AsmLn;
  822. end;
  823. else
  824. internalerror(200704252);
  825. end;
  826. end;
  827. ait_realconst:
  828. begin
  829. WriteRealConstAsBytes(tai_realconst(hp),#9#9'DB'#9,do_line);
  830. end;
  831. ait_string :
  832. begin
  833. counter := 0;
  834. lines := tai_string(hp).len div line_length;
  835. { separate lines in different parts }
  836. if tai_string(hp).len > 0 then
  837. Begin
  838. for j := 0 to lines-1 do
  839. begin
  840. writer.AsmWrite(#9#9'DB'#9);
  841. quoted:=false;
  842. for i:=counter to counter+line_length-1 do
  843. begin
  844. { it is an ascii character. }
  845. if (ord(tai_string(hp).str[i])>31) and
  846. (ord(tai_string(hp).str[i])<128) and
  847. (tai_string(hp).str[i]<>'"') then
  848. begin
  849. if not(quoted) then
  850. begin
  851. if i>counter then
  852. writer.AsmWrite(',');
  853. writer.AsmWrite('"');
  854. end;
  855. writer.AsmWrite(tai_string(hp).str[i]);
  856. quoted:=true;
  857. end { if > 31 and < 128 and ord('"') }
  858. else
  859. begin
  860. if quoted then
  861. writer.AsmWrite('"');
  862. if i>counter then
  863. writer.AsmWrite(',');
  864. quoted:=false;
  865. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  866. end;
  867. end; { end for i:=0 to... }
  868. if quoted then writer.AsmWrite('"');
  869. writer.AsmWrite(target_info.newline);
  870. inc(counter,line_length);
  871. end; { end for j:=0 ... }
  872. { do last line of lines }
  873. if counter<tai_string(hp).len then
  874. writer.AsmWrite(#9#9'DB'#9);
  875. quoted:=false;
  876. for i:=counter to tai_string(hp).len-1 do
  877. begin
  878. { it is an ascii character. }
  879. if (ord(tai_string(hp).str[i])>31) and
  880. (ord(tai_string(hp).str[i])<128) and
  881. (tai_string(hp).str[i]<>'"') then
  882. begin
  883. if not(quoted) then
  884. begin
  885. if i>counter then
  886. writer.AsmWrite(',');
  887. writer.AsmWrite('"');
  888. end;
  889. writer.AsmWrite(tai_string(hp).str[i]);
  890. quoted:=true;
  891. end { if > 31 and < 128 and " }
  892. else
  893. begin
  894. if quoted then
  895. writer.AsmWrite('"');
  896. if i>counter then
  897. writer.AsmWrite(',');
  898. quoted:=false;
  899. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  900. end;
  901. end; { end for i:=0 to... }
  902. if quoted then
  903. writer.AsmWrite('"');
  904. end;
  905. writer.AsmLn;
  906. end;
  907. ait_label :
  908. begin
  909. if tai_label(hp).labsym.is_used then
  910. begin
  911. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  912. begin
  913. writer.AsmWrite(#9'GLOBAL ');
  914. writer.AsmWriteLn(tai_label(hp).labsym.name);
  915. end;
  916. writer.AsmWriteLn(tai_label(hp).labsym.name+':');
  917. end;
  918. if SmartAsm then
  919. AddSymbol(tai_label(hp).labsym.name,true);
  920. end;
  921. ait_symbol :
  922. begin
  923. if tai_symbol(hp).has_value then
  924. internalerror(2009090803);
  925. if tai_symbol(hp).is_global or SmartAsm then
  926. begin
  927. writer.AsmWrite(#9'GLOBAL ');
  928. writer.AsmWriteLn(tai_symbol(hp).sym.name);
  929. end;
  930. writer.AsmWrite(tai_symbol(hp).sym.name);
  931. if SmartAsm then
  932. AddSymbol(tai_symbol(hp).sym.name,true);
  933. if (not assigned(hp.next)) or (assigned(hp.next) and not(tai(hp.next).typ in
  934. [ait_const,ait_realconst,ait_string])) then
  935. writer.AsmWriteLn(':')
  936. end;
  937. ait_symbol_end : ;
  938. ait_instruction :
  939. begin
  940. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  941. { We need intel order, no At&t }
  942. taicpu(hp).SetOperandOrder(op_intel);
  943. { LOCK must be on same line as opcode }
  944. if (taicpu(hp).ops = 0) and
  945. (fixed_opcode = A_LOCK) then
  946. SkipNewLine:=true
  947. else
  948. SkipNewLine:=false;
  949. s:='';
  950. if ((fixed_opcode=A_FADDP) or
  951. (fixed_opcode=A_FMULP))
  952. and (taicpu(hp).ops=0) then
  953. begin
  954. taicpu(hp).allocate_oper(2);
  955. taicpu(hp).oper[0]^.typ:=top_reg;
  956. taicpu(hp).oper[0]^.reg:=NR_ST1;
  957. taicpu(hp).oper[1]^.typ:=top_reg;
  958. taicpu(hp).oper[1]^.reg:=NR_ST;
  959. end;
  960. { NASM only accepts move for loading of
  961. simple symbol address }
  962. if ((taicpu(hp).opcode=A_LEA) and
  963. (taicpu(hp).ops=2) and
  964. (taicpu(hp).oper[0]^.typ=top_reg) and
  965. (reg2opsize(taicpu(hp).oper[0]^.reg) in [S_NO,S_Q]) and
  966. (taicpu(hp).oper[1]^.typ=top_ref) and
  967. (taicpu(hp).oper[1]^.ref^.refaddr<>addr_no) and
  968. assigned(taicpu(hp).oper[1]^.ref^.symbol) and
  969. (taicpu(hp).oper[1]^.ref^.base=NR_NO)) then
  970. begin
  971. writer.AsmWrite(asminfo^.comment);
  972. writer.AsmWriteln('Converting LEA to MOV instruction');
  973. taicpu(hp).opcode:=A_MOV;
  974. end;
  975. if fixed_opcode=A_FWAIT then
  976. writer.AsmWriteln(#9#9'DB'#9'09bh')
  977. else if (fixed_opcode=A_XLAT) and (taicpu(hp).ops=1) and
  978. (taicpu(hp).oper[0]^.typ=top_ref) then
  979. begin
  980. writer.AsmWrite(#9#9);
  981. if (taicpu(hp).oper[0]^.ref^.segment<>NR_NO) and
  982. (taicpu(hp).oper[0]^.ref^.segment<>NR_DS) then
  983. writer.AsmWrite(std_regname(taicpu(hp).oper[0]^.ref^.segment)+' ');
  984. case get_ref_address_size(taicpu(hp).oper[0]^.ref^) of
  985. 16:
  986. writer.AsmWrite('a16 ');
  987. 32:
  988. writer.AsmWrite('a32 ');
  989. 64:
  990. writer.AsmWrite('a64 ');
  991. end;
  992. writer.AsmWriteLn('xlatb');
  993. end
  994. else if is_x86_parameterized_string_op(fixed_opcode) then
  995. begin
  996. writer.AsmWrite(#9#9);
  997. i:=get_x86_string_op_si_param(fixed_opcode);
  998. if (i<>-1) and (taicpu(hp).oper[i]^.typ=top_ref) and
  999. (taicpu(hp).oper[i]^.ref^.segment<>NR_NO) and
  1000. (taicpu(hp).oper[i]^.ref^.segment<>NR_DS) then
  1001. writer.AsmWrite(std_regname(taicpu(hp).oper[i]^.ref^.segment)+' ');
  1002. for i:=0 to taicpu(hp).ops-1 do
  1003. if taicpu(hp).oper[i]^.typ=top_ref then
  1004. begin
  1005. case get_ref_address_size(taicpu(hp).oper[i]^.ref^) of
  1006. 16:
  1007. writer.AsmWrite('a16 ');
  1008. 32:
  1009. writer.AsmWrite('a32 ');
  1010. 64:
  1011. writer.AsmWrite('a64 ');
  1012. end;
  1013. break;
  1014. end;
  1015. writer.AsmWrite(std_op2str[fixed_opcode]);
  1016. case taicpu(hp).opsize of
  1017. S_B:
  1018. writer.AsmWrite('b');
  1019. S_W:
  1020. writer.AsmWrite('w');
  1021. S_L:
  1022. writer.AsmWrite('d');
  1023. S_Q:
  1024. writer.AsmWrite('q');
  1025. else
  1026. internalerror(2017101101);
  1027. end;
  1028. writer.AsmLn;
  1029. end
  1030. else
  1031. begin
  1032. prefix:='';
  1033. {$ifndef i8086}
  1034. { We need to explicitely set
  1035. word prefix to get selectors
  1036. to be pushed in 2 bytes PM }
  1037. if (taicpu(hp).opsize=S_W) and
  1038. ((fixed_opcode=A_PUSH) or
  1039. (fixed_opcode=A_POP)) and
  1040. (taicpu(hp).oper[0]^.typ=top_reg) and
  1041. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  1042. writer.AsmWriteln(#9#9'DB'#9'066h');
  1043. {$endif not i8086}
  1044. if (fixed_opcode=A_RETW) or (fixed_opcode=A_RETNW) or (fixed_opcode=A_RETFW) or
  1045. {$ifdef x86_64}
  1046. (fixed_opcode=A_RETQ) or (fixed_opcode=A_RETNQ) or (fixed_opcode=A_RETFQ) or
  1047. {$else x86_64}
  1048. (fixed_opcode=A_RETD) or (fixed_opcode=A_RETND) or
  1049. {$endif x86_64}
  1050. (fixed_opcode=A_RETFD) then
  1051. begin
  1052. case fixed_opcode of
  1053. A_RETW:
  1054. writer.AsmWrite(#9#9'o16 ret');
  1055. A_RETNW:
  1056. writer.AsmWrite(#9#9'o16 retn');
  1057. A_RETFW:
  1058. writer.AsmWrite(#9#9'o16 retf');
  1059. {$ifdef x86_64}
  1060. A_RETQ,
  1061. A_RETNQ:
  1062. writer.AsmWrite(#9#9'ret');
  1063. A_RETFQ:
  1064. writer.AsmWrite(#9#9'o64 retf');
  1065. {$else x86_64}
  1066. A_RETD:
  1067. writer.AsmWrite(#9#9'o32 ret');
  1068. A_RETND:
  1069. writer.AsmWrite(#9#9'o32 retn');
  1070. {$endif x86_64}
  1071. A_RETFD:
  1072. writer.AsmWrite(#9#9'o32 retf');
  1073. else
  1074. internalerror(2017111001);
  1075. end;
  1076. end
  1077. else if (fixed_opcode=A_SEGCS) or (fixed_opcode=A_SEGDS) or
  1078. (fixed_opcode=A_SEGSS) or (fixed_opcode=A_SEGES) or
  1079. (fixed_opcode=A_SEGFS) or (fixed_opcode=A_SEGGS) then
  1080. begin
  1081. case fixed_opcode of
  1082. A_SEGCS:
  1083. writer.AsmWrite(#9#9'cs');
  1084. A_SEGDS:
  1085. writer.AsmWrite(#9#9'ds');
  1086. A_SEGSS:
  1087. writer.AsmWrite(#9#9'ss');
  1088. A_SEGES:
  1089. writer.AsmWrite(#9#9'es');
  1090. A_SEGFS:
  1091. writer.AsmWrite(#9#9'fs');
  1092. A_SEGGS:
  1093. writer.AsmWrite(#9#9'gs');
  1094. else
  1095. internalerror(2018020101);
  1096. end;
  1097. end
  1098. else
  1099. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  1100. if taicpu(hp).ops<>0 then
  1101. begin
  1102. if is_calljmp(fixed_opcode) then
  1103. begin
  1104. writer.AsmWrite(#9);
  1105. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp));
  1106. end
  1107. else
  1108. begin
  1109. for i:=0 to taicpu(hp).ops-1 do
  1110. begin
  1111. if i=0 then
  1112. writer.AsmWrite(#9)
  1113. else
  1114. writer.AsmWrite(',');
  1115. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  1116. end;
  1117. end;
  1118. end;
  1119. if not SkipNewLine then
  1120. writer.AsmLn;
  1121. end;
  1122. end;
  1123. ait_stab,
  1124. ait_force_line,
  1125. ait_function_name : ;
  1126. ait_cutobject :
  1127. begin
  1128. if SmartAsm then
  1129. begin
  1130. { only reset buffer if nothing has changed }
  1131. if not writer.ClearIfEmpty then
  1132. begin
  1133. if SmartAsm then
  1134. begin
  1135. WriteSmartExternals;
  1136. FreeExternChainList;
  1137. end;
  1138. WriteGroups;
  1139. writer.AsmClose;
  1140. DoAssemble;
  1141. writer.AsmCreate(tai_cutobject(hp).place);
  1142. ResetSectionsList;
  1143. WriteHeader;
  1144. end;
  1145. { avoid empty files }
  1146. LastSecType:=sec_none;
  1147. LastSecName:='';
  1148. LastAlign:=4;
  1149. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1150. begin
  1151. if tai(hp.next).typ=ait_section then
  1152. begin
  1153. LastSecType:=tai_section(hp.next).sectype;
  1154. LastSecName:=tai_section(hp.next).name^;
  1155. LastAlign:=tai_section(hp.next).secalign;
  1156. end;
  1157. hp:=tai(hp.next);
  1158. end;
  1159. if LastSecType<>sec_none then
  1160. WriteSection(LastSecType,LastSecName,LastAlign);
  1161. writer.MarkEmpty;
  1162. NewObject:=true;
  1163. end;
  1164. end;
  1165. ait_marker :
  1166. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1167. inc(InlineLevel)
  1168. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1169. dec(InlineLevel);
  1170. ait_directive :
  1171. begin
  1172. case tai_directive(hp).directive of
  1173. asd_nasm_import,
  1174. asd_extern :
  1175. begin
  1176. case tai_directive(hp).directive of
  1177. asd_nasm_import :
  1178. writer.AsmWrite('import ');
  1179. asd_extern :
  1180. writer.AsmWrite('EXTERN ');
  1181. else
  1182. internalerror(200509191);
  1183. end;
  1184. if tai_directive(hp).name<>'' then
  1185. begin
  1186. if SmartAsm then
  1187. AddSymbol(tai_directive(hp).name,false);
  1188. writer.AsmWrite(tai_directive(hp).name);
  1189. end;
  1190. end;
  1191. asd_cpu :
  1192. begin
  1193. writer.AsmWrite('CPU ');
  1194. for cpu:=low(tcputype) to high(tcputype) do
  1195. begin
  1196. if tai_directive(hp).name=CPUTypeStr[CPU] then
  1197. begin
  1198. writer.AsmWriteLn(nasm_cpu_name[cpu]);
  1199. break;
  1200. end;
  1201. end;
  1202. end;
  1203. {$ifdef OMFOBJSUPPORT}
  1204. asd_omf_linnum_line :
  1205. writer.AsmWriteLn('; OMF LINNUM Line '+tai_directive(hp).name);
  1206. {$endif OMFOBJSUPPORT}
  1207. else
  1208. internalerror(200509191);
  1209. end;
  1210. writer.AsmLn;
  1211. end;
  1212. ait_seh_directive :
  1213. { Ignore for now };
  1214. ait_varloc:
  1215. begin
  1216. if tai_varloc(hp).newlocationhi<>NR_NO then
  1217. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1218. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1219. else
  1220. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1221. std_regname(tai_varloc(hp).newlocation));
  1222. end;
  1223. else
  1224. internalerror(10000);
  1225. end;
  1226. hp:=tai(hp.next);
  1227. end;
  1228. end;
  1229. procedure TX86NasmAssembler.WriteExternals;
  1230. var
  1231. sym : TAsmSymbol;
  1232. i : longint;
  1233. begin
  1234. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1235. begin
  1236. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1237. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  1238. writer.AsmWriteln('EXTERN'#9+sym.name);
  1239. end;
  1240. end;
  1241. procedure TX86NasmAssembler.WriteSmartExternals;
  1242. var
  1243. EC : PExternChain;
  1244. begin
  1245. EC:=FEC;
  1246. while assigned(EC) do
  1247. begin
  1248. if not EC^.is_defined then
  1249. writer.AsmWriteln('EXTERN'#9+EC^.psym^);
  1250. EC:=EC^.next;
  1251. end;
  1252. end;
  1253. procedure TX86NasmAssembler.WriteHeader;
  1254. begin
  1255. {$if defined(i8086)}
  1256. writer.AsmWriteLn('BITS 16');
  1257. {$elseif defined(i386)}
  1258. writer.AsmWriteLn('BITS 32');
  1259. using_relative:=false;
  1260. {$elseif defined(x86_64)}
  1261. writer.AsmWriteLn('BITS 64');
  1262. writer.AsmWriteLn('default rel');
  1263. using_relative:=true;
  1264. {$endif}
  1265. writer.AsmWriteLn('CPU '+nasm_cpu_name[current_settings.cputype]);
  1266. end;
  1267. procedure TX86NasmAssembler.WriteAsmList;
  1268. var
  1269. hal : tasmlisttype;
  1270. begin
  1271. {$ifdef EXTDEBUG}
  1272. if current_module.mainsource<>'' then
  1273. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1274. {$endif}
  1275. ResetSectionsList;
  1276. WriteHeader;
  1277. writer.AsmLn;
  1278. WriteExternals;
  1279. for hal:=low(TasmlistType) to high(TasmlistType) do
  1280. begin
  1281. if not (current_asmdata.asmlists[hal].empty) then
  1282. begin
  1283. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1284. writetree(current_asmdata.asmlists[hal]);
  1285. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1286. end;
  1287. end;
  1288. writer.AsmLn;
  1289. if SmartAsm then
  1290. begin
  1291. WriteSmartExternals;
  1292. FreeExternChainList;
  1293. end;
  1294. WriteGroups;
  1295. {$ifdef EXTDEBUG}
  1296. if current_module.mainsource<>'' then
  1297. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1298. {$endif EXTDEBUG}
  1299. end;
  1300. function TX86NasmAssembler.MakeCmdLine: TCmdStr;
  1301. var
  1302. FormatName : string;
  1303. begin
  1304. result:=Inherited MakeCmdLine;
  1305. {$ifdef i8086}
  1306. case target_info.system of
  1307. system_i8086_msdos,
  1308. system_i8086_win16,
  1309. system_i8086_embedded:
  1310. begin
  1311. FormatName:='obj';
  1312. if (cs_debuginfo in current_settings.moduleswitches) or
  1313. (cs_asm_source in current_settings.globalswitches) then
  1314. Replace(result,'$DEBUG','-g')
  1315. else
  1316. Replace(result,'$DEBUG','');
  1317. end
  1318. else
  1319. internalerror(2014082060);
  1320. end;
  1321. {$endif i8086}
  1322. {$ifdef i386}
  1323. case target_info.system of
  1324. system_i386_go32v2:
  1325. FormatName:='coff';
  1326. system_i386_wdosx,
  1327. system_i386_win32:
  1328. FormatName:='win32';
  1329. system_i386_embedded:
  1330. FormatName:='obj';
  1331. system_i386_linux,
  1332. system_i386_beos:
  1333. FormatName:='elf';
  1334. system_i386_darwin:
  1335. FormatName:='macho32';
  1336. else
  1337. FormatName:='elf';
  1338. end;
  1339. {$endif i386}
  1340. {$ifdef x86_64}
  1341. case target_info.system of
  1342. system_x86_64_win64:
  1343. FormatName:='win64';
  1344. system_x86_64_darwin:
  1345. FormatName:='macho64';
  1346. system_x86_64_embedded:
  1347. FormatName:='obj';
  1348. system_x86_64_linux:
  1349. FormatName:='elf64';
  1350. else
  1351. FormatName:='elf64';
  1352. end;
  1353. {$endif x86_64}
  1354. Replace(result,'$FORMAT',FormatName);
  1355. end;
  1356. {*****************************************************************************
  1357. Initialize
  1358. *****************************************************************************}
  1359. {$ifdef i8086}
  1360. const
  1361. as_i8086_nasm_info : tasminfo =
  1362. (
  1363. id : as_i8086_nasm;
  1364. idtxt : 'NASM';
  1365. asmbin : 'nasm';
  1366. asmcmd : '-f $FORMAT $DEBUG -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1367. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1368. flags : [af_needar,af_no_debug];
  1369. labelprefix : '..@';
  1370. comment : '; ';
  1371. dollarsign: '$';
  1372. );
  1373. as_i8086_nasmobj_info : tasminfo =
  1374. (
  1375. id : as_i8086_nasmobj;
  1376. idtxt : 'NASMOBJ';
  1377. asmbin : 'nasm';
  1378. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1379. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1380. flags : [af_needar,af_no_debug];
  1381. labelprefix : '..@';
  1382. comment : '; ';
  1383. dollarsign: '$';
  1384. );
  1385. {$endif i8086}
  1386. {$ifdef i386}
  1387. const
  1388. as_i386_nasmcoff_info : tasminfo =
  1389. (
  1390. id : as_i386_nasmcoff;
  1391. idtxt : 'NASMCOFF';
  1392. asmbin : 'nasm';
  1393. asmcmd : '-f coff -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1394. supported_targets : [system_i386_go32v2];
  1395. flags : [af_needar,af_no_debug];
  1396. labelprefix : '..@';
  1397. comment : '; ';
  1398. dollarsign: '$';
  1399. );
  1400. as_i386_nasmwin32_info : tasminfo =
  1401. (
  1402. id : as_i386_nasmwin32;
  1403. idtxt : 'NASMWIN32';
  1404. asmbin : 'nasm';
  1405. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1406. supported_targets : [system_i386_win32];
  1407. flags : [af_needar,af_no_debug,af_smartlink_sections];
  1408. labelprefix : '..@';
  1409. comment : '; ';
  1410. dollarsign: '$';
  1411. );
  1412. as_i386_nasmobj_info : tasminfo =
  1413. (
  1414. id : as_i386_nasmobj;
  1415. idtxt : 'NASMOBJ';
  1416. asmbin : 'nasm';
  1417. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1418. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1419. flags : [af_needar,af_no_debug];
  1420. labelprefix : '..@';
  1421. comment : '; ';
  1422. dollarsign: '$';
  1423. );
  1424. as_i386_nasmwdosx_info : tasminfo =
  1425. (
  1426. id : as_i386_nasmwdosx;
  1427. idtxt : 'NASMWDOSX';
  1428. asmbin : 'nasm';
  1429. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1430. supported_targets : [system_i386_wdosx];
  1431. flags : [af_needar,af_no_debug];
  1432. labelprefix : '..@';
  1433. comment : '; ';
  1434. dollarsign: '$';
  1435. );
  1436. as_i386_nasmelf_info : tasminfo =
  1437. (
  1438. id : as_i386_nasmelf;
  1439. idtxt : 'NASMELF';
  1440. asmbin : 'nasm';
  1441. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1442. supported_targets : [system_i386_linux];
  1443. flags : [af_needar,af_no_debug];
  1444. labelprefix : '..@';
  1445. comment : '; ';
  1446. dollarsign: '$';
  1447. );
  1448. {
  1449. as_i386_nasmdarwin_info : tasminfo =
  1450. (
  1451. id : as_i386_nasmdarwin;
  1452. idtxt : 'NASMDARWIN';
  1453. asmbin : 'nasm';
  1454. asmcmd : '-f macho32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1455. supported_targets : [system_i386_darwin];
  1456. flags : [af_needar,af_no_debug];
  1457. labelprefix : '..@';
  1458. comment : '; ';
  1459. dollarsign: '$';
  1460. );
  1461. }
  1462. as_i386_nasmbeos_info : tasminfo =
  1463. (
  1464. id : as_i386_nasmbeos;
  1465. idtxt : 'NASMELF';
  1466. asmbin : 'nasm';
  1467. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1468. supported_targets : [system_i386_beos];
  1469. flags : [af_needar,af_no_debug];
  1470. labelprefix : '..@';
  1471. comment : '; ';
  1472. dollarsign: '$';
  1473. );
  1474. as_i386_nasmhaiku_info : tasminfo =
  1475. (
  1476. id : as_i386_nasmhaiku;
  1477. idtxt : 'NASMELF';
  1478. asmbin : 'nasm';
  1479. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1480. supported_targets : [system_i386_haiku];
  1481. flags : [af_needar,af_no_debug];
  1482. labelprefix : '..@';
  1483. comment : '; ';
  1484. dollarsign: '$';
  1485. );
  1486. as_i386_nasm_info : tasminfo =
  1487. (
  1488. id : as_i386_nasm;
  1489. idtxt : 'NASM';
  1490. asmbin : 'nasm';
  1491. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1492. supported_targets : [system_any];
  1493. flags : [af_needar,af_no_debug];
  1494. labelprefix : '..@';
  1495. comment : '; ';
  1496. dollarsign: '$';
  1497. );
  1498. {$endif i386}
  1499. {$ifdef x86_64}
  1500. const
  1501. as_x86_64_nasm_info : tasminfo =
  1502. (
  1503. id : as_x86_64_nasm;
  1504. idtxt : 'NASM';
  1505. asmbin : 'nasm';
  1506. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1507. supported_targets : [system_any];
  1508. flags : [af_needar{,af_no_debug}];
  1509. labelprefix : '..@';
  1510. comment : '; ';
  1511. dollarsign: '$';
  1512. );
  1513. as_x86_64_nasmwin64_info : tasminfo =
  1514. (
  1515. id : as_x86_64_nasmwin64;
  1516. idtxt : 'NASMWIN64';
  1517. asmbin : 'nasm';
  1518. asmcmd : '-f win64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1519. supported_targets : [system_x86_64_win64];
  1520. flags : [af_needar,af_no_debug];
  1521. labelprefix : '..@';
  1522. comment : '; ';
  1523. dollarsign: '$';
  1524. );
  1525. as_x86_64_nasmelf_info : tasminfo =
  1526. (
  1527. id : as_x86_64_nasmelf;
  1528. idtxt : 'NASMELF';
  1529. asmbin : 'nasm';
  1530. asmcmd : '-f elf64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1531. supported_targets : [system_x86_64_linux];
  1532. flags : [af_needar,af_no_debug];
  1533. labelprefix : '..@';
  1534. comment : '; ';
  1535. dollarsign: '$';
  1536. );
  1537. as_x86_64_nasmdarwin_info : tasminfo =
  1538. (
  1539. id : as_x86_64_nasmdarwin;
  1540. idtxt : 'NASMDARWIN';
  1541. asmbin : 'nasm';
  1542. asmcmd : '-f macho64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1543. supported_targets : [system_x86_64_darwin];
  1544. flags : [af_needar,af_no_debug];
  1545. labelprefix : '..@';
  1546. comment : '; ';
  1547. dollarsign: '$';
  1548. );
  1549. {$endif x86_64}
  1550. initialization
  1551. {$ifdef i8086}
  1552. RegisterAssembler(as_i8086_nasm_info,TX86NasmAssembler);
  1553. RegisterAssembler(as_i8086_nasmobj_info,TX86NasmAssembler);
  1554. {$endif i8086}
  1555. {$ifdef i386}
  1556. RegisterAssembler(as_i386_nasmcoff_info,TX86NasmAssembler);
  1557. RegisterAssembler(as_i386_nasmwin32_info,TX86NasmAssembler);
  1558. RegisterAssembler(as_i386_nasmwdosx_info,TX86NasmAssembler);
  1559. RegisterAssembler(as_i386_nasmobj_info,TX86NasmAssembler);
  1560. RegisterAssembler(as_i386_nasmbeos_info,TX86NasmAssembler);
  1561. RegisterAssembler(as_i386_nasmhaiku_info,TX86NasmAssembler);
  1562. RegisterAssembler(as_i386_nasmelf_info,TX86NasmAssembler);
  1563. RegisterAssembler(as_i386_nasm_info,TX86NasmAssembler);
  1564. {$endif i386}
  1565. {$ifdef x86_64}
  1566. RegisterAssembler(as_x86_64_nasm_info,TX86NasmAssembler);
  1567. RegisterAssembler(as_x86_64_nasmwin64_info,TX86NasmAssembler);
  1568. RegisterAssembler(as_x86_64_nasmelf_info,TX86NasmAssembler);
  1569. RegisterAssembler(as_x86_64_nasmdarwin_info,TX86NasmAssembler);
  1570. {$endif x86_64}
  1571. end.