agx86nsm.pas 40 KB

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