agx86nsm.pas 39 KB

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