agx86nsm.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. cpubase,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. { T386NasmAssembler }
  26. T386NasmAssembler = class(texternalassembler)
  27. private
  28. function CodeSectionName: string;
  29. procedure WriteReference(var ref : treference);
  30. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  31. procedure WriteOper_jmp(const o:toper; op : tasmop);
  32. procedure WriteSection(atype:TAsmSectiontype;const aname:string);
  33. public
  34. procedure WriteTree(p:TAsmList);override;
  35. procedure WriteAsmList;override;
  36. procedure WriteExternals;
  37. procedure WriteSmartExternals;
  38. procedure WriteHeader;
  39. end;
  40. implementation
  41. uses
  42. cutils,globtype,globals,systems,cclasses,
  43. fmodule,finput,verbose,cpuinfo,cgbase
  44. ;
  45. type
  46. {$ifdef cpuextended}
  47. t80bitarray = array[0..9] of byte;
  48. {$endif cpuextended}
  49. t64bitarray = array[0..7] of byte;
  50. t32bitarray = array[0..3] of byte;
  51. const
  52. line_length = 64;
  53. nasm_regname_table : array[tregisterindex] of string[7] = (
  54. {r386nasm.inc contains the Nasm name of each register.}
  55. {$if defined(x86_64)}
  56. {$fatal nasm support not yet implemented for x86_64 }
  57. {$elseif defined(i386)}
  58. {$i r386nasm.inc}
  59. {$elseif defined(i8086)}
  60. {$i r8086nasm.inc}
  61. {$endif}
  62. );
  63. function nasm_regname(r:Tregister):string;
  64. var
  65. p : tregisterindex;
  66. begin
  67. p:=findreg_by_number(r);
  68. if p<>0 then
  69. result:=nasm_regname_table[p]
  70. else
  71. result:=generic_regname(r);
  72. end;
  73. function single2str(d : single) : string;
  74. var
  75. hs : string;
  76. p : longint;
  77. begin
  78. str(d,hs);
  79. { nasm expects a lowercase e }
  80. p:=pos('E',hs);
  81. if p>0 then
  82. hs[p]:='e';
  83. p:=pos('+',hs);
  84. if p>0 then
  85. delete(hs,p,1);
  86. single2str:=lower(hs);
  87. end;
  88. function double2str(d : double) : string;
  89. var
  90. hs : string;
  91. p : longint;
  92. begin
  93. str(d,hs);
  94. { nasm expects a lowercase e }
  95. p:=pos('E',hs);
  96. if p>0 then
  97. hs[p]:='e';
  98. p:=pos('+',hs);
  99. if p>0 then
  100. delete(hs,p,1);
  101. double2str:=lower(hs);
  102. end;
  103. function extended2str(e : extended) : string;
  104. var
  105. hs : string;
  106. p : longint;
  107. begin
  108. str(e,hs);
  109. { nasm expects a lowercase e }
  110. p:=pos('E',hs);
  111. if p>0 then
  112. hs[p]:='e';
  113. p:=pos('+',hs);
  114. if p>0 then
  115. delete(hs,p,1);
  116. extended2str:=lower(hs);
  117. end;
  118. { convert floating point values }
  119. { to correct endian }
  120. procedure swap64bitarray(var t: t64bitarray);
  121. var
  122. b: byte;
  123. begin
  124. b:= t[7];
  125. t[7] := t[0];
  126. t[0] := b;
  127. b := t[6];
  128. t[6] := t[1];
  129. t[1] := b;
  130. b:= t[5];
  131. t[5] := t[2];
  132. t[2] := b;
  133. b:= t[4];
  134. t[4] := t[3];
  135. t[3] := b;
  136. end;
  137. procedure swap32bitarray(var t: t32bitarray);
  138. var
  139. b: byte;
  140. begin
  141. b:= t[1];
  142. t[1]:= t[2];
  143. t[2]:= b;
  144. b:= t[0];
  145. t[0]:= t[3];
  146. t[3]:= b;
  147. end;
  148. function comp2str(d : bestreal) : string;
  149. type
  150. pdouble = ^double;
  151. var
  152. c : comp;
  153. dd : pdouble;
  154. begin
  155. c:=comp(d);
  156. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  157. comp2str:=double2str(dd^);
  158. end;
  159. function sizestr(s:topsize;dest:boolean):string;
  160. begin
  161. case s of
  162. S_B : sizestr:='byte ';
  163. S_W : sizestr:='word ';
  164. S_L : sizestr:='dword ';
  165. S_Q : sizestr:='qword ';
  166. S_IS : sizestr:='word ';
  167. S_IL : sizestr:='dword ';
  168. S_IQ : sizestr:='qword ';
  169. S_FS : sizestr:='dword ';
  170. S_FL : sizestr:='qword ';
  171. S_FX : sizestr:='tword ';
  172. S_BW : if dest then
  173. sizestr:='word '
  174. else
  175. sizestr:='byte ';
  176. S_BL : if dest then
  177. sizestr:='dword '
  178. else
  179. sizestr:='byte ';
  180. S_WL : if dest then
  181. sizestr:='dword '
  182. else
  183. sizestr:='word ';
  184. else { S_NO }
  185. sizestr:='';
  186. end;
  187. end;
  188. Function PadTabs(const p:string;addch:char):string;
  189. var
  190. s : string;
  191. i : longint;
  192. begin
  193. i:=length(p);
  194. if addch<>#0 then
  195. begin
  196. inc(i);
  197. s:=p+addch;
  198. end
  199. else
  200. s:=p;
  201. if i<8 then
  202. PadTabs:=s+#9#9
  203. else
  204. PadTabs:=s+#9;
  205. end;
  206. type
  207. PExternChain = ^TExternChain;
  208. TExternChain = Record
  209. psym : pshortstring;
  210. is_defined : boolean;
  211. next : PExternChain;
  212. end;
  213. const
  214. FEC : PExternChain = nil;
  215. procedure AddSymbol(symname : string; defined : boolean);
  216. var
  217. EC : PExternChain;
  218. begin
  219. EC:=FEC;
  220. while assigned(EC) do
  221. begin
  222. if EC^.psym^=symname then
  223. begin
  224. if defined then
  225. EC^.is_defined:=true;
  226. exit;
  227. end;
  228. EC:=EC^.next;
  229. end;
  230. New(EC);
  231. EC^.next:=FEC;
  232. FEC:=EC;
  233. FEC^.psym:=stringdup(symname);
  234. FEC^.is_defined := defined;
  235. end;
  236. procedure FreeExternChainList;
  237. var
  238. EC : PExternChain;
  239. begin
  240. EC:=FEC;
  241. while assigned(EC) do
  242. begin
  243. FEC:=EC^.next;
  244. stringdispose(EC^.psym);
  245. Dispose(EC);
  246. EC:=FEC;
  247. end;
  248. end;
  249. {****************************************************************************
  250. T386NasmAssembler
  251. ****************************************************************************}
  252. function T386NasmAssembler.CodeSectionName: string;
  253. begin
  254. {$ifdef i8086}
  255. if current_settings.x86memorymodel in x86_far_code_models then
  256. result:=current_module.modulename^ + '_TEXT'
  257. else
  258. {$endif}
  259. result:='.text';
  260. end;
  261. procedure T386NasmAssembler.WriteReference(var ref : treference);
  262. var
  263. first : boolean;
  264. begin
  265. with ref do
  266. begin
  267. AsmWrite('[');
  268. first:=true;
  269. if (segment<>NR_NO) then
  270. AsmWrite(nasm_regname(segment)+':');
  271. if assigned(symbol) then
  272. begin
  273. AsmWrite(symbol.name);
  274. if SmartAsm then
  275. AddSymbol(symbol.name,false);
  276. first:=false;
  277. end;
  278. if (base<>NR_NO) then
  279. begin
  280. if not(first) then
  281. AsmWrite('+')
  282. else
  283. first:=false;
  284. AsmWrite(nasm_regname(base))
  285. end;
  286. if (index<>NR_NO) then
  287. begin
  288. if not(first) then
  289. AsmWrite('+')
  290. else
  291. first:=false;
  292. AsmWrite(nasm_regname(index));
  293. if scalefactor<>0 then
  294. AsmWrite('*'+tostr(scalefactor));
  295. end;
  296. if offset<0 then
  297. begin
  298. AsmWrite(tostr(offset));
  299. first:=false;
  300. end
  301. else if (offset>0) then
  302. begin
  303. AsmWrite('+'+tostr(offset));
  304. first:=false;
  305. end;
  306. if first then
  307. AsmWrite('0');
  308. AsmWrite(']');
  309. end;
  310. end;
  311. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  312. begin
  313. case o.typ of
  314. top_reg :
  315. AsmWrite(nasm_regname(o.reg));
  316. top_const :
  317. begin
  318. if (ops=1) and (opcode<>A_RET) then
  319. AsmWrite(sizestr(s,dest));
  320. AsmWrite(tostr(longint(o.val)));
  321. end;
  322. top_ref :
  323. begin
  324. if o.ref^.refaddr=addr_no then
  325. begin
  326. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  327. (opcode = A_LSS) or (opcode = A_LFS) or
  328. (opcode = A_LES) or (opcode = A_LDS) or
  329. // (opcode = A_SHR) or (opcode = A_SHL) or
  330. // (opcode = A_SAR) or (opcode = A_SAL) or
  331. (opcode = A_OUT) or (opcode = A_IN)) then
  332. AsmWrite(sizestr(s,dest));
  333. WriteReference(o.ref^);
  334. end
  335. {$ifdef i8086}
  336. else if o.ref^.refaddr=addr_dgroup then
  337. begin
  338. AsmWrite('dgroup');
  339. end
  340. {$endif i8086}
  341. else
  342. begin
  343. {$ifdef x86_64}
  344. asmwrite('qword ');
  345. {$endif}
  346. {$ifdef i386}
  347. asmwrite('dword ');
  348. {$endif i386}
  349. {$ifdef i8086}
  350. if o.ref^.refaddr=addr_far then
  351. asmwrite('far ')
  352. else if o.ref^.refaddr=addr_seg then
  353. asmwrite('SEG ')
  354. else
  355. asmwrite('word ');
  356. {$endif i8086}
  357. if assigned(o.ref^.symbol) then
  358. begin
  359. if SmartAsm then
  360. AddSymbol(o.ref^.symbol.name,false);
  361. asmwrite(o.ref^.symbol.name);
  362. if o.ref^.offset=0 then
  363. exit;
  364. end;
  365. if o.ref^.offset>0 then
  366. asmwrite('+');
  367. asmwrite(tostr(o.ref^.offset));
  368. end;
  369. end;
  370. else
  371. internalerror(10001);
  372. end;
  373. end;
  374. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  375. begin
  376. case o.typ of
  377. top_reg :
  378. AsmWrite(nasm_regname(o.reg));
  379. top_ref :
  380. if o.ref^.refaddr in [addr_no{$ifdef i8086},addr_far_ref{$endif}] then
  381. begin
  382. {$ifdef i8086}
  383. if o.ref^.refaddr=addr_far_ref then
  384. AsmWrite('far ');
  385. {$endif i8086}
  386. WriteReference(o.ref^);
  387. end
  388. else
  389. begin
  390. { NEAR forces NASM to emit near jumps, which are 386+ }
  391. {$ifndef i8086}
  392. if not(
  393. (op=A_JCXZ) or (op=A_JECXZ) or
  394. {$ifdef x86_64}
  395. (op=A_JRCXZ) or
  396. {$endif x86_64}
  397. (op=A_LOOP) or (op=A_LOOPE) or
  398. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  399. (op=A_LOOPZ)
  400. ) then
  401. AsmWrite('NEAR ');
  402. {$endif i8086}
  403. {$ifdef i8086}
  404. if o.ref^.refaddr=addr_far then
  405. AsmWrite('far ');
  406. {$endif i8086}
  407. AsmWrite(o.ref^.symbol.name);
  408. if SmartAsm then
  409. AddSymbol(o.ref^.symbol.name,false);
  410. if o.ref^.offset>0 then
  411. AsmWrite('+'+tostr(o.ref^.offset))
  412. else
  413. if o.ref^.offset<0 then
  414. AsmWrite(tostr(o.ref^.offset));
  415. end;
  416. top_const :
  417. AsmWrite(tostr(aint(o.val)));
  418. else
  419. internalerror(10001);
  420. end;
  421. end;
  422. const
  423. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[30]=(
  424. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  425. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  426. #9'RVA'#9,#9'SECREL32'#9,#9'FIXME_darwin_dwarf_delta64'#9,
  427. #9'FIXME_darwin_dwarf_delta32'#9,#9'FIXME_half16bit'#9,
  428. #9'DW'#9,#9'DD'#9,#9'FIXME_64BIT_UNALIGNED'#9
  429. );
  430. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  431. const
  432. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  433. '.text',
  434. '.data',
  435. '.data',
  436. '.rodata',
  437. '.bss',
  438. '.tbss',
  439. '.pdata',
  440. '.text','.data','.data','.data','.data',
  441. '.stab',
  442. '.stabstr',
  443. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  444. '.eh_frame',
  445. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  446. '.fpc',
  447. '',
  448. '.init',
  449. '.fini',
  450. '.objc_class',
  451. '.objc_meta_class',
  452. '.objc_cat_cls_meth',
  453. '.objc_cat_inst_meth',
  454. '.objc_protocol',
  455. '.objc_string_object',
  456. '.objc_cls_meth',
  457. '.objc_inst_meth',
  458. '.objc_cls_refs',
  459. '.objc_message_refs',
  460. '.objc_symbols',
  461. '.objc_category',
  462. '.objc_class_vars',
  463. '.objc_instance_vars',
  464. '.objc_module_info',
  465. '.objc_class_names',
  466. '.objc_meth_var_types',
  467. '.objc_meth_var_names',
  468. '.objc_selector_strs',
  469. '.objc_protocol_ext',
  470. '.objc_class_ext',
  471. '.objc_property',
  472. '.objc_image_info',
  473. '.objc_cstring_object',
  474. '.objc_sel_fixup',
  475. '__DATA,__objc_data',
  476. '__DATA,__objc_const',
  477. '.objc_superrefs',
  478. '__DATA, __datacoal_nt,coalesced',
  479. '.objc_classlist',
  480. '.objc_nlclasslist',
  481. '.objc_catlist',
  482. '.obcj_nlcatlist',
  483. '.objc_protolist'
  484. );
  485. begin
  486. AsmLn;
  487. AsmWrite('SECTION ');
  488. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  489. Thus, data which normally goes into .rodata and .rodata_norel sections must
  490. end up in .data section }
  491. if (atype in [sec_rodata,sec_rodata_norel]) and
  492. (target_info.system=system_i386_go32v2) then
  493. AsmWrite('.data')
  494. else if secnames[atype]='.text' then
  495. AsmWrite(CodeSectionName)
  496. else
  497. AsmWrite(secnames[atype]);
  498. if create_smartlink_sections and
  499. (atype<>sec_bss) and
  500. (aname<>'') then
  501. begin
  502. AsmWrite('.');
  503. AsmWrite(aname);
  504. end;
  505. AsmLn;
  506. LasTSecType:=atype;
  507. end;
  508. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  509. {$ifdef cpuextended}
  510. type
  511. t80bitarray = array[0..9] of byte;
  512. {$endif cpuextended}
  513. var
  514. s : string;
  515. hp : tai;
  516. counter,
  517. lines,
  518. i,j,l : longint;
  519. InlineLevel : longint;
  520. consttype : taiconst_type;
  521. do_line,
  522. quoted : boolean;
  523. co : comp;
  524. sin : single;
  525. d : double;
  526. {$ifdef cpuextended}
  527. e : extended;
  528. {$endif cpuextended}
  529. fixed_opcode: TAsmOp;
  530. begin
  531. if not assigned(p) then
  532. exit;
  533. InlineLevel:=0;
  534. { lineinfo is only needed for al_procedures (PFV) }
  535. do_line:=(cs_asm_source in current_settings.globalswitches) or
  536. ((cs_lineinfo in current_settings.moduleswitches)
  537. and (p=current_asmdata.asmlists[al_procedures]));
  538. hp:=tai(p.first);
  539. while assigned(hp) do
  540. begin
  541. prefetch(pointer(hp.next)^);
  542. if not(hp.typ in SkipLineInfo) then
  543. begin
  544. current_filepos:=tailineinfo(hp).fileinfo;
  545. { no line info for inlined code }
  546. if do_line and (inlinelevel=0) then
  547. WriteSourceLine(hp as tailineinfo);
  548. end;
  549. case hp.typ of
  550. ait_comment :
  551. Begin
  552. AsmWrite(target_asm.comment);
  553. AsmWritePChar(tai_comment(hp).str);
  554. AsmLn;
  555. End;
  556. ait_regalloc :
  557. begin
  558. if (cs_asm_regalloc in current_settings.globalswitches) then
  559. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  560. regallocstr[tai_regalloc(hp).ratype]);
  561. end;
  562. ait_tempalloc :
  563. begin
  564. if (cs_asm_tempalloc in current_settings.globalswitches) then
  565. WriteTempalloc(tai_tempalloc(hp));
  566. end;
  567. ait_section :
  568. begin
  569. if tai_section(hp).sectype<>sec_none then
  570. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  571. LasTSecType:=tai_section(hp).sectype;
  572. end;
  573. ait_align :
  574. begin
  575. { nasm gives warnings when it finds align in bss as it
  576. wants to store data }
  577. if (lastsectype<>sec_bss) and
  578. (tai_align(hp).aligntype>1) then
  579. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  580. end;
  581. ait_datablock :
  582. begin
  583. if tai_datablock(hp).is_global or SmartAsm then
  584. begin
  585. AsmWrite(#9'GLOBAL ');
  586. AsmWriteLn(tai_datablock(hp).sym.name);
  587. end;
  588. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  589. if SmartAsm then
  590. AddSymbol(tai_datablock(hp).sym.name,true);
  591. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  592. end;
  593. ait_const:
  594. begin
  595. consttype:=tai_const(hp).consttype;
  596. case consttype of
  597. aitconst_64bit,
  598. aitconst_64bit_unaligned:
  599. begin
  600. if assigned(tai_const(hp).sym) then
  601. internalerror(200404292);
  602. AsmWrite(ait_const2str[aitconst_32bit]);
  603. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  604. AsmWrite(',');
  605. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  606. AsmLn;
  607. end;
  608. aitconst_uleb128bit,
  609. aitconst_sleb128bit,
  610. aitconst_128bit:
  611. begin
  612. end;
  613. {$ifdef i8086}
  614. aitconst_farptr:
  615. begin
  616. AsmWrite(ait_const2str[aitconst_16bit]);
  617. if assigned(tai_const(hp).sym) then
  618. begin
  619. if SmartAsm then
  620. AddSymbol(tai_const(hp).sym.name,false);
  621. AsmWrite(tai_const(hp).sym.name);
  622. if tai_const(hp).value<>0 then
  623. AsmWrite(tostr_with_plus(tai_const(hp).value));
  624. AsmLn;
  625. AsmWrite(ait_const2str[aitconst_16bit]);
  626. AsmWrite('SEG ');
  627. AsmWrite(tai_const(hp).sym.name);
  628. end
  629. else
  630. AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
  631. tostr(hi(longint(tai_const(hp).value))));
  632. AsmLn;
  633. end;
  634. {$endif i8086}
  635. aitconst_32bit,
  636. aitconst_16bit,
  637. aitconst_8bit,
  638. aitconst_rva_symbol,
  639. aitconst_secrel32_symbol,
  640. aitconst_16bit_unaligned,
  641. aitconst_32bit_unaligned:
  642. begin
  643. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  644. l:=0;
  645. repeat
  646. if assigned(tai_const(hp).sym) then
  647. begin
  648. if SmartAsm then
  649. begin
  650. AddSymbol(tai_const(hp).sym.name,false);
  651. if assigned(tai_const(hp).endsym) then
  652. AddSymbol(tai_const(hp).endsym.name,false);
  653. end;
  654. if assigned(tai_const(hp).endsym) then
  655. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  656. else
  657. s:=tai_const(hp).sym.name;
  658. if tai_const(hp).value<>0 then
  659. s:=s+tostr_with_plus(tai_const(hp).value);
  660. end
  661. else
  662. s:=tostr(tai_const(hp).value);
  663. AsmWrite(s);
  664. inc(l,length(s));
  665. if (l>line_length) or
  666. (hp.next=nil) or
  667. (tai(hp.next).typ<>ait_const) or
  668. (tai_const(hp.next).consttype<>consttype) then
  669. break;
  670. hp:=tai(hp.next);
  671. AsmWrite(',');
  672. until false;
  673. AsmLn;
  674. end;
  675. else
  676. internalerror(200704252);
  677. end;
  678. end;
  679. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  680. ait_real_80bit :
  681. begin
  682. if do_line then
  683. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  684. { Make sure e is a extended type, bestreal could be
  685. a different type (bestreal) !! (PFV) }
  686. e:=tai_real_80bit(hp).value;
  687. AsmWrite(#9#9'DB'#9);
  688. for i:=0 to 9 do
  689. begin
  690. if i<>0 then
  691. AsmWrite(',');
  692. AsmWrite(tostr(t80bitarray(e)[i]));
  693. end;
  694. for i:=11 to tai_real_80bit(hp).savesize do
  695. AsmWrite(',0');
  696. AsmLn;
  697. end;
  698. {$else cpuextended}
  699. ait_real_80bit :
  700. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  701. {$endif cpuextended}
  702. // ait_real_64bit :
  703. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  704. ait_real_64bit :
  705. begin
  706. if do_line then
  707. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  708. d:=tai_real_64bit(hp).value;
  709. { swap the values to correct endian if required }
  710. if source_info.endian <> target_info.endian then
  711. swap64bitarray(t64bitarray(d));
  712. AsmWrite(#9#9'DB'#9);
  713. {$ifdef arm}
  714. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  715. begin
  716. for i:=4 to 7 do
  717. begin
  718. if i<>4 then
  719. AsmWrite(',');
  720. AsmWrite(tostr(t64bitarray(d)[i]));
  721. end;
  722. for i:=0 to 3 do
  723. begin
  724. AsmWrite(',');
  725. AsmWrite(tostr(t64bitarray(d)[i]));
  726. end;
  727. end
  728. else
  729. {$endif arm}
  730. begin
  731. for i:=0 to 7 do
  732. begin
  733. if i<>0 then
  734. AsmWrite(',');
  735. AsmWrite(tostr(t64bitarray(d)[i]));
  736. end;
  737. end;
  738. AsmLn;
  739. end;
  740. // ait_real_32bit :
  741. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  742. ait_real_32bit :
  743. begin
  744. if do_line then
  745. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  746. sin:=tai_real_32bit(hp).value;
  747. { swap the values to correct endian if required }
  748. if source_info.endian <> target_info.endian then
  749. swap32bitarray(t32bitarray(sin));
  750. AsmWrite(#9#9'DB'#9);
  751. for i:=0 to 3 do
  752. begin
  753. if i<>0 then
  754. AsmWrite(',');
  755. AsmWrite(tostr(t32bitarray(sin)[i]));
  756. end;
  757. AsmLn;
  758. end;
  759. // ait_comp_64bit :
  760. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  761. ait_comp_64bit :
  762. begin
  763. if do_line then
  764. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  765. AsmWrite(#9#9'DB'#9);
  766. co:=comp(tai_comp_64bit(hp).value);
  767. { swap the values to correct endian if required }
  768. if source_info.endian <> target_info.endian then
  769. swap64bitarray(t64bitarray(co));
  770. for i:=0 to 7 do
  771. begin
  772. if i<>0 then
  773. AsmWrite(',');
  774. AsmWrite(tostr(t64bitarray(co)[i]));
  775. end;
  776. AsmLn;
  777. end;
  778. ait_string :
  779. begin
  780. counter := 0;
  781. lines := tai_string(hp).len div line_length;
  782. { separate lines in different parts }
  783. if tai_string(hp).len > 0 then
  784. Begin
  785. for j := 0 to lines-1 do
  786. begin
  787. AsmWrite(#9#9'DB'#9);
  788. quoted:=false;
  789. for i:=counter to counter+line_length-1 do
  790. begin
  791. { it is an ascii character. }
  792. if (ord(tai_string(hp).str[i])>31) and
  793. (ord(tai_string(hp).str[i])<128) and
  794. (tai_string(hp).str[i]<>'"') then
  795. begin
  796. if not(quoted) then
  797. begin
  798. if i>counter then
  799. AsmWrite(',');
  800. AsmWrite('"');
  801. end;
  802. AsmWrite(tai_string(hp).str[i]);
  803. quoted:=true;
  804. end { if > 31 and < 128 and ord('"') }
  805. else
  806. begin
  807. if quoted then
  808. AsmWrite('"');
  809. if i>counter then
  810. AsmWrite(',');
  811. quoted:=false;
  812. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  813. end;
  814. end; { end for i:=0 to... }
  815. if quoted then AsmWrite('"');
  816. AsmWrite(target_info.newline);
  817. inc(counter,line_length);
  818. end; { end for j:=0 ... }
  819. { do last line of lines }
  820. if counter<tai_string(hp).len then
  821. AsmWrite(#9#9'DB'#9);
  822. quoted:=false;
  823. for i:=counter to tai_string(hp).len-1 do
  824. begin
  825. { it is an ascii character. }
  826. if (ord(tai_string(hp).str[i])>31) and
  827. (ord(tai_string(hp).str[i])<128) and
  828. (tai_string(hp).str[i]<>'"') then
  829. begin
  830. if not(quoted) then
  831. begin
  832. if i>counter then
  833. AsmWrite(',');
  834. AsmWrite('"');
  835. end;
  836. AsmWrite(tai_string(hp).str[i]);
  837. quoted:=true;
  838. end { if > 31 and < 128 and " }
  839. else
  840. begin
  841. if quoted then
  842. AsmWrite('"');
  843. if i>counter then
  844. AsmWrite(',');
  845. quoted:=false;
  846. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  847. end;
  848. end; { end for i:=0 to... }
  849. if quoted then
  850. AsmWrite('"');
  851. end;
  852. AsmLn;
  853. end;
  854. ait_label :
  855. begin
  856. if tai_label(hp).labsym.is_used then
  857. begin
  858. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  859. begin
  860. AsmWrite(#9'GLOBAL ');
  861. AsmWriteLn(tai_label(hp).labsym.name);
  862. end;
  863. AsmWriteLn(tai_label(hp).labsym.name+':');
  864. end;
  865. if SmartAsm then
  866. AddSymbol(tai_label(hp).labsym.name,true);
  867. end;
  868. ait_symbol :
  869. begin
  870. if tai_symbol(hp).has_value then
  871. internalerror(2009090803);
  872. if tai_symbol(hp).is_global or SmartAsm then
  873. begin
  874. AsmWrite(#9'GLOBAL ');
  875. AsmWriteLn(tai_symbol(hp).sym.name);
  876. end;
  877. AsmWrite(tai_symbol(hp).sym.name);
  878. if SmartAsm then
  879. AddSymbol(tai_symbol(hp).sym.name,true);
  880. if assigned(hp.next) and not(tai(hp.next).typ in
  881. [ait_const,
  882. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  883. AsmWriteLn(':')
  884. end;
  885. ait_symbol_end : ;
  886. ait_instruction :
  887. begin
  888. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  889. { We need intel order, no At&t }
  890. taicpu(hp).SetOperandOrder(op_intel);
  891. s:='';
  892. if ((fixed_opcode=A_FADDP) or
  893. (fixed_opcode=A_FMULP))
  894. and (taicpu(hp).ops=0) then
  895. begin
  896. taicpu(hp).allocate_oper(2);
  897. taicpu(hp).oper[0]^.typ:=top_reg;
  898. taicpu(hp).oper[0]^.reg:=NR_ST1;
  899. taicpu(hp).oper[1]^.typ:=top_reg;
  900. taicpu(hp).oper[1]^.reg:=NR_ST;
  901. end;
  902. if fixed_opcode=A_FWAIT then
  903. AsmWriteln(#9#9'DB'#9'09bh')
  904. else
  905. begin
  906. {$ifndef i8086}
  907. { We need to explicitely set
  908. word prefix to get selectors
  909. to be pushed in 2 bytes PM }
  910. if (taicpu(hp).opsize=S_W) and
  911. ((fixed_opcode=A_PUSH) or
  912. (fixed_opcode=A_POP)) and
  913. (taicpu(hp).oper[0]^.typ=top_reg) and
  914. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  915. AsmWriteln(#9#9'DB'#9'066h');
  916. {$endif not i8086}
  917. AsmWrite(#9#9+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  918. if taicpu(hp).ops<>0 then
  919. begin
  920. if is_calljmp(fixed_opcode) then
  921. begin
  922. AsmWrite(#9);
  923. WriteOper_jmp(taicpu(hp).oper[0]^,fixed_opcode);
  924. end
  925. else
  926. begin
  927. for i:=0 to taicpu(hp).ops-1 do
  928. begin
  929. if i=0 then
  930. AsmWrite(#9)
  931. else
  932. AsmWrite(',');
  933. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  934. end;
  935. end;
  936. end;
  937. AsmLn;
  938. end;
  939. end;
  940. ait_stab,
  941. ait_force_line,
  942. ait_function_name : ;
  943. ait_cutobject :
  944. begin
  945. if SmartAsm then
  946. begin
  947. { only reset buffer if nothing has changed }
  948. if AsmSize=AsmStartSize then
  949. AsmClear
  950. else
  951. begin
  952. if SmartAsm then
  953. begin
  954. WriteSmartExternals;
  955. FreeExternChainList;
  956. end;
  957. AsmClose;
  958. DoAssemble;
  959. AsmCreate(tai_cutobject(hp).place);
  960. WriteHeader;
  961. end;
  962. { avoid empty files }
  963. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  964. begin
  965. if tai(hp.next).typ=ait_section then
  966. lasTSectype:=tai_section(hp.next).sectype;
  967. hp:=tai(hp.next);
  968. end;
  969. if lasTSectype<>sec_none then
  970. WriteSection(lasTSectype,'');
  971. AsmStartSize:=AsmSize;
  972. end;
  973. end;
  974. ait_marker :
  975. if tai_marker(hp).kind=mark_NoLineInfoStart then
  976. inc(InlineLevel)
  977. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  978. dec(InlineLevel);
  979. ait_directive :
  980. begin
  981. case tai_directive(hp).directive of
  982. asd_nasm_import :
  983. AsmWrite('import ');
  984. asd_extern :
  985. AsmWrite('EXTERN ');
  986. else
  987. internalerror(200509191);
  988. end;
  989. if tai_directive(hp).name<>'' then
  990. begin
  991. if SmartAsm then
  992. AddSymbol(tai_directive(hp).name,false);
  993. AsmWrite(tai_directive(hp).name);
  994. end;
  995. AsmLn;
  996. end;
  997. ait_seh_directive :
  998. { Ignore for now };
  999. else
  1000. internalerror(10000);
  1001. end;
  1002. hp:=tai(hp.next);
  1003. end;
  1004. end;
  1005. procedure T386NasmAssembler.WriteExternals;
  1006. var
  1007. sym : TAsmSymbol;
  1008. i : longint;
  1009. begin
  1010. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1011. begin
  1012. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1013. if sym.bind=AB_EXTERNAL then
  1014. AsmWriteln('EXTERN'#9+sym.name);
  1015. end;
  1016. end;
  1017. procedure T386NasmAssembler.WriteSmartExternals;
  1018. var
  1019. EC : PExternChain;
  1020. begin
  1021. EC:=FEC;
  1022. while assigned(EC) do
  1023. begin
  1024. if not EC^.is_defined then
  1025. AsmWriteln('EXTERN'#9+EC^.psym^);
  1026. EC:=EC^.next;
  1027. end;
  1028. end;
  1029. procedure T386NasmAssembler.WriteHeader;
  1030. begin
  1031. {$ifdef i8086}
  1032. AsmWriteLn('BITS 16');
  1033. case current_settings.cputype of
  1034. cpu_8086: AsmWriteLn('CPU 8086');
  1035. cpu_186: AsmWriteLn('CPU 186');
  1036. cpu_286: AsmWriteLn('CPU 286');
  1037. cpu_386: AsmWriteLn('CPU 386');
  1038. cpu_Pentium: AsmWriteLn('CPU PENTIUM');
  1039. cpu_Pentium2: AsmWriteLn('CPU P2');
  1040. cpu_Pentium3: AsmWriteLn('CPU P3');
  1041. cpu_Pentium4: AsmWriteLn('CPU P4');
  1042. cpu_PentiumM: AsmWriteLn('CPU P4');
  1043. else
  1044. internalerror(2013050101);
  1045. end;
  1046. AsmWriteLn('SECTION ' + CodeSectionName + ' use16 class=code');
  1047. if current_settings.x86memorymodel in x86_near_data_models then
  1048. begin
  1049. { NASM complains if you put a missing section in the GROUP directive, so }
  1050. { we add empty declarations to make sure they exist, even if empty }
  1051. AsmWriteLn('SECTION .rodata');
  1052. AsmWriteLn('SECTION .data');
  1053. AsmWriteLn('SECTION .fpc');
  1054. { WLINK requires class=bss in order to leave the BSS section out of the executable }
  1055. AsmWriteLn('SECTION .bss class=bss');
  1056. { group these sections in the same segment }
  1057. if current_settings.x86memorymodel=mm_tiny then
  1058. AsmWriteLn('GROUP dgroup text rodata data fpc bss')
  1059. else
  1060. AsmWriteLn('GROUP dgroup rodata data fpc bss');
  1061. end;
  1062. if paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  1063. begin
  1064. AsmWriteLn('SECTION .debug_frame use32 class=DWARF');
  1065. AsmWriteLn('SECTION .debug_info use32 class=DWARF');
  1066. AsmWriteLn('SECTION .debug_line use32 class=DWARF');
  1067. AsmWriteLn('SECTION .debug_abbrev use32 class=DWARF');
  1068. end;
  1069. AsmWriteLn('SECTION ' + CodeSectionName);
  1070. {$else i8086}
  1071. AsmWriteLn('BITS 32');
  1072. {$endif i8086}
  1073. end;
  1074. procedure T386NasmAssembler.WriteAsmList;
  1075. var
  1076. hal : tasmlisttype;
  1077. begin
  1078. {$ifdef EXTDEBUG}
  1079. if current_module.mainsource<>'' then
  1080. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1081. {$endif}
  1082. WriteHeader;
  1083. AsmLn;
  1084. WriteExternals;
  1085. for hal:=low(TasmlistType) to high(TasmlistType) do
  1086. begin
  1087. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1088. writetree(current_asmdata.asmlists[hal]);
  1089. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1090. end;
  1091. AsmLn;
  1092. if SmartAsm then
  1093. begin
  1094. WriteSmartExternals;
  1095. FreeExternChainList;
  1096. end;
  1097. {$ifdef EXTDEBUG}
  1098. if current_module.mainsource<>'' then
  1099. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1100. {$endif EXTDEBUG}
  1101. end;
  1102. {*****************************************************************************
  1103. Initialize
  1104. *****************************************************************************}
  1105. const
  1106. as_i386_nasmcoff_info : tasminfo =
  1107. (
  1108. id : as_i386_nasmcoff;
  1109. idtxt : 'NASMCOFF';
  1110. asmbin : 'nasm';
  1111. asmcmd : '-f coff -o $OBJ -w-orphan-labels $ASM';
  1112. supported_targets : [system_i386_go32v2];
  1113. flags : [af_needar,af_no_debug];
  1114. labelprefix : '..@';
  1115. comment : '; ';
  1116. dollarsign: '$';
  1117. );
  1118. as_i386_nasmwin32_info : tasminfo =
  1119. (
  1120. id : as_i386_nasmwin32;
  1121. idtxt : 'NASMWIN32';
  1122. asmbin : 'nasm';
  1123. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $ASM';
  1124. supported_targets : [system_i386_win32];
  1125. flags : [af_needar,af_no_debug];
  1126. labelprefix : '..@';
  1127. comment : '; ';
  1128. dollarsign: '$';
  1129. );
  1130. as_i386_nasmobj_info : tasminfo =
  1131. (
  1132. id : as_i386_nasmobj;
  1133. idtxt : 'NASMOBJ';
  1134. asmbin : 'nasm';
  1135. asmcmd : '-f obj -o $OBJ -w-orphan-labels $ASM';
  1136. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1137. flags : [af_needar,af_no_debug];
  1138. labelprefix : '..@';
  1139. comment : '; ';
  1140. dollarsign: '$';
  1141. );
  1142. as_i386_nasmwdosx_info : tasminfo =
  1143. (
  1144. id : as_i386_nasmwdosx;
  1145. idtxt : 'NASMWDOSX';
  1146. asmbin : 'nasm';
  1147. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $ASM';
  1148. supported_targets : [system_i386_wdosx];
  1149. flags : [af_needar,af_no_debug];
  1150. labelprefix : '..@';
  1151. comment : '; ';
  1152. dollarsign: '$';
  1153. );
  1154. as_i386_nasmelf_info : tasminfo =
  1155. (
  1156. id : as_i386_nasmelf;
  1157. idtxt : 'NASMELF';
  1158. asmbin : 'nasm';
  1159. asmcmd : '-f elf -o $OBJ -w-orphan-labels $ASM';
  1160. supported_targets : [system_i386_linux];
  1161. flags : [af_needar,af_no_debug];
  1162. labelprefix : '..@';
  1163. comment : '; ';
  1164. dollarsign: '$';
  1165. );
  1166. as_i386_nasmbeos_info : tasminfo =
  1167. (
  1168. id : as_i386_nasmbeos;
  1169. idtxt : 'NASMELF';
  1170. asmbin : 'nasm';
  1171. asmcmd : '-f elf -o $OBJ -w-orphan-labels $ASM';
  1172. supported_targets : [system_i386_beos];
  1173. flags : [af_needar,af_no_debug];
  1174. labelprefix : '..@';
  1175. comment : '; ';
  1176. dollarsign: '$';
  1177. );
  1178. as_i386_nasmhaiku_info : tasminfo =
  1179. (
  1180. id : as_i386_nasmhaiku;
  1181. idtxt : 'NASMELF';
  1182. asmbin : 'nasm';
  1183. asmcmd : '-f elf -o $OBJ -w-orphan-labels $ASM';
  1184. supported_targets : [system_i386_haiku];
  1185. flags : [af_needar,af_no_debug];
  1186. labelprefix : '..@';
  1187. comment : '; ';
  1188. dollarsign: '$';
  1189. );
  1190. initialization
  1191. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1192. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1193. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1194. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1195. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1196. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1197. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1198. end.