agx86nsm.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. prefix : string;
  531. begin
  532. if not assigned(p) then
  533. exit;
  534. InlineLevel:=0;
  535. { lineinfo is only needed for al_procedures (PFV) }
  536. do_line:=(cs_asm_source in current_settings.globalswitches) or
  537. ((cs_lineinfo in current_settings.moduleswitches)
  538. and (p=current_asmdata.asmlists[al_procedures]));
  539. hp:=tai(p.first);
  540. while assigned(hp) do
  541. begin
  542. prefetch(pointer(hp.next)^);
  543. if not(hp.typ in SkipLineInfo) then
  544. begin
  545. current_filepos:=tailineinfo(hp).fileinfo;
  546. { no line info for inlined code }
  547. if do_line and (inlinelevel=0) then
  548. WriteSourceLine(hp as tailineinfo);
  549. end;
  550. case hp.typ of
  551. ait_comment :
  552. Begin
  553. AsmWrite(target_asm.comment);
  554. AsmWritePChar(tai_comment(hp).str);
  555. AsmLn;
  556. End;
  557. ait_regalloc :
  558. begin
  559. if (cs_asm_regalloc in current_settings.globalswitches) then
  560. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  561. regallocstr[tai_regalloc(hp).ratype]);
  562. end;
  563. ait_tempalloc :
  564. begin
  565. if (cs_asm_tempalloc in current_settings.globalswitches) then
  566. WriteTempalloc(tai_tempalloc(hp));
  567. end;
  568. ait_section :
  569. begin
  570. if tai_section(hp).sectype<>sec_none then
  571. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  572. LasTSecType:=tai_section(hp).sectype;
  573. end;
  574. ait_align :
  575. begin
  576. { nasm gives warnings when it finds align in bss as it
  577. wants to store data }
  578. if (lastsectype<>sec_bss) and
  579. (tai_align(hp).aligntype>1) then
  580. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  581. end;
  582. ait_datablock :
  583. begin
  584. if tai_datablock(hp).is_global or SmartAsm then
  585. begin
  586. AsmWrite(#9'GLOBAL ');
  587. AsmWriteLn(tai_datablock(hp).sym.name);
  588. end;
  589. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  590. if SmartAsm then
  591. AddSymbol(tai_datablock(hp).sym.name,true);
  592. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  593. end;
  594. ait_const:
  595. begin
  596. consttype:=tai_const(hp).consttype;
  597. case consttype of
  598. aitconst_64bit,
  599. aitconst_64bit_unaligned:
  600. begin
  601. if assigned(tai_const(hp).sym) then
  602. internalerror(200404292);
  603. AsmWrite(ait_const2str[aitconst_32bit]);
  604. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  605. AsmWrite(',');
  606. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  607. AsmLn;
  608. end;
  609. aitconst_uleb128bit,
  610. aitconst_sleb128bit,
  611. aitconst_128bit:
  612. begin
  613. end;
  614. {$ifdef i8086}
  615. aitconst_farptr:
  616. begin
  617. AsmWrite(ait_const2str[aitconst_16bit]);
  618. if assigned(tai_const(hp).sym) then
  619. begin
  620. if SmartAsm then
  621. AddSymbol(tai_const(hp).sym.name,false);
  622. AsmWrite(tai_const(hp).sym.name);
  623. if tai_const(hp).value<>0 then
  624. AsmWrite(tostr_with_plus(tai_const(hp).value));
  625. AsmLn;
  626. AsmWrite(ait_const2str[aitconst_16bit]);
  627. AsmWrite('SEG ');
  628. AsmWrite(tai_const(hp).sym.name);
  629. end
  630. else
  631. AsmWrite(tostr(lo(longint(tai_const(hp).value)))+','+
  632. tostr(hi(longint(tai_const(hp).value))));
  633. AsmLn;
  634. end;
  635. {$endif i8086}
  636. aitconst_32bit,
  637. aitconst_16bit,
  638. aitconst_8bit,
  639. aitconst_rva_symbol,
  640. aitconst_secrel32_symbol,
  641. aitconst_16bit_unaligned,
  642. aitconst_32bit_unaligned:
  643. begin
  644. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  645. l:=0;
  646. repeat
  647. if assigned(tai_const(hp).sym) then
  648. begin
  649. if SmartAsm then
  650. begin
  651. AddSymbol(tai_const(hp).sym.name,false);
  652. if assigned(tai_const(hp).endsym) then
  653. AddSymbol(tai_const(hp).endsym.name,false);
  654. end;
  655. if assigned(tai_const(hp).endsym) then
  656. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  657. else
  658. s:=tai_const(hp).sym.name;
  659. if tai_const(hp).value<>0 then
  660. s:=s+tostr_with_plus(tai_const(hp).value);
  661. end
  662. else
  663. s:=tostr(tai_const(hp).value);
  664. AsmWrite(s);
  665. inc(l,length(s));
  666. if (l>line_length) or
  667. (hp.next=nil) or
  668. (tai(hp.next).typ<>ait_const) or
  669. (tai_const(hp.next).consttype<>consttype) then
  670. break;
  671. hp:=tai(hp.next);
  672. AsmWrite(',');
  673. until false;
  674. AsmLn;
  675. end;
  676. else
  677. internalerror(200704252);
  678. end;
  679. end;
  680. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  681. ait_real_80bit :
  682. begin
  683. if do_line then
  684. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  685. { Make sure e is a extended type, bestreal could be
  686. a different type (bestreal) !! (PFV) }
  687. e:=tai_real_80bit(hp).value;
  688. AsmWrite(#9#9'DB'#9);
  689. for i:=0 to 9 do
  690. begin
  691. if i<>0 then
  692. AsmWrite(',');
  693. AsmWrite(tostr(t80bitarray(e)[i]));
  694. end;
  695. for i:=11 to tai_real_80bit(hp).savesize do
  696. AsmWrite(',0');
  697. AsmLn;
  698. end;
  699. {$else cpuextended}
  700. ait_real_80bit :
  701. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  702. {$endif cpuextended}
  703. // ait_real_64bit :
  704. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  705. ait_real_64bit :
  706. begin
  707. if do_line then
  708. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  709. d:=tai_real_64bit(hp).value;
  710. { swap the values to correct endian if required }
  711. if source_info.endian <> target_info.endian then
  712. swap64bitarray(t64bitarray(d));
  713. AsmWrite(#9#9'DB'#9);
  714. {$ifdef arm}
  715. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  716. begin
  717. for i:=4 to 7 do
  718. begin
  719. if i<>4 then
  720. AsmWrite(',');
  721. AsmWrite(tostr(t64bitarray(d)[i]));
  722. end;
  723. for i:=0 to 3 do
  724. begin
  725. AsmWrite(',');
  726. AsmWrite(tostr(t64bitarray(d)[i]));
  727. end;
  728. end
  729. else
  730. {$endif arm}
  731. begin
  732. for i:=0 to 7 do
  733. begin
  734. if i<>0 then
  735. AsmWrite(',');
  736. AsmWrite(tostr(t64bitarray(d)[i]));
  737. end;
  738. end;
  739. AsmLn;
  740. end;
  741. // ait_real_32bit :
  742. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  743. ait_real_32bit :
  744. begin
  745. if do_line then
  746. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  747. sin:=tai_real_32bit(hp).value;
  748. { swap the values to correct endian if required }
  749. if source_info.endian <> target_info.endian then
  750. swap32bitarray(t32bitarray(sin));
  751. AsmWrite(#9#9'DB'#9);
  752. for i:=0 to 3 do
  753. begin
  754. if i<>0 then
  755. AsmWrite(',');
  756. AsmWrite(tostr(t32bitarray(sin)[i]));
  757. end;
  758. AsmLn;
  759. end;
  760. // ait_comp_64bit :
  761. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  762. ait_comp_64bit :
  763. begin
  764. if do_line then
  765. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  766. AsmWrite(#9#9'DB'#9);
  767. co:=comp(tai_comp_64bit(hp).value);
  768. { swap the values to correct endian if required }
  769. if source_info.endian <> target_info.endian then
  770. swap64bitarray(t64bitarray(co));
  771. for i:=0 to 7 do
  772. begin
  773. if i<>0 then
  774. AsmWrite(',');
  775. AsmWrite(tostr(t64bitarray(co)[i]));
  776. end;
  777. AsmLn;
  778. end;
  779. ait_string :
  780. begin
  781. counter := 0;
  782. lines := tai_string(hp).len div line_length;
  783. { separate lines in different parts }
  784. if tai_string(hp).len > 0 then
  785. Begin
  786. for j := 0 to lines-1 do
  787. begin
  788. AsmWrite(#9#9'DB'#9);
  789. quoted:=false;
  790. for i:=counter to counter+line_length-1 do
  791. begin
  792. { it is an ascii character. }
  793. if (ord(tai_string(hp).str[i])>31) and
  794. (ord(tai_string(hp).str[i])<128) and
  795. (tai_string(hp).str[i]<>'"') then
  796. begin
  797. if not(quoted) then
  798. begin
  799. if i>counter then
  800. AsmWrite(',');
  801. AsmWrite('"');
  802. end;
  803. AsmWrite(tai_string(hp).str[i]);
  804. quoted:=true;
  805. end { if > 31 and < 128 and ord('"') }
  806. else
  807. begin
  808. if quoted then
  809. AsmWrite('"');
  810. if i>counter then
  811. AsmWrite(',');
  812. quoted:=false;
  813. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  814. end;
  815. end; { end for i:=0 to... }
  816. if quoted then AsmWrite('"');
  817. AsmWrite(target_info.newline);
  818. inc(counter,line_length);
  819. end; { end for j:=0 ... }
  820. { do last line of lines }
  821. if counter<tai_string(hp).len then
  822. AsmWrite(#9#9'DB'#9);
  823. quoted:=false;
  824. for i:=counter to tai_string(hp).len-1 do
  825. begin
  826. { it is an ascii character. }
  827. if (ord(tai_string(hp).str[i])>31) and
  828. (ord(tai_string(hp).str[i])<128) and
  829. (tai_string(hp).str[i]<>'"') then
  830. begin
  831. if not(quoted) then
  832. begin
  833. if i>counter then
  834. AsmWrite(',');
  835. AsmWrite('"');
  836. end;
  837. AsmWrite(tai_string(hp).str[i]);
  838. quoted:=true;
  839. end { if > 31 and < 128 and " }
  840. else
  841. begin
  842. if quoted then
  843. AsmWrite('"');
  844. if i>counter then
  845. AsmWrite(',');
  846. quoted:=false;
  847. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  848. end;
  849. end; { end for i:=0 to... }
  850. if quoted then
  851. AsmWrite('"');
  852. end;
  853. AsmLn;
  854. end;
  855. ait_label :
  856. begin
  857. if tai_label(hp).labsym.is_used then
  858. begin
  859. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  860. begin
  861. AsmWrite(#9'GLOBAL ');
  862. AsmWriteLn(tai_label(hp).labsym.name);
  863. end;
  864. AsmWriteLn(tai_label(hp).labsym.name+':');
  865. end;
  866. if SmartAsm then
  867. AddSymbol(tai_label(hp).labsym.name,true);
  868. end;
  869. ait_symbol :
  870. begin
  871. if tai_symbol(hp).has_value then
  872. internalerror(2009090803);
  873. if tai_symbol(hp).is_global or SmartAsm then
  874. begin
  875. AsmWrite(#9'GLOBAL ');
  876. AsmWriteLn(tai_symbol(hp).sym.name);
  877. end;
  878. AsmWrite(tai_symbol(hp).sym.name);
  879. if SmartAsm then
  880. AddSymbol(tai_symbol(hp).sym.name,true);
  881. if assigned(hp.next) and not(tai(hp.next).typ in
  882. [ait_const,
  883. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  884. AsmWriteLn(':')
  885. end;
  886. ait_symbol_end : ;
  887. ait_instruction :
  888. begin
  889. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  890. { We need intel order, no At&t }
  891. taicpu(hp).SetOperandOrder(op_intel);
  892. s:='';
  893. if ((fixed_opcode=A_FADDP) or
  894. (fixed_opcode=A_FMULP))
  895. and (taicpu(hp).ops=0) then
  896. begin
  897. taicpu(hp).allocate_oper(2);
  898. taicpu(hp).oper[0]^.typ:=top_reg;
  899. taicpu(hp).oper[0]^.reg:=NR_ST1;
  900. taicpu(hp).oper[1]^.typ:=top_reg;
  901. taicpu(hp).oper[1]^.reg:=NR_ST;
  902. end;
  903. if fixed_opcode=A_FWAIT then
  904. AsmWriteln(#9#9'DB'#9'09bh')
  905. else
  906. begin
  907. prefix:='';
  908. {$ifdef i8086}
  909. { nickysn note: I don't know if the 187 requires FWAIT before
  910. every instruction like the 8087, so I'm including it just in case }
  911. if (current_settings.cputype<=cpu_186) and
  912. requires_fwait_on_8087(fixed_opcode) then
  913. prefix:='wait '+prefix;
  914. {$endif i8086}
  915. {$ifndef i8086}
  916. { We need to explicitely set
  917. word prefix to get selectors
  918. to be pushed in 2 bytes PM }
  919. if (taicpu(hp).opsize=S_W) and
  920. ((fixed_opcode=A_PUSH) or
  921. (fixed_opcode=A_POP)) and
  922. (taicpu(hp).oper[0]^.typ=top_reg) and
  923. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  924. AsmWriteln(#9#9'DB'#9'066h');
  925. {$endif not i8086}
  926. AsmWrite(#9#9+prefix+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  927. if taicpu(hp).ops<>0 then
  928. begin
  929. if is_calljmp(fixed_opcode) then
  930. begin
  931. AsmWrite(#9);
  932. WriteOper_jmp(taicpu(hp).oper[0]^,fixed_opcode);
  933. end
  934. else
  935. begin
  936. for i:=0 to taicpu(hp).ops-1 do
  937. begin
  938. if i=0 then
  939. AsmWrite(#9)
  940. else
  941. AsmWrite(',');
  942. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  943. end;
  944. end;
  945. end;
  946. AsmLn;
  947. end;
  948. end;
  949. ait_stab,
  950. ait_force_line,
  951. ait_function_name : ;
  952. ait_cutobject :
  953. begin
  954. if SmartAsm then
  955. begin
  956. { only reset buffer if nothing has changed }
  957. if AsmSize=AsmStartSize then
  958. AsmClear
  959. else
  960. begin
  961. if SmartAsm then
  962. begin
  963. WriteSmartExternals;
  964. FreeExternChainList;
  965. end;
  966. AsmClose;
  967. DoAssemble;
  968. AsmCreate(tai_cutobject(hp).place);
  969. WriteHeader;
  970. end;
  971. { avoid empty files }
  972. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  973. begin
  974. if tai(hp.next).typ=ait_section then
  975. lasTSectype:=tai_section(hp.next).sectype;
  976. hp:=tai(hp.next);
  977. end;
  978. if lasTSectype<>sec_none then
  979. WriteSection(lasTSectype,'');
  980. AsmStartSize:=AsmSize;
  981. end;
  982. end;
  983. ait_marker :
  984. if tai_marker(hp).kind=mark_NoLineInfoStart then
  985. inc(InlineLevel)
  986. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  987. dec(InlineLevel);
  988. ait_directive :
  989. begin
  990. case tai_directive(hp).directive of
  991. asd_nasm_import :
  992. AsmWrite('import ');
  993. asd_extern :
  994. AsmWrite('EXTERN ');
  995. else
  996. internalerror(200509191);
  997. end;
  998. if tai_directive(hp).name<>'' then
  999. begin
  1000. if SmartAsm then
  1001. AddSymbol(tai_directive(hp).name,false);
  1002. AsmWrite(tai_directive(hp).name);
  1003. end;
  1004. AsmLn;
  1005. end;
  1006. ait_seh_directive :
  1007. { Ignore for now };
  1008. ait_varloc:
  1009. begin
  1010. if tai_varloc(hp).newlocationhi<>NR_NO then
  1011. AsmWriteLn(target_asm.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1012. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation))
  1013. else
  1014. AsmWriteLn(target_asm.comment+'Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1015. std_regname(tai_varloc(hp).newlocation));
  1016. end;
  1017. else
  1018. internalerror(10000);
  1019. end;
  1020. hp:=tai(hp.next);
  1021. end;
  1022. end;
  1023. procedure T386NasmAssembler.WriteExternals;
  1024. var
  1025. sym : TAsmSymbol;
  1026. i : longint;
  1027. begin
  1028. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  1029. begin
  1030. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  1031. if sym.bind=AB_EXTERNAL then
  1032. AsmWriteln('EXTERN'#9+sym.name);
  1033. end;
  1034. end;
  1035. procedure T386NasmAssembler.WriteSmartExternals;
  1036. var
  1037. EC : PExternChain;
  1038. begin
  1039. EC:=FEC;
  1040. while assigned(EC) do
  1041. begin
  1042. if not EC^.is_defined then
  1043. AsmWriteln('EXTERN'#9+EC^.psym^);
  1044. EC:=EC^.next;
  1045. end;
  1046. end;
  1047. procedure T386NasmAssembler.WriteHeader;
  1048. begin
  1049. {$ifdef i8086}
  1050. AsmWriteLn('BITS 16');
  1051. case current_settings.cputype of
  1052. cpu_8086: AsmWriteLn('CPU 8086');
  1053. cpu_186: AsmWriteLn('CPU 186');
  1054. cpu_286: AsmWriteLn('CPU 286');
  1055. cpu_386: AsmWriteLn('CPU 386');
  1056. cpu_Pentium: AsmWriteLn('CPU PENTIUM');
  1057. cpu_Pentium2: AsmWriteLn('CPU P2');
  1058. cpu_Pentium3: AsmWriteLn('CPU P3');
  1059. cpu_Pentium4: AsmWriteLn('CPU P4');
  1060. cpu_PentiumM: AsmWriteLn('CPU P4');
  1061. else
  1062. internalerror(2013050101);
  1063. end;
  1064. AsmWriteLn('SECTION ' + CodeSectionName + ' use16 class=code');
  1065. if current_settings.x86memorymodel in x86_near_data_models then
  1066. begin
  1067. { NASM complains if you put a missing section in the GROUP directive, so }
  1068. { we add empty declarations to make sure they exist, even if empty }
  1069. AsmWriteLn('SECTION .rodata');
  1070. AsmWriteLn('SECTION .data');
  1071. AsmWriteLn('SECTION .fpc');
  1072. { WLINK requires class=bss in order to leave the BSS section out of the executable }
  1073. AsmWriteLn('SECTION .bss class=bss');
  1074. { group these sections in the same segment }
  1075. if current_settings.x86memorymodel=mm_tiny then
  1076. AsmWriteLn('GROUP dgroup text rodata data fpc bss')
  1077. else
  1078. AsmWriteLn('GROUP dgroup rodata data fpc bss');
  1079. end;
  1080. if paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
  1081. begin
  1082. AsmWriteLn('SECTION .debug_frame use32 class=DWARF');
  1083. AsmWriteLn('SECTION .debug_info use32 class=DWARF');
  1084. AsmWriteLn('SECTION .debug_line use32 class=DWARF');
  1085. AsmWriteLn('SECTION .debug_abbrev use32 class=DWARF');
  1086. end;
  1087. AsmWriteLn('SECTION ' + CodeSectionName);
  1088. {$else i8086}
  1089. AsmWriteLn('BITS 32');
  1090. {$endif i8086}
  1091. end;
  1092. procedure T386NasmAssembler.WriteAsmList;
  1093. var
  1094. hal : tasmlisttype;
  1095. begin
  1096. {$ifdef EXTDEBUG}
  1097. if current_module.mainsource<>'' then
  1098. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1099. {$endif}
  1100. WriteHeader;
  1101. AsmLn;
  1102. WriteExternals;
  1103. for hal:=low(TasmlistType) to high(TasmlistType) do
  1104. begin
  1105. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1106. writetree(current_asmdata.asmlists[hal]);
  1107. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1108. end;
  1109. AsmLn;
  1110. if SmartAsm then
  1111. begin
  1112. WriteSmartExternals;
  1113. FreeExternChainList;
  1114. end;
  1115. {$ifdef EXTDEBUG}
  1116. if current_module.mainsource<>'' then
  1117. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1118. {$endif EXTDEBUG}
  1119. end;
  1120. {*****************************************************************************
  1121. Initialize
  1122. *****************************************************************************}
  1123. const
  1124. as_i386_nasmcoff_info : tasminfo =
  1125. (
  1126. id : as_i386_nasmcoff;
  1127. idtxt : 'NASMCOFF';
  1128. asmbin : 'nasm';
  1129. asmcmd : '-f coff -o $OBJ -w-orphan-labels $ASM';
  1130. supported_targets : [system_i386_go32v2];
  1131. flags : [af_needar,af_no_debug];
  1132. labelprefix : '..@';
  1133. comment : '; ';
  1134. dollarsign: '$';
  1135. );
  1136. as_i386_nasmwin32_info : tasminfo =
  1137. (
  1138. id : as_i386_nasmwin32;
  1139. idtxt : 'NASMWIN32';
  1140. asmbin : 'nasm';
  1141. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $ASM';
  1142. supported_targets : [system_i386_win32];
  1143. flags : [af_needar,af_no_debug];
  1144. labelprefix : '..@';
  1145. comment : '; ';
  1146. dollarsign: '$';
  1147. );
  1148. as_i386_nasmobj_info : tasminfo =
  1149. (
  1150. id : as_i386_nasmobj;
  1151. idtxt : 'NASMOBJ';
  1152. asmbin : 'nasm';
  1153. asmcmd : '-f obj -o $OBJ -w-orphan-labels $ASM';
  1154. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1155. flags : [af_needar,af_no_debug];
  1156. labelprefix : '..@';
  1157. comment : '; ';
  1158. dollarsign: '$';
  1159. );
  1160. as_i386_nasmwdosx_info : tasminfo =
  1161. (
  1162. id : as_i386_nasmwdosx;
  1163. idtxt : 'NASMWDOSX';
  1164. asmbin : 'nasm';
  1165. asmcmd : '-f win32 -o $OBJ -w-orphan-labels $ASM';
  1166. supported_targets : [system_i386_wdosx];
  1167. flags : [af_needar,af_no_debug];
  1168. labelprefix : '..@';
  1169. comment : '; ';
  1170. dollarsign: '$';
  1171. );
  1172. as_i386_nasmelf_info : tasminfo =
  1173. (
  1174. id : as_i386_nasmelf;
  1175. idtxt : 'NASMELF';
  1176. asmbin : 'nasm';
  1177. asmcmd : '-f elf -o $OBJ -w-orphan-labels $ASM';
  1178. supported_targets : [system_i386_linux];
  1179. flags : [af_needar,af_no_debug];
  1180. labelprefix : '..@';
  1181. comment : '; ';
  1182. dollarsign: '$';
  1183. );
  1184. as_i386_nasmbeos_info : tasminfo =
  1185. (
  1186. id : as_i386_nasmbeos;
  1187. idtxt : 'NASMELF';
  1188. asmbin : 'nasm';
  1189. asmcmd : '-f elf -o $OBJ -w-orphan-labels $ASM';
  1190. supported_targets : [system_i386_beos];
  1191. flags : [af_needar,af_no_debug];
  1192. labelprefix : '..@';
  1193. comment : '; ';
  1194. dollarsign: '$';
  1195. );
  1196. as_i386_nasmhaiku_info : tasminfo =
  1197. (
  1198. id : as_i386_nasmhaiku;
  1199. idtxt : 'NASMELF';
  1200. asmbin : 'nasm';
  1201. asmcmd : '-f elf -o $OBJ -w-orphan-labels $ASM';
  1202. supported_targets : [system_i386_haiku];
  1203. flags : [af_needar,af_no_debug];
  1204. labelprefix : '..@';
  1205. comment : '; ';
  1206. dollarsign: '$';
  1207. );
  1208. initialization
  1209. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1210. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1211. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1212. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1213. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1214. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1215. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1216. end.