agx86nsm.pas 41 KB

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