agx86nsm.pas 58 KB

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