agx86nsm.pas 40 KB

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