agx86nsm.pas 51 KB

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