agx86nsm.pas 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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,
  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:=aname + '_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(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(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(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_user) then
  520. writer.AsmWrite(aname)
  521. else if (atype=sec_threadvar) and
  522. (target_info.system in (systems_windows+systems_wince)) then
  523. writer.AsmWrite('.tls'#9'bss')
  524. else if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
  525. begin
  526. 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 secnames[atype]='.text' then
  552. writer.AsmWrite(CodeSectionName(aname))
  553. else
  554. writer.AsmWrite(secnames[atype]);
  555. if create_smartlink_sections and
  556. (atype<>sec_bss) and
  557. (aname<>'') then
  558. begin
  559. writer.AsmWrite('.');
  560. writer.AsmWrite(aname);
  561. if atype in [sec_init, sec_fini, sec_stub, sec_code] then
  562. writer.AsmWrite(' code align='+tostr(alignment))
  563. else if atype in [sec_rodata, sec_rodata_norel] then
  564. writer.AsmWrite(' rdata align='+tostr(alignment))
  565. else
  566. writer.AsmWrite(' data align='+tostr(alignment))
  567. end;
  568. writer.AsmLn;
  569. LastSecType:=atype;
  570. end;
  571. procedure TX86NasmAssembler.WriteHiddenSymbolAttribute(sym: TAsmSymbol);
  572. begin
  573. if target_info.system in systems_windows then
  574. exit;
  575. if target_info.system in systems_darwin then
  576. writer.AsmWrite(':private_extern')
  577. else
  578. { no colon }
  579. writer.AsmWrite(' hidden')
  580. end;
  581. procedure TX86NasmAssembler.ResetSectionsList;
  582. begin
  583. FSections.Free;
  584. FSections:=TFPHashObjectList.Create;
  585. FGroups.Free;
  586. FGroups:=TFPHashObjectList.Create;
  587. end;
  588. procedure TX86NasmAssembler.AddGroup(const grpname: string);
  589. begin
  590. if FGroups.Find(grpname)=nil then
  591. TX86NasmGroup.Create(FGroups,grpname);
  592. end;
  593. procedure TX86NasmAssembler.AddSegmentToGroup(const grpname, segname: string);
  594. var
  595. grp: TX86NasmGroup;
  596. begin
  597. grp:=TX86NasmGroup(FGroups.Find(grpname));
  598. if grp=nil then
  599. grp:=TX86NasmGroup.Create(FGroups,grpname);
  600. TX86NasmSection.Create(grp.Sections,segname);
  601. end;
  602. procedure TX86NasmAssembler.WriteGroup(data: TObject; arg: pointer);
  603. var
  604. grp: TX86NasmGroup;
  605. i: Integer;
  606. begin
  607. grp:=TX86NasmGroup(data);
  608. writer.AsmWrite('GROUP '+grp.Name);
  609. for i:=0 to grp.Sections.Count-1 do
  610. writer.AsmWrite(' '+grp.Sections.NameOfIndex(i));
  611. writer.AsmLn;
  612. end;
  613. procedure TX86NasmAssembler.WriteGroups;
  614. {$ifdef i8086}
  615. var
  616. i: Integer;
  617. {$endif i8086}
  618. begin
  619. {$ifdef i8086}
  620. if target_info.system in [system_i8086_msdos,system_i8086_win16,system_i8086_embedded] then
  621. begin
  622. if current_settings.x86memorymodel=mm_huge then
  623. WriteSection(sec_data,'',2);
  624. writer.AsmLn;
  625. FGroups.ForEachCall(@WriteGroup,nil);
  626. end;
  627. {$endif i8086}
  628. end;
  629. procedure TX86NasmAssembler.WriteTree(p:TAsmList);
  630. {$ifdef cpuextended}
  631. type
  632. t80bitarray = array[0..9] of byte;
  633. {$endif cpuextended}
  634. var
  635. s : string;
  636. hp : tai;
  637. counter,
  638. lines,
  639. i,j,l : longint;
  640. InlineLevel : longint;
  641. consttype : taiconst_type;
  642. do_line, SkipNewLine,
  643. quoted : boolean;
  644. fixed_opcode: TAsmOp;
  645. prefix, LastSecName : string;
  646. LastAlign : LongInt;
  647. cpu: tcputype;
  648. prevfileinfo : tfileposinfo;
  649. previnfile : tinputfile;
  650. NewObject : boolean;
  651. begin
  652. if not assigned(p) then
  653. exit;
  654. InlineLevel:=0;
  655. NewObject:=true;
  656. { lineinfo is only needed for al_procedures (PFV) }
  657. do_line:=(cs_asm_source in current_settings.globalswitches) or
  658. ((cs_lineinfo in current_settings.moduleswitches)
  659. and (p=current_asmdata.asmlists[al_procedures]));
  660. hp:=tai(p.first);
  661. while assigned(hp) do
  662. begin
  663. prefetch(pointer(hp.next)^);
  664. if not(hp.typ in SkipLineInfo) then
  665. begin
  666. previnfile:=lastinfile;
  667. prevfileinfo:=lastfileinfo;
  668. current_filepos:=tailineinfo(hp).fileinfo;
  669. { no line info for inlined code }
  670. if do_line and (inlinelevel=0) then
  671. WriteSourceLine(hp as tailineinfo);
  672. if (lastfileinfo.line<>prevfileinfo.line) or
  673. (previnfile<>lastinfile) then
  674. begin
  675. { +0 postfix means no line increment per assembler instruction }
  676. writer.AsmWrite('%LINE '+tostr(current_filepos.line)+'+0');
  677. if assigned(lastinfile) and ((previnfile<>lastinfile) or NewObject) then
  678. writer.AsmWriteLn(' '+lastinfile.name)
  679. else
  680. writer.AsmLn;
  681. NewObject:=false;
  682. end;
  683. end;
  684. case hp.typ of
  685. ait_comment :
  686. Begin
  687. writer.AsmWrite(asminfo^.comment);
  688. writer.AsmWritePChar(tai_comment(hp).str);
  689. writer.AsmLn;
  690. End;
  691. ait_regalloc :
  692. begin
  693. if (cs_asm_regalloc in current_settings.globalswitches) then
  694. writer.AsmWriteLn(#9#9+asminfo^.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  695. regallocstr[tai_regalloc(hp).ratype]);
  696. end;
  697. ait_tempalloc :
  698. begin
  699. if (cs_asm_tempalloc in current_settings.globalswitches) then
  700. WriteTempalloc(tai_tempalloc(hp));
  701. end;
  702. ait_section :
  703. begin
  704. if tai_section(hp).sectype<>sec_none then
  705. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secalign);
  706. LastSecType:=tai_section(hp).sectype;
  707. end;
  708. ait_align :
  709. begin
  710. if (tai_align(hp).aligntype>1) then
  711. begin
  712. if (LastSecType=sec_bss) or (
  713. (LastSecType=sec_threadvar) and
  714. (target_info.system in (systems_windows+systems_wince))
  715. ) then
  716. writer.AsmWriteLn(#9'ALIGNB '+tostr(tai_align(hp).aligntype))
  717. else if tai_align_abstract(hp).use_op then
  718. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB '+tostr(tai_align_abstract(hp).fillop))
  719. else if LastSecType in [sec_code,sec_stub,sec_init,sec_fini] then
  720. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype))
  721. else
  722. writer.AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype)+',DB 0');
  723. end;
  724. end;
  725. ait_datablock :
  726. begin
  727. if tai_datablock(hp).is_global or SmartAsm then
  728. begin
  729. writer.AsmWrite(#9'GLOBAL ');
  730. writer.AsmWrite(tai_datablock(hp).sym.name);
  731. if tai_datablock(hp).sym.bind=AB_PRIVATE_EXTERN then
  732. WriteHiddenSymbolAttribute(tai_datablock(hp).sym);
  733. end;
  734. writer.AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  735. if SmartAsm then
  736. AddSymbol(tai_datablock(hp).sym.name,true);
  737. writer.AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  738. end;
  739. ait_const:
  740. begin
  741. consttype:=tai_const(hp).consttype;
  742. case consttype of
  743. aitconst_uleb128bit,
  744. aitconst_sleb128bit,
  745. aitconst_128bit:
  746. begin
  747. writer.AsmWriteLn(asminfo^.comment+'Unsupported const type '+
  748. ait_const2str[consttype]);
  749. end;
  750. {$ifdef i8086}
  751. aitconst_farptr:
  752. begin
  753. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  754. if assigned(tai_const(hp).sym) then
  755. begin
  756. if SmartAsm then
  757. AddSymbol(tai_const(hp).sym.name,false);
  758. writer.AsmWrite(tai_const(hp).sym.name);
  759. if tai_const(hp).value<>0 then
  760. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  761. writer.AsmLn;
  762. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  763. writer.AsmWrite('SEG ');
  764. writer.AsmWrite(tai_const(hp).sym.name);
  765. end
  766. else
  767. writer.AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
  768. tostr(hi(longint(tai_const(hp).value))));
  769. writer.AsmLn;
  770. end;
  771. aitconst_seg:
  772. begin
  773. writer.AsmWrite(ait_const2str[aitconst_16bit]);
  774. if assigned(tai_const(hp).sym) then
  775. begin
  776. if SmartAsm then
  777. AddSymbol(tai_const(hp).sym.name,false);
  778. writer.AsmWrite('SEG ');
  779. writer.AsmWrite(tai_const(hp).sym.name);
  780. end
  781. else
  782. internalerror(2015110501);
  783. writer.AsmLn;
  784. end;
  785. aitconst_dgroup:
  786. writer.AsmWriteLn(#9'DW'#9'DGROUP');
  787. aitconst_fardataseg:
  788. writer.AsmWriteLn(#9'DW'#9+current_module.modulename^+'_DATA');
  789. {$endif i8086}
  790. {$ifdef x86_64}
  791. aitconst_rva_symbol,
  792. aitconst_secrel32_symbol: ;
  793. {$endif x86_64}
  794. {$ifdef i386}
  795. aitconst_rva_symbol,
  796. aitconst_secrel32_symbol,
  797. {$endif i386}
  798. aitconst_64bit,
  799. aitconst_32bit,
  800. aitconst_16bit,
  801. aitconst_8bit,
  802. aitconst_16bit_unaligned,
  803. aitconst_32bit_unaligned,
  804. aitconst_64bit_unaligned:
  805. begin
  806. writer.AsmWrite(ait_const2str[tai_const(hp).consttype]);
  807. l:=0;
  808. repeat
  809. if assigned(tai_const(hp).sym) then
  810. begin
  811. if SmartAsm then
  812. begin
  813. AddSymbol(tai_const(hp).sym.name,false);
  814. if assigned(tai_const(hp).endsym) then
  815. AddSymbol(tai_const(hp).endsym.name,false);
  816. end;
  817. if assigned(tai_const(hp).endsym) then
  818. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  819. else
  820. s:=tai_const(hp).sym.name;
  821. if tai_const(hp).value<>0 then
  822. s:=s+tostr_with_plus(tai_const(hp).value);
  823. end
  824. else
  825. s:=tostr(tai_const(hp).value);
  826. writer.AsmWrite(s);
  827. inc(l,length(s));
  828. if (l>line_length) or
  829. (hp.next=nil) or
  830. (tai(hp.next).typ<>ait_const) or
  831. (tai_const(hp.next).consttype<>consttype) then
  832. break;
  833. hp:=tai(hp.next);
  834. writer.AsmWrite(',');
  835. until false;
  836. writer.AsmLn;
  837. end;
  838. else
  839. internalerror(200704252);
  840. end;
  841. end;
  842. ait_realconst:
  843. begin
  844. WriteRealConstAsBytes(tai_realconst(hp),#9#9'DB'#9,do_line);
  845. end;
  846. ait_string :
  847. begin
  848. counter := 0;
  849. lines := tai_string(hp).len div line_length;
  850. { separate lines in different parts }
  851. if tai_string(hp).len > 0 then
  852. Begin
  853. for j := 0 to lines-1 do
  854. begin
  855. writer.AsmWrite(#9#9'DB'#9);
  856. quoted:=false;
  857. for i:=counter to counter+line_length-1 do
  858. begin
  859. { it is an ascii character. }
  860. if (ord(tai_string(hp).str[i])>31) and
  861. (ord(tai_string(hp).str[i])<128) and
  862. (tai_string(hp).str[i]<>'"') then
  863. begin
  864. if not(quoted) then
  865. begin
  866. if i>counter then
  867. writer.AsmWrite(',');
  868. writer.AsmWrite('"');
  869. end;
  870. writer.AsmWrite(tai_string(hp).str[i]);
  871. quoted:=true;
  872. end { if > 31 and < 128 and ord('"') }
  873. else
  874. begin
  875. if quoted then
  876. writer.AsmWrite('"');
  877. if i>counter then
  878. writer.AsmWrite(',');
  879. quoted:=false;
  880. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  881. end;
  882. end; { end for i:=0 to... }
  883. if quoted then writer.AsmWrite('"');
  884. writer.AsmWrite(target_info.newline);
  885. inc(counter,line_length);
  886. end; { end for j:=0 ... }
  887. { do last line of lines }
  888. if counter<tai_string(hp).len then
  889. writer.AsmWrite(#9#9'DB'#9);
  890. quoted:=false;
  891. for i:=counter to tai_string(hp).len-1 do
  892. begin
  893. { it is an ascii character. }
  894. if (ord(tai_string(hp).str[i])>31) and
  895. (ord(tai_string(hp).str[i])<128) and
  896. (tai_string(hp).str[i]<>'"') then
  897. begin
  898. if not(quoted) then
  899. begin
  900. if i>counter then
  901. writer.AsmWrite(',');
  902. writer.AsmWrite('"');
  903. end;
  904. writer.AsmWrite(tai_string(hp).str[i]);
  905. quoted:=true;
  906. end { if > 31 and < 128 and " }
  907. else
  908. begin
  909. if quoted then
  910. writer.AsmWrite('"');
  911. if i>counter then
  912. writer.AsmWrite(',');
  913. quoted:=false;
  914. writer.AsmWrite(tostr(ord(tai_string(hp).str[i])));
  915. end;
  916. end; { end for i:=0 to... }
  917. if quoted then
  918. writer.AsmWrite('"');
  919. end;
  920. writer.AsmLn;
  921. end;
  922. ait_label :
  923. begin
  924. if tai_label(hp).labsym.is_used then
  925. begin
  926. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  927. begin
  928. writer.AsmWrite(#9'GLOBAL ');
  929. writer.AsmWriteLn(tai_label(hp).labsym.name);
  930. end;
  931. writer.AsmWriteLn(tai_label(hp).labsym.name+':');
  932. end;
  933. if SmartAsm then
  934. AddSymbol(tai_label(hp).labsym.name,true);
  935. end;
  936. ait_symbol :
  937. begin
  938. if tai_symbol(hp).has_value then
  939. internalerror(2009090803);
  940. if tai_symbol(hp).is_global or SmartAsm then
  941. begin
  942. writer.AsmWrite(#9'GLOBAL ');
  943. writer.AsmWriteLn(tai_symbol(hp).sym.name);
  944. if tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN then
  945. WriteHiddenSymbolAttribute(tai_symbol(hp).sym);
  946. end;
  947. writer.AsmWrite(tai_symbol(hp).sym.name);
  948. if SmartAsm then
  949. AddSymbol(tai_symbol(hp).sym.name,true);
  950. if (not assigned(hp.next)) or (assigned(hp.next) and not(tai(hp.next).typ in
  951. [ait_const,ait_realconst,ait_string])) then
  952. writer.AsmWriteLn(':')
  953. end;
  954. ait_symbol_end : ;
  955. ait_instruction :
  956. begin
  957. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  958. { We need intel order, no At&t }
  959. taicpu(hp).SetOperandOrder(op_intel);
  960. { LOCK must be on same line as opcode }
  961. if (taicpu(hp).ops = 0) and
  962. (fixed_opcode = A_LOCK) then
  963. SkipNewLine:=true
  964. else
  965. SkipNewLine:=false;
  966. s:='';
  967. if ((fixed_opcode=A_FADDP) or
  968. (fixed_opcode=A_FMULP))
  969. and (taicpu(hp).ops=0) then
  970. begin
  971. taicpu(hp).allocate_oper(2);
  972. taicpu(hp).oper[0]^.typ:=top_reg;
  973. taicpu(hp).oper[0]^.reg:=NR_ST1;
  974. taicpu(hp).oper[1]^.typ:=top_reg;
  975. taicpu(hp).oper[1]^.reg:=NR_ST;
  976. end;
  977. { NASM only accepts move for loading of
  978. simple symbol address }
  979. if ((taicpu(hp).opcode=A_LEA) and
  980. (taicpu(hp).ops=2) and
  981. (taicpu(hp).oper[0]^.typ=top_reg) and
  982. (reg2opsize(taicpu(hp).oper[0]^.reg) in [S_NO,S_Q]) and
  983. (taicpu(hp).oper[1]^.typ=top_ref) and
  984. (taicpu(hp).oper[1]^.ref^.refaddr<>addr_no) and
  985. assigned(taicpu(hp).oper[1]^.ref^.symbol) and
  986. (taicpu(hp).oper[1]^.ref^.base=NR_NO)) then
  987. begin
  988. writer.AsmWrite(asminfo^.comment);
  989. writer.AsmWriteln('Converting LEA to MOV instruction');
  990. taicpu(hp).opcode:=A_MOV;
  991. end;
  992. if fixed_opcode=A_FWAIT then
  993. writer.AsmWriteln(#9#9'DB'#9'09bh')
  994. else if (fixed_opcode=A_XLAT) and (taicpu(hp).ops=1) and
  995. (taicpu(hp).oper[0]^.typ=top_ref) then
  996. begin
  997. writer.AsmWrite(#9#9);
  998. if (taicpu(hp).oper[0]^.ref^.segment<>NR_NO) and
  999. (taicpu(hp).oper[0]^.ref^.segment<>NR_DS) then
  1000. writer.AsmWrite(std_regname(taicpu(hp).oper[0]^.ref^.segment)+' ');
  1001. case get_ref_address_size(taicpu(hp).oper[0]^.ref^) of
  1002. 16:
  1003. writer.AsmWrite('a16 ');
  1004. 32:
  1005. writer.AsmWrite('a32 ');
  1006. 64:
  1007. writer.AsmWrite('a64 ');
  1008. end;
  1009. writer.AsmWriteLn('xlatb');
  1010. end
  1011. else if is_x86_parameterized_string_op(fixed_opcode) then
  1012. begin
  1013. writer.AsmWrite(#9#9);
  1014. i:=get_x86_string_op_si_param(fixed_opcode);
  1015. if (i<>-1) and (taicpu(hp).oper[i]^.typ=top_ref) and
  1016. (taicpu(hp).oper[i]^.ref^.segment<>NR_NO) and
  1017. (taicpu(hp).oper[i]^.ref^.segment<>NR_DS) then
  1018. writer.AsmWrite(std_regname(taicpu(hp).oper[i]^.ref^.segment)+' ');
  1019. for i:=0 to taicpu(hp).ops-1 do
  1020. if taicpu(hp).oper[i]^.typ=top_ref then
  1021. begin
  1022. case get_ref_address_size(taicpu(hp).oper[i]^.ref^) of
  1023. 16:
  1024. writer.AsmWrite('a16 ');
  1025. 32:
  1026. writer.AsmWrite('a32 ');
  1027. 64:
  1028. writer.AsmWrite('a64 ');
  1029. end;
  1030. break;
  1031. end;
  1032. writer.AsmWrite(std_op2str[fixed_opcode]);
  1033. case taicpu(hp).opsize of
  1034. S_B:
  1035. writer.AsmWrite('b');
  1036. S_W:
  1037. writer.AsmWrite('w');
  1038. S_L:
  1039. writer.AsmWrite('d');
  1040. S_Q:
  1041. writer.AsmWrite('q');
  1042. else
  1043. internalerror(2017101101);
  1044. end;
  1045. writer.AsmLn;
  1046. end
  1047. else
  1048. begin
  1049. prefix:='';
  1050. {$ifndef i8086}
  1051. { We need to explicitely set
  1052. word prefix to get selectors
  1053. to be pushed in 2 bytes PM }
  1054. if (taicpu(hp).opsize=S_W) and
  1055. ((fixed_opcode=A_PUSH) or
  1056. (fixed_opcode=A_POP)) and
  1057. (taicpu(hp).oper[0]^.typ=top_reg) and
  1058. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  1059. writer.AsmWriteln(#9#9'DB'#9'066h');
  1060. {$endif not i8086}
  1061. if (fixed_opcode=A_RETW) or (fixed_opcode=A_RETNW) or (fixed_opcode=A_RETFW) or
  1062. {$ifdef x86_64}
  1063. (fixed_opcode=A_RETQ) or (fixed_opcode=A_RETNQ) or (fixed_opcode=A_RETFQ) or
  1064. {$else x86_64}
  1065. (fixed_opcode=A_RETD) or (fixed_opcode=A_RETND) or
  1066. {$endif x86_64}
  1067. (fixed_opcode=A_RETFD) then
  1068. begin
  1069. case fixed_opcode of
  1070. A_RETW:
  1071. writer.AsmWrite(#9#9'o16 ret');
  1072. A_RETNW:
  1073. writer.AsmWrite(#9#9'o16 retn');
  1074. A_RETFW:
  1075. writer.AsmWrite(#9#9'o16 retf');
  1076. {$ifdef x86_64}
  1077. A_RETQ,
  1078. A_RETNQ:
  1079. writer.AsmWrite(#9#9'ret');
  1080. A_RETFQ:
  1081. writer.AsmWrite(#9#9'o64 retf');
  1082. {$else x86_64}
  1083. A_RETD:
  1084. writer.AsmWrite(#9#9'o32 ret');
  1085. A_RETND:
  1086. writer.AsmWrite(#9#9'o32 retn');
  1087. {$endif x86_64}
  1088. A_RETFD:
  1089. writer.AsmWrite(#9#9'o32 retf');
  1090. else
  1091. internalerror(2017111001);
  1092. end;
  1093. end
  1094. else if (fixed_opcode=A_SEGCS) or (fixed_opcode=A_SEGDS) or
  1095. (fixed_opcode=A_SEGSS) or (fixed_opcode=A_SEGES) or
  1096. (fixed_opcode=A_SEGFS) or (fixed_opcode=A_SEGGS) then
  1097. begin
  1098. case fixed_opcode of
  1099. A_SEGCS:
  1100. writer.AsmWrite(#9#9'cs');
  1101. A_SEGDS:
  1102. writer.AsmWrite(#9#9'ds');
  1103. A_SEGSS:
  1104. writer.AsmWrite(#9#9'ss');
  1105. A_SEGES:
  1106. writer.AsmWrite(#9#9'es');
  1107. A_SEGFS:
  1108. writer.AsmWrite(#9#9'fs');
  1109. A_SEGGS:
  1110. writer.AsmWrite(#9#9'gs');
  1111. else
  1112. internalerror(2018020101);
  1113. end;
  1114. end
  1115. else
  1116. writer.AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  1117. if taicpu(hp).ops<>0 then
  1118. begin
  1119. if is_calljmp(fixed_opcode) then
  1120. begin
  1121. writer.AsmWrite(#9);
  1122. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp));
  1123. end
  1124. else
  1125. begin
  1126. for i:=0 to taicpu(hp).ops-1 do
  1127. begin
  1128. if i=0 then
  1129. writer.AsmWrite(#9)
  1130. else
  1131. writer.AsmWrite(',');
  1132. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  1133. end;
  1134. end;
  1135. end;
  1136. if not SkipNewLine then
  1137. writer.AsmLn;
  1138. end;
  1139. end;
  1140. ait_stab,
  1141. ait_force_line,
  1142. ait_function_name : ;
  1143. ait_cutobject :
  1144. begin
  1145. if SmartAsm then
  1146. begin
  1147. { only reset buffer if nothing has changed }
  1148. if not writer.ClearIfEmpty then
  1149. begin
  1150. if SmartAsm then
  1151. begin
  1152. WriteSmartExternals;
  1153. FreeExternChainList;
  1154. end;
  1155. WriteGroups;
  1156. writer.AsmClose;
  1157. DoAssemble;
  1158. writer.AsmCreate(tai_cutobject(hp).place);
  1159. ResetSectionsList;
  1160. WriteHeader;
  1161. end;
  1162. { avoid empty files }
  1163. LastSecType:=sec_none;
  1164. LastSecName:='';
  1165. LastAlign:=4;
  1166. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1167. begin
  1168. if tai(hp.next).typ=ait_section then
  1169. begin
  1170. LastSecType:=tai_section(hp.next).sectype;
  1171. LastSecName:=tai_section(hp.next).name^;
  1172. LastAlign:=tai_section(hp.next).secalign;
  1173. end;
  1174. hp:=tai(hp.next);
  1175. end;
  1176. if LastSecType<>sec_none then
  1177. WriteSection(LastSecType,LastSecName,LastAlign);
  1178. writer.MarkEmpty;
  1179. NewObject:=true;
  1180. end;
  1181. end;
  1182. ait_marker :
  1183. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1184. inc(InlineLevel)
  1185. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1186. dec(InlineLevel);
  1187. ait_directive :
  1188. begin
  1189. case tai_directive(hp).directive of
  1190. asd_nasm_import,
  1191. asd_extern :
  1192. begin
  1193. case tai_directive(hp).directive of
  1194. asd_nasm_import :
  1195. writer.AsmWrite('import ');
  1196. asd_extern :
  1197. writer.AsmWrite('EXTERN ');
  1198. else
  1199. internalerror(200509191);
  1200. end;
  1201. if tai_directive(hp).name<>'' then
  1202. begin
  1203. if SmartAsm then
  1204. AddSymbol(tai_directive(hp).name,false);
  1205. writer.AsmWrite(tai_directive(hp).name);
  1206. end;
  1207. end;
  1208. asd_cpu :
  1209. begin
  1210. writer.AsmWrite('CPU ');
  1211. for cpu:=low(tcputype) to high(tcputype) do
  1212. begin
  1213. if tai_directive(hp).name=CPUTypeStr[CPU] then
  1214. begin
  1215. writer.AsmWriteLn(nasm_cpu_name[cpu]);
  1216. break;
  1217. end;
  1218. end;
  1219. end;
  1220. {$ifdef OMFOBJSUPPORT}
  1221. asd_omf_linnum_line :
  1222. writer.AsmWriteLn('; OMF LINNUM Line '+tai_directive(hp).name);
  1223. {$endif OMFOBJSUPPORT}
  1224. else
  1225. internalerror(200509191);
  1226. end;
  1227. writer.AsmLn;
  1228. end;
  1229. ait_seh_directive :
  1230. { Ignore for now };
  1231. ait_varloc:
  1232. begin
  1233. if tai_varloc(hp).newlocationhi<>NR_NO then
  1234. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1235. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1236. else
  1237. writer.AsmWriteLn(asminfo^.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1238. std_regname(tai_varloc(hp).newlocation));
  1239. end;
  1240. else
  1241. internalerror(10000);
  1242. end;
  1243. hp:=tai(hp.next);
  1244. end;
  1245. end;
  1246. procedure TX86NasmAssembler.WriteExternals;
  1247. var
  1248. sym : TAsmSymbol;
  1249. i : longint;
  1250. begin
  1251. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1252. begin
  1253. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1254. if sym.bind in [AB_EXTERNAL,AB_EXTERNAL_INDIRECT] then
  1255. writer.AsmWriteln('EXTERN'#9+sym.name);
  1256. end;
  1257. end;
  1258. procedure TX86NasmAssembler.WriteSmartExternals;
  1259. var
  1260. EC : PExternChain;
  1261. begin
  1262. EC:=FEC;
  1263. while assigned(EC) do
  1264. begin
  1265. if not EC^.is_defined then
  1266. writer.AsmWriteln('EXTERN'#9+EC^.psym^);
  1267. EC:=EC^.next;
  1268. end;
  1269. end;
  1270. procedure TX86NasmAssembler.WriteHeader;
  1271. begin
  1272. {$if defined(i8086)}
  1273. writer.AsmWriteLn('BITS 16');
  1274. {$elseif defined(i386)}
  1275. writer.AsmWriteLn('BITS 32');
  1276. using_relative:=false;
  1277. {$elseif defined(x86_64)}
  1278. writer.AsmWriteLn('BITS 64');
  1279. writer.AsmWriteLn('default rel');
  1280. using_relative:=true;
  1281. {$endif}
  1282. writer.AsmWriteLn('CPU '+nasm_cpu_name[current_settings.cputype]);
  1283. end;
  1284. procedure TX86NasmAssembler.WriteAsmList;
  1285. var
  1286. hal : tasmlisttype;
  1287. begin
  1288. {$ifdef EXTDEBUG}
  1289. if current_module.mainsource<>'' then
  1290. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1291. {$endif}
  1292. ResetSectionsList;
  1293. WriteHeader;
  1294. writer.AsmLn;
  1295. WriteExternals;
  1296. for hal:=low(TasmlistType) to high(TasmlistType) do
  1297. begin
  1298. if not (current_asmdata.asmlists[hal].empty) then
  1299. begin
  1300. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1301. writetree(current_asmdata.asmlists[hal]);
  1302. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmListTypeStr[hal]);
  1303. end;
  1304. end;
  1305. writer.AsmLn;
  1306. if SmartAsm then
  1307. begin
  1308. WriteSmartExternals;
  1309. FreeExternChainList;
  1310. end;
  1311. WriteGroups;
  1312. {$ifdef EXTDEBUG}
  1313. if current_module.mainsource<>'' then
  1314. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1315. {$endif EXTDEBUG}
  1316. end;
  1317. function TX86NasmAssembler.MakeCmdLine: TCmdStr;
  1318. var
  1319. FormatName : string;
  1320. begin
  1321. result:=Inherited MakeCmdLine;
  1322. {$ifdef i8086}
  1323. case target_info.system of
  1324. system_i8086_msdos,
  1325. system_i8086_win16,
  1326. system_i8086_embedded:
  1327. begin
  1328. FormatName:='obj';
  1329. if (cs_debuginfo in current_settings.moduleswitches) or
  1330. (cs_asm_source in current_settings.globalswitches) then
  1331. Replace(result,'$DEBUG','-g')
  1332. else
  1333. Replace(result,'$DEBUG','');
  1334. end
  1335. else
  1336. internalerror(2014082060);
  1337. end;
  1338. {$endif i8086}
  1339. {$ifdef i386}
  1340. case target_info.system of
  1341. system_i386_go32v2:
  1342. FormatName:='coff';
  1343. system_i386_wdosx,
  1344. system_i386_win32:
  1345. FormatName:='win32';
  1346. system_i386_embedded:
  1347. FormatName:='obj';
  1348. system_i386_linux,
  1349. system_i386_beos:
  1350. FormatName:='elf';
  1351. system_i386_darwin:
  1352. FormatName:='macho32';
  1353. else
  1354. FormatName:='elf';
  1355. end;
  1356. {$endif i386}
  1357. {$ifdef x86_64}
  1358. case target_info.system of
  1359. system_x86_64_win64:
  1360. FormatName:='win64';
  1361. system_x86_64_darwin:
  1362. FormatName:='macho64';
  1363. system_x86_64_embedded:
  1364. FormatName:='obj';
  1365. system_x86_64_linux:
  1366. FormatName:='elf64';
  1367. else
  1368. FormatName:='elf64';
  1369. end;
  1370. {$endif x86_64}
  1371. Replace(result,'$FORMAT',FormatName);
  1372. end;
  1373. {*****************************************************************************
  1374. Initialize
  1375. *****************************************************************************}
  1376. {$ifdef i8086}
  1377. const
  1378. as_i8086_nasm_info : tasminfo =
  1379. (
  1380. id : as_i8086_nasm;
  1381. idtxt : 'NASM';
  1382. asmbin : 'nasm';
  1383. asmcmd : '-f $FORMAT $DEBUG -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1384. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1385. flags : [af_needar,af_no_debug];
  1386. labelprefix : '..@';
  1387. comment : '; ';
  1388. dollarsign: '$';
  1389. );
  1390. as_i8086_nasmobj_info : tasminfo =
  1391. (
  1392. id : as_i8086_nasmobj;
  1393. idtxt : 'NASMOBJ';
  1394. asmbin : 'nasm';
  1395. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1396. supported_targets : [system_i8086_msdos,system_i8086_win16,system_i8086_embedded];
  1397. flags : [af_needar,af_no_debug];
  1398. labelprefix : '..@';
  1399. comment : '; ';
  1400. dollarsign: '$';
  1401. );
  1402. {$endif i8086}
  1403. {$ifdef i386}
  1404. const
  1405. as_i386_nasmcoff_info : tasminfo =
  1406. (
  1407. id : as_i386_nasmcoff;
  1408. idtxt : 'NASMCOFF';
  1409. asmbin : 'nasm';
  1410. asmcmd : '-f coff -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1411. supported_targets : [system_i386_go32v2];
  1412. flags : [af_needar,af_no_debug];
  1413. labelprefix : '..@';
  1414. comment : '; ';
  1415. dollarsign: '$';
  1416. );
  1417. as_i386_nasmwin32_info : tasminfo =
  1418. (
  1419. id : as_i386_nasmwin32;
  1420. idtxt : 'NASMWIN32';
  1421. asmbin : 'nasm';
  1422. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1423. supported_targets : [system_i386_win32];
  1424. flags : [af_needar,af_no_debug,af_smartlink_sections];
  1425. labelprefix : '..@';
  1426. comment : '; ';
  1427. dollarsign: '$';
  1428. );
  1429. as_i386_nasmobj_info : tasminfo =
  1430. (
  1431. id : as_i386_nasmobj;
  1432. idtxt : 'NASMOBJ';
  1433. asmbin : 'nasm';
  1434. asmcmd : '-f obj -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1435. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1436. flags : [af_needar,af_no_debug];
  1437. labelprefix : '..@';
  1438. comment : '; ';
  1439. dollarsign: '$';
  1440. );
  1441. as_i386_nasmwdosx_info : tasminfo =
  1442. (
  1443. id : as_i386_nasmwdosx;
  1444. idtxt : 'NASMWDOSX';
  1445. asmbin : 'nasm';
  1446. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1447. supported_targets : [system_i386_wdosx];
  1448. flags : [af_needar,af_no_debug];
  1449. labelprefix : '..@';
  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. comment : '; ';
  1463. dollarsign: '$';
  1464. );
  1465. {
  1466. as_i386_nasmdarwin_info : tasminfo =
  1467. (
  1468. id : as_i386_nasmdarwin;
  1469. idtxt : 'NASMDARWIN';
  1470. asmbin : 'nasm';
  1471. asmcmd : '-f macho32 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1472. supported_targets : [system_i386_darwin];
  1473. flags : [af_needar,af_no_debug];
  1474. labelprefix : '..@';
  1475. comment : '; ';
  1476. dollarsign: '$';
  1477. );
  1478. }
  1479. as_i386_nasmbeos_info : tasminfo =
  1480. (
  1481. id : as_i386_nasmbeos;
  1482. idtxt : 'NASMELF';
  1483. asmbin : 'nasm';
  1484. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1485. supported_targets : [system_i386_beos];
  1486. flags : [af_needar,af_no_debug];
  1487. labelprefix : '..@';
  1488. comment : '; ';
  1489. dollarsign: '$';
  1490. );
  1491. as_i386_nasmhaiku_info : tasminfo =
  1492. (
  1493. id : as_i386_nasmhaiku;
  1494. idtxt : 'NASMELF';
  1495. asmbin : 'nasm';
  1496. asmcmd : '-f elf -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1497. supported_targets : [system_i386_haiku];
  1498. flags : [af_needar,af_no_debug];
  1499. labelprefix : '..@';
  1500. comment : '; ';
  1501. dollarsign: '$';
  1502. );
  1503. as_i386_nasm_info : tasminfo =
  1504. (
  1505. id : as_i386_nasm;
  1506. idtxt : 'NASM';
  1507. asmbin : 'nasm';
  1508. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1509. supported_targets : [system_any];
  1510. flags : [af_needar,af_no_debug];
  1511. labelprefix : '..@';
  1512. comment : '; ';
  1513. dollarsign: '$';
  1514. );
  1515. {$endif i386}
  1516. {$ifdef x86_64}
  1517. const
  1518. as_x86_64_nasm_info : tasminfo =
  1519. (
  1520. id : as_x86_64_nasm;
  1521. idtxt : 'NASM';
  1522. asmbin : 'nasm';
  1523. asmcmd : '-f $FORMAT -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1524. supported_targets : [system_any];
  1525. flags : [af_needar{,af_no_debug}];
  1526. labelprefix : '..@';
  1527. comment : '; ';
  1528. dollarsign: '$';
  1529. );
  1530. as_x86_64_nasmwin64_info : tasminfo =
  1531. (
  1532. id : as_x86_64_nasmwin64;
  1533. idtxt : 'NASMWIN64';
  1534. asmbin : 'nasm';
  1535. asmcmd : '-f win64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1536. supported_targets : [system_x86_64_win64];
  1537. flags : [af_needar,af_no_debug];
  1538. labelprefix : '..@';
  1539. comment : '; ';
  1540. dollarsign: '$';
  1541. );
  1542. as_x86_64_nasmelf_info : tasminfo =
  1543. (
  1544. id : as_x86_64_nasmelf;
  1545. idtxt : 'NASMELF';
  1546. asmbin : 'nasm';
  1547. asmcmd : '-f elf64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1548. supported_targets : [system_x86_64_linux];
  1549. flags : [af_needar,af_no_debug];
  1550. labelprefix : '..@';
  1551. comment : '; ';
  1552. dollarsign: '$';
  1553. );
  1554. as_x86_64_nasmdarwin_info : tasminfo =
  1555. (
  1556. id : as_x86_64_nasmdarwin;
  1557. idtxt : 'NASMDARWIN';
  1558. asmbin : 'nasm';
  1559. asmcmd : '-f macho64 -o $OBJ -w-orphan-labels $EXTRAOPT $ASM';
  1560. supported_targets : [system_x86_64_darwin];
  1561. flags : [af_needar,af_no_debug];
  1562. labelprefix : '..@';
  1563. comment : '; ';
  1564. dollarsign: '$';
  1565. );
  1566. {$endif x86_64}
  1567. initialization
  1568. {$ifdef i8086}
  1569. RegisterAssembler(as_i8086_nasm_info,TX86NasmAssembler);
  1570. RegisterAssembler(as_i8086_nasmobj_info,TX86NasmAssembler);
  1571. {$endif i8086}
  1572. {$ifdef i386}
  1573. RegisterAssembler(as_i386_nasmcoff_info,TX86NasmAssembler);
  1574. RegisterAssembler(as_i386_nasmwin32_info,TX86NasmAssembler);
  1575. RegisterAssembler(as_i386_nasmwdosx_info,TX86NasmAssembler);
  1576. RegisterAssembler(as_i386_nasmobj_info,TX86NasmAssembler);
  1577. RegisterAssembler(as_i386_nasmbeos_info,TX86NasmAssembler);
  1578. RegisterAssembler(as_i386_nasmhaiku_info,TX86NasmAssembler);
  1579. RegisterAssembler(as_i386_nasmelf_info,TX86NasmAssembler);
  1580. RegisterAssembler(as_i386_nasm_info,TX86NasmAssembler);
  1581. {$endif i386}
  1582. {$ifdef x86_64}
  1583. RegisterAssembler(as_x86_64_nasm_info,TX86NasmAssembler);
  1584. RegisterAssembler(as_x86_64_nasmwin64_info,TX86NasmAssembler);
  1585. RegisterAssembler(as_x86_64_nasmelf_info,TX86NasmAssembler);
  1586. RegisterAssembler(as_x86_64_nasmdarwin_info,TX86NasmAssembler);
  1587. {$endif x86_64}
  1588. end.