agx86nsm.pas 59 KB

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