agx86nsm.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. procedure WriteReference(var ref : treference);
  29. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  30. procedure WriteOper_jmp(const o:toper; op : tasmop);
  31. procedure WriteSection(atype:TAsmSectiontype;const aname:string);
  32. public
  33. procedure WriteTree(p:TAsmList);override;
  34. procedure WriteAsmList;override;
  35. procedure WriteExternals;
  36. procedure WriteSmartExternals;
  37. procedure WriteHeader;
  38. end;
  39. implementation
  40. uses
  41. cutils,globtype,globals,systems,cclasses,
  42. fmodule,finput,verbose,cpuinfo,cgbase
  43. ;
  44. type
  45. {$ifdef cpuextended}
  46. t80bitarray = array[0..9] of byte;
  47. {$endif cpuextended}
  48. t64bitarray = array[0..7] of byte;
  49. t32bitarray = array[0..3] of byte;
  50. const
  51. line_length = 64;
  52. nasm_regname_table : array[tregisterindex] of string[7] = (
  53. {r386nasm.inc contains the Nasm name of each register.}
  54. {$if defined(x86_64)}
  55. {$fatal nasm support not yet implemented for x86_64 }
  56. {$elseif defined(i386)}
  57. {$i r386nasm.inc}
  58. {$elseif defined(i8086)}
  59. {$i r8086nasm.inc}
  60. {$endif}
  61. );
  62. function nasm_regname(r:Tregister):string;
  63. var
  64. p : tregisterindex;
  65. begin
  66. p:=findreg_by_number(r);
  67. if p<>0 then
  68. result:=nasm_regname_table[p]
  69. else
  70. result:=generic_regname(r);
  71. end;
  72. function single2str(d : single) : string;
  73. var
  74. hs : string;
  75. p : longint;
  76. begin
  77. str(d,hs);
  78. { nasm expects a lowercase e }
  79. p:=pos('E',hs);
  80. if p>0 then
  81. hs[p]:='e';
  82. p:=pos('+',hs);
  83. if p>0 then
  84. delete(hs,p,1);
  85. single2str:=lower(hs);
  86. end;
  87. function double2str(d : double) : string;
  88. var
  89. hs : string;
  90. p : longint;
  91. begin
  92. str(d,hs);
  93. { nasm expects a lowercase e }
  94. p:=pos('E',hs);
  95. if p>0 then
  96. hs[p]:='e';
  97. p:=pos('+',hs);
  98. if p>0 then
  99. delete(hs,p,1);
  100. double2str:=lower(hs);
  101. end;
  102. function extended2str(e : extended) : string;
  103. var
  104. hs : string;
  105. p : longint;
  106. begin
  107. str(e,hs);
  108. { nasm expects a lowercase e }
  109. p:=pos('E',hs);
  110. if p>0 then
  111. hs[p]:='e';
  112. p:=pos('+',hs);
  113. if p>0 then
  114. delete(hs,p,1);
  115. extended2str:=lower(hs);
  116. end;
  117. { convert floating point values }
  118. { to correct endian }
  119. procedure swap64bitarray(var t: t64bitarray);
  120. var
  121. b: byte;
  122. begin
  123. b:= t[7];
  124. t[7] := t[0];
  125. t[0] := b;
  126. b := t[6];
  127. t[6] := t[1];
  128. t[1] := b;
  129. b:= t[5];
  130. t[5] := t[2];
  131. t[2] := b;
  132. b:= t[4];
  133. t[4] := t[3];
  134. t[3] := b;
  135. end;
  136. procedure swap32bitarray(var t: t32bitarray);
  137. var
  138. b: byte;
  139. begin
  140. b:= t[1];
  141. t[1]:= t[2];
  142. t[2]:= b;
  143. b:= t[0];
  144. t[0]:= t[3];
  145. t[3]:= b;
  146. end;
  147. function comp2str(d : bestreal) : string;
  148. type
  149. pdouble = ^double;
  150. var
  151. c : comp;
  152. dd : pdouble;
  153. begin
  154. c:=comp(d);
  155. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  156. comp2str:=double2str(dd^);
  157. end;
  158. function sizestr(s:topsize;dest:boolean):string;
  159. begin
  160. case s of
  161. S_B : sizestr:='byte ';
  162. S_W : sizestr:='word ';
  163. S_L : sizestr:='dword ';
  164. S_Q : sizestr:='qword ';
  165. S_IS : sizestr:='word ';
  166. S_IL : sizestr:='dword ';
  167. S_IQ : sizestr:='qword ';
  168. S_FS : sizestr:='dword ';
  169. S_FL : sizestr:='qword ';
  170. S_FX : sizestr:='tword ';
  171. S_BW : if dest then
  172. sizestr:='word '
  173. else
  174. sizestr:='byte ';
  175. S_BL : if dest then
  176. sizestr:='dword '
  177. else
  178. sizestr:='byte ';
  179. S_WL : if dest then
  180. sizestr:='dword '
  181. else
  182. sizestr:='word ';
  183. else { S_NO }
  184. sizestr:='';
  185. end;
  186. end;
  187. Function PadTabs(const p:string;addch:char):string;
  188. var
  189. s : string;
  190. i : longint;
  191. begin
  192. i:=length(p);
  193. if addch<>#0 then
  194. begin
  195. inc(i);
  196. s:=p+addch;
  197. end
  198. else
  199. s:=p;
  200. if i<8 then
  201. PadTabs:=s+#9#9
  202. else
  203. PadTabs:=s+#9;
  204. end;
  205. type
  206. PExternChain = ^TExternChain;
  207. TExternChain = Record
  208. psym : pshortstring;
  209. is_defined : boolean;
  210. next : PExternChain;
  211. end;
  212. const
  213. FEC : PExternChain = nil;
  214. procedure AddSymbol(symname : string; defined : boolean);
  215. var
  216. EC : PExternChain;
  217. begin
  218. EC:=FEC;
  219. while assigned(EC) do
  220. begin
  221. if EC^.psym^=symname then
  222. begin
  223. if defined then
  224. EC^.is_defined:=true;
  225. exit;
  226. end;
  227. EC:=EC^.next;
  228. end;
  229. New(EC);
  230. EC^.next:=FEC;
  231. FEC:=EC;
  232. FEC^.psym:=stringdup(symname);
  233. FEC^.is_defined := defined;
  234. end;
  235. procedure FreeExternChainList;
  236. var
  237. EC : PExternChain;
  238. begin
  239. EC:=FEC;
  240. while assigned(EC) do
  241. begin
  242. FEC:=EC^.next;
  243. stringdispose(EC^.psym);
  244. Dispose(EC);
  245. EC:=FEC;
  246. end;
  247. end;
  248. {****************************************************************************
  249. T386NasmAssembler
  250. ****************************************************************************}
  251. procedure T386NasmAssembler.WriteReference(var ref : treference);
  252. var
  253. first : boolean;
  254. begin
  255. with ref do
  256. begin
  257. AsmWrite('[');
  258. first:=true;
  259. if (segment<>NR_NO) then
  260. AsmWrite(nasm_regname(segment)+':');
  261. if assigned(symbol) then
  262. begin
  263. AsmWrite(symbol.name);
  264. if SmartAsm then
  265. AddSymbol(symbol.name,false);
  266. first:=false;
  267. end;
  268. if (base<>NR_NO) then
  269. begin
  270. if not(first) then
  271. AsmWrite('+')
  272. else
  273. first:=false;
  274. AsmWrite(nasm_regname(base))
  275. end;
  276. if (index<>NR_NO) then
  277. begin
  278. if not(first) then
  279. AsmWrite('+')
  280. else
  281. first:=false;
  282. AsmWrite(nasm_regname(index));
  283. if scalefactor<>0 then
  284. AsmWrite('*'+tostr(scalefactor));
  285. end;
  286. if offset<0 then
  287. begin
  288. AsmWrite(tostr(offset));
  289. first:=false;
  290. end
  291. else if (offset>0) then
  292. begin
  293. AsmWrite('+'+tostr(offset));
  294. first:=false;
  295. end;
  296. if first then
  297. AsmWrite('0');
  298. AsmWrite(']');
  299. end;
  300. end;
  301. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  302. begin
  303. case o.typ of
  304. top_reg :
  305. AsmWrite(nasm_regname(o.reg));
  306. top_const :
  307. begin
  308. if (ops=1) and (opcode<>A_RET) then
  309. AsmWrite(sizestr(s,dest));
  310. AsmWrite(tostr(longint(o.val)));
  311. end;
  312. top_ref :
  313. begin
  314. if o.ref^.refaddr=addr_no then
  315. begin
  316. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  317. (opcode = A_LSS) or (opcode = A_LFS) or
  318. (opcode = A_LES) or (opcode = A_LDS) or
  319. // (opcode = A_SHR) or (opcode = A_SHL) or
  320. // (opcode = A_SAR) or (opcode = A_SAL) or
  321. (opcode = A_OUT) or (opcode = A_IN)) then
  322. AsmWrite(sizestr(s,dest));
  323. WriteReference(o.ref^);
  324. end
  325. {$ifdef i8086}
  326. else if o.ref^.refaddr=addr_dgroup then
  327. begin
  328. AsmWrite('dgroup');
  329. end
  330. {$endif i8086}
  331. else
  332. begin
  333. {$ifdef x86_64}
  334. asmwrite('qword ');
  335. {$endif}
  336. {$ifdef i386}
  337. asmwrite('dword ');
  338. {$endif i386}
  339. {$ifdef i8086}
  340. asmwrite('word ');
  341. {$endif i8086}
  342. if assigned(o.ref^.symbol) then
  343. begin
  344. if SmartAsm then
  345. AddSymbol(o.ref^.symbol.name,false);
  346. asmwrite(o.ref^.symbol.name);
  347. if o.ref^.offset=0 then
  348. exit;
  349. end;
  350. if o.ref^.offset>0 then
  351. asmwrite('+');
  352. asmwrite(tostr(o.ref^.offset));
  353. end;
  354. end;
  355. else
  356. internalerror(10001);
  357. end;
  358. end;
  359. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  360. begin
  361. case o.typ of
  362. top_reg :
  363. AsmWrite(nasm_regname(o.reg));
  364. top_ref :
  365. if o.ref^.refaddr=addr_no then
  366. WriteReference(o.ref^)
  367. else
  368. begin
  369. { NEAR forces NASM to emit near jumps, which are 386+ }
  370. {$ifndef i8086}
  371. if not(
  372. (op=A_JCXZ) or (op=A_JECXZ) or
  373. {$ifdef x86_64}
  374. (op=A_JRCXZ) or
  375. {$endif x86_64}
  376. (op=A_LOOP) or (op=A_LOOPE) or
  377. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  378. (op=A_LOOPZ)
  379. ) then
  380. AsmWrite('NEAR ');
  381. {$endif i8086}
  382. AsmWrite(o.ref^.symbol.name);
  383. if SmartAsm then
  384. AddSymbol(o.ref^.symbol.name,false);
  385. if o.ref^.offset>0 then
  386. AsmWrite('+'+tostr(o.ref^.offset))
  387. else
  388. if o.ref^.offset<0 then
  389. AsmWrite(tostr(o.ref^.offset));
  390. end;
  391. top_const :
  392. AsmWrite(tostr(aint(o.val)));
  393. else
  394. internalerror(10001);
  395. end;
  396. end;
  397. const
  398. ait_const2str : array[aitconst_128bit..aitconst_secrel32_symbol] of string[20]=(
  399. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  400. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  401. #9'RVA'#9,#9'SECREL32'#9
  402. );
  403. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  404. const
  405. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  406. '.text',
  407. '.data',
  408. '.data',
  409. '.rodata',
  410. '.bss',
  411. '.tbss',
  412. '.pdata',
  413. '.text','.data','.data','.data','.data',
  414. '.stab',
  415. '.stabstr',
  416. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  417. '.eh_frame',
  418. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  419. '.fpc',
  420. '',
  421. '.init',
  422. '.fini',
  423. '.objc_class',
  424. '.objc_meta_class',
  425. '.objc_cat_cls_meth',
  426. '.objc_cat_inst_meth',
  427. '.objc_protocol',
  428. '.objc_string_object',
  429. '.objc_cls_meth',
  430. '.objc_inst_meth',
  431. '.objc_cls_refs',
  432. '.objc_message_refs',
  433. '.objc_symbols',
  434. '.objc_category',
  435. '.objc_class_vars',
  436. '.objc_instance_vars',
  437. '.objc_module_info',
  438. '.objc_class_names',
  439. '.objc_meth_var_types',
  440. '.objc_meth_var_names',
  441. '.objc_selector_strs',
  442. '.objc_protocol_ext',
  443. '.objc_class_ext',
  444. '.objc_property',
  445. '.objc_image_info',
  446. '.objc_cstring_object',
  447. '.objc_sel_fixup',
  448. '__DATA,__objc_data',
  449. '__DATA,__objc_const',
  450. '.objc_superrefs',
  451. '__DATA, __datacoal_nt,coalesced',
  452. '.objc_classlist',
  453. '.objc_nlclasslist',
  454. '.objc_catlist',
  455. '.obcj_nlcatlist',
  456. '.objc_protolist'
  457. );
  458. begin
  459. AsmLn;
  460. AsmWrite('SECTION ');
  461. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  462. Thus, data which normally goes into .rodata and .rodata_norel sections must
  463. end up in .data section }
  464. if (atype in [sec_rodata,sec_rodata_norel]) and
  465. (target_info.system=system_i386_go32v2) then
  466. AsmWrite('.data')
  467. else
  468. AsmWrite(secnames[atype]);
  469. if create_smartlink_sections and
  470. (atype<>sec_bss) and
  471. (aname<>'') then
  472. begin
  473. AsmWrite('.');
  474. AsmWrite(aname);
  475. end;
  476. AsmLn;
  477. LasTSecType:=atype;
  478. end;
  479. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  480. {$ifdef cpuextended}
  481. type
  482. t80bitarray = array[0..9] of byte;
  483. {$endif cpuextended}
  484. var
  485. s : string;
  486. hp : tai;
  487. counter,
  488. lines,
  489. i,j,l : longint;
  490. InlineLevel : longint;
  491. consttype : taiconst_type;
  492. do_line,
  493. quoted : boolean;
  494. co : comp;
  495. sin : single;
  496. d : double;
  497. {$ifdef cpuextended}
  498. e : extended;
  499. {$endif cpuextended}
  500. fixed_opcode: TAsmOp;
  501. begin
  502. if not assigned(p) then
  503. exit;
  504. InlineLevel:=0;
  505. { lineinfo is only needed for al_procedures (PFV) }
  506. do_line:=(cs_asm_source in current_settings.globalswitches) or
  507. ((cs_lineinfo in current_settings.moduleswitches)
  508. and (p=current_asmdata.asmlists[al_procedures]));
  509. hp:=tai(p.first);
  510. while assigned(hp) do
  511. begin
  512. prefetch(pointer(hp.next)^);
  513. if not(hp.typ in SkipLineInfo) then
  514. begin
  515. current_filepos:=tailineinfo(hp).fileinfo;
  516. { no line info for inlined code }
  517. if do_line and (inlinelevel=0) then
  518. WriteSourceLine(hp as tailineinfo);
  519. end;
  520. case hp.typ of
  521. ait_comment :
  522. Begin
  523. AsmWrite(target_asm.comment);
  524. AsmWritePChar(tai_comment(hp).str);
  525. AsmLn;
  526. End;
  527. ait_regalloc :
  528. begin
  529. if (cs_asm_regalloc in current_settings.globalswitches) then
  530. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+' '+
  531. regallocstr[tai_regalloc(hp).ratype]);
  532. end;
  533. ait_tempalloc :
  534. begin
  535. if (cs_asm_tempalloc in current_settings.globalswitches) then
  536. WriteTempalloc(tai_tempalloc(hp));
  537. end;
  538. ait_section :
  539. begin
  540. if tai_section(hp).sectype<>sec_none then
  541. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  542. LasTSecType:=tai_section(hp).sectype;
  543. end;
  544. ait_align :
  545. begin
  546. { nasm gives warnings when it finds align in bss as it
  547. wants to store data }
  548. if (lastsectype<>sec_bss) and
  549. (tai_align(hp).aligntype>1) then
  550. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  551. end;
  552. ait_datablock :
  553. begin
  554. if tai_datablock(hp).is_global or SmartAsm then
  555. begin
  556. AsmWrite(#9'GLOBAL ');
  557. AsmWriteLn(tai_datablock(hp).sym.name);
  558. end;
  559. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  560. if SmartAsm then
  561. AddSymbol(tai_datablock(hp).sym.name,true);
  562. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  563. end;
  564. ait_const:
  565. begin
  566. consttype:=tai_const(hp).consttype;
  567. case consttype of
  568. aitconst_64bit :
  569. begin
  570. if assigned(tai_const(hp).sym) then
  571. internalerror(200404292);
  572. AsmWrite(ait_const2str[aitconst_32bit]);
  573. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  574. AsmWrite(',');
  575. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  576. AsmLn;
  577. end;
  578. aitconst_uleb128bit,
  579. aitconst_sleb128bit,
  580. aitconst_128bit:
  581. begin
  582. end;
  583. aitconst_32bit,
  584. aitconst_16bit,
  585. aitconst_8bit,
  586. aitconst_rva_symbol,
  587. aitconst_secrel32_symbol :
  588. begin
  589. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  590. l:=0;
  591. repeat
  592. if assigned(tai_const(hp).sym) then
  593. begin
  594. if SmartAsm then
  595. begin
  596. AddSymbol(tai_const(hp).sym.name,false);
  597. if assigned(tai_const(hp).endsym) then
  598. AddSymbol(tai_const(hp).endsym.name,false);
  599. end;
  600. if assigned(tai_const(hp).endsym) then
  601. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  602. else
  603. s:=tai_const(hp).sym.name;
  604. if tai_const(hp).value<>0 then
  605. s:=s+tostr_with_plus(tai_const(hp).value);
  606. end
  607. else
  608. s:=tostr(tai_const(hp).value);
  609. AsmWrite(s);
  610. inc(l,length(s));
  611. if (l>line_length) or
  612. (hp.next=nil) or
  613. (tai(hp.next).typ<>ait_const) or
  614. (tai_const(hp.next).consttype<>consttype) then
  615. break;
  616. hp:=tai(hp.next);
  617. AsmWrite(',');
  618. until false;
  619. AsmLn;
  620. end;
  621. else
  622. internalerror(200704252);
  623. end;
  624. end;
  625. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  626. ait_real_80bit :
  627. begin
  628. if do_line then
  629. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  630. { Make sure e is a extended type, bestreal could be
  631. a different type (bestreal) !! (PFV) }
  632. e:=tai_real_80bit(hp).value;
  633. AsmWrite(#9#9'DB'#9);
  634. for i:=0 to 9 do
  635. begin
  636. if i<>0 then
  637. AsmWrite(',');
  638. AsmWrite(tostr(t80bitarray(e)[i]));
  639. end;
  640. for i:=11 to tai_real_80bit(hp).savesize do
  641. AsmWrite(',0');
  642. AsmLn;
  643. end;
  644. {$else cpuextended}
  645. ait_real_80bit :
  646. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  647. {$endif cpuextended}
  648. // ait_real_64bit :
  649. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  650. ait_real_64bit :
  651. begin
  652. if do_line then
  653. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  654. d:=tai_real_64bit(hp).value;
  655. { swap the values to correct endian if required }
  656. if source_info.endian <> target_info.endian then
  657. swap64bitarray(t64bitarray(d));
  658. AsmWrite(#9#9'DB'#9);
  659. {$ifdef arm}
  660. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  661. begin
  662. for i:=4 to 7 do
  663. begin
  664. if i<>4 then
  665. AsmWrite(',');
  666. AsmWrite(tostr(t64bitarray(d)[i]));
  667. end;
  668. for i:=0 to 3 do
  669. begin
  670. AsmWrite(',');
  671. AsmWrite(tostr(t64bitarray(d)[i]));
  672. end;
  673. end
  674. else
  675. {$endif arm}
  676. begin
  677. for i:=0 to 7 do
  678. begin
  679. if i<>0 then
  680. AsmWrite(',');
  681. AsmWrite(tostr(t64bitarray(d)[i]));
  682. end;
  683. end;
  684. AsmLn;
  685. end;
  686. // ait_real_32bit :
  687. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  688. ait_real_32bit :
  689. begin
  690. if do_line then
  691. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  692. sin:=tai_real_32bit(hp).value;
  693. { swap the values to correct endian if required }
  694. if source_info.endian <> target_info.endian then
  695. swap32bitarray(t32bitarray(sin));
  696. AsmWrite(#9#9'DB'#9);
  697. for i:=0 to 3 do
  698. begin
  699. if i<>0 then
  700. AsmWrite(',');
  701. AsmWrite(tostr(t32bitarray(sin)[i]));
  702. end;
  703. AsmLn;
  704. end;
  705. // ait_comp_64bit :
  706. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  707. ait_comp_64bit :
  708. begin
  709. if do_line then
  710. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  711. AsmWrite(#9#9'DB'#9);
  712. co:=comp(tai_comp_64bit(hp).value);
  713. { swap the values to correct endian if required }
  714. if source_info.endian <> target_info.endian then
  715. swap64bitarray(t64bitarray(co));
  716. for i:=0 to 7 do
  717. begin
  718. if i<>0 then
  719. AsmWrite(',');
  720. AsmWrite(tostr(t64bitarray(co)[i]));
  721. end;
  722. AsmLn;
  723. end;
  724. ait_string :
  725. begin
  726. counter := 0;
  727. lines := tai_string(hp).len div line_length;
  728. { separate lines in different parts }
  729. if tai_string(hp).len > 0 then
  730. Begin
  731. for j := 0 to lines-1 do
  732. begin
  733. AsmWrite(#9#9'DB'#9);
  734. quoted:=false;
  735. for i:=counter to counter+line_length-1 do
  736. begin
  737. { it is an ascii character. }
  738. if (ord(tai_string(hp).str[i])>31) and
  739. (ord(tai_string(hp).str[i])<128) and
  740. (tai_string(hp).str[i]<>'"') then
  741. begin
  742. if not(quoted) then
  743. begin
  744. if i>counter then
  745. AsmWrite(',');
  746. AsmWrite('"');
  747. end;
  748. AsmWrite(tai_string(hp).str[i]);
  749. quoted:=true;
  750. end { if > 31 and < 128 and ord('"') }
  751. else
  752. begin
  753. if quoted then
  754. AsmWrite('"');
  755. if i>counter then
  756. AsmWrite(',');
  757. quoted:=false;
  758. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  759. end;
  760. end; { end for i:=0 to... }
  761. if quoted then AsmWrite('"');
  762. AsmWrite(target_info.newline);
  763. inc(counter,line_length);
  764. end; { end for j:=0 ... }
  765. { do last line of lines }
  766. if counter<tai_string(hp).len then
  767. AsmWrite(#9#9'DB'#9);
  768. quoted:=false;
  769. for i:=counter to tai_string(hp).len-1 do
  770. begin
  771. { it is an ascii character. }
  772. if (ord(tai_string(hp).str[i])>31) and
  773. (ord(tai_string(hp).str[i])<128) and
  774. (tai_string(hp).str[i]<>'"') then
  775. begin
  776. if not(quoted) then
  777. begin
  778. if i>counter then
  779. AsmWrite(',');
  780. AsmWrite('"');
  781. end;
  782. AsmWrite(tai_string(hp).str[i]);
  783. quoted:=true;
  784. end { if > 31 and < 128 and " }
  785. else
  786. begin
  787. if quoted then
  788. AsmWrite('"');
  789. if i>counter then
  790. AsmWrite(',');
  791. quoted:=false;
  792. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  793. end;
  794. end; { end for i:=0 to... }
  795. if quoted then
  796. AsmWrite('"');
  797. end;
  798. AsmLn;
  799. end;
  800. ait_label :
  801. begin
  802. if tai_label(hp).labsym.is_used then
  803. begin
  804. if SmartAsm and (tai_label(hp).labsym.bind=AB_GLOBAL) then
  805. begin
  806. AsmWrite(#9'GLOBAL ');
  807. AsmWriteLn(tai_label(hp).labsym.name);
  808. end;
  809. AsmWriteLn(tai_label(hp).labsym.name+':');
  810. end;
  811. if SmartAsm then
  812. AddSymbol(tai_label(hp).labsym.name,true);
  813. end;
  814. ait_symbol :
  815. begin
  816. if tai_symbol(hp).has_value then
  817. internalerror(2009090803);
  818. if tai_symbol(hp).is_global or SmartAsm then
  819. begin
  820. AsmWrite(#9'GLOBAL ');
  821. AsmWriteLn(tai_symbol(hp).sym.name);
  822. end;
  823. AsmWrite(tai_symbol(hp).sym.name);
  824. if SmartAsm then
  825. AddSymbol(tai_symbol(hp).sym.name,true);
  826. if assigned(hp.next) and not(tai(hp.next).typ in
  827. [ait_const,
  828. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  829. AsmWriteLn(':')
  830. end;
  831. ait_symbol_end : ;
  832. ait_instruction :
  833. begin
  834. fixed_opcode:=taicpu(hp).FixNonCommutativeOpcodes;
  835. { We need intel order, no At&t }
  836. taicpu(hp).SetOperandOrder(op_intel);
  837. s:='';
  838. if ((fixed_opcode=A_FADDP) or
  839. (fixed_opcode=A_FMULP))
  840. and (taicpu(hp).ops=0) then
  841. begin
  842. taicpu(hp).allocate_oper(2);
  843. taicpu(hp).oper[0]^.typ:=top_reg;
  844. taicpu(hp).oper[0]^.reg:=NR_ST1;
  845. taicpu(hp).oper[1]^.typ:=top_reg;
  846. taicpu(hp).oper[1]^.reg:=NR_ST;
  847. end;
  848. if fixed_opcode=A_FWAIT then
  849. AsmWriteln(#9#9'DB'#9'09bh')
  850. else
  851. begin
  852. {$ifndef i8086}
  853. { We need to explicitely set
  854. word prefix to get selectors
  855. to be pushed in 2 bytes PM }
  856. if (taicpu(hp).opsize=S_W) and
  857. ((fixed_opcode=A_PUSH) or
  858. (fixed_opcode=A_POP)) and
  859. (taicpu(hp).oper[0]^.typ=top_reg) and
  860. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  861. AsmWriteln(#9#9'DB'#9'066h');
  862. {$endif not i8086}
  863. AsmWrite(#9#9+std_op2str[fixed_opcode]+cond2str[taicpu(hp).condition]);
  864. if taicpu(hp).ops<>0 then
  865. begin
  866. if is_calljmp(fixed_opcode) then
  867. begin
  868. AsmWrite(#9);
  869. WriteOper_jmp(taicpu(hp).oper[0]^,fixed_opcode);
  870. end
  871. else
  872. begin
  873. for i:=0 to taicpu(hp).ops-1 do
  874. begin
  875. if i=0 then
  876. AsmWrite(#9)
  877. else
  878. AsmWrite(',');
  879. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,fixed_opcode,taicpu(hp).ops,(i=2));
  880. end;
  881. end;
  882. end;
  883. AsmLn;
  884. end;
  885. end;
  886. ait_stab,
  887. ait_force_line,
  888. ait_function_name : ;
  889. ait_cutobject :
  890. begin
  891. if SmartAsm then
  892. begin
  893. { only reset buffer if nothing has changed }
  894. if AsmSize=AsmStartSize then
  895. AsmClear
  896. else
  897. begin
  898. if SmartAsm then
  899. begin
  900. WriteSmartExternals;
  901. FreeExternChainList;
  902. end;
  903. AsmClose;
  904. DoAssemble;
  905. AsmCreate(tai_cutobject(hp).place);
  906. WriteHeader;
  907. end;
  908. { avoid empty files }
  909. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  910. begin
  911. if tai(hp.next).typ=ait_section then
  912. lasTSectype:=tai_section(hp.next).sectype;
  913. hp:=tai(hp.next);
  914. end;
  915. if lasTSectype<>sec_none then
  916. WriteSection(lasTSectype,'');
  917. AsmStartSize:=AsmSize;
  918. end;
  919. end;
  920. ait_marker :
  921. if tai_marker(hp).kind=mark_NoLineInfoStart then
  922. inc(InlineLevel)
  923. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  924. dec(InlineLevel);
  925. ait_directive :
  926. begin
  927. case tai_directive(hp).directive of
  928. asd_nasm_import :
  929. AsmWrite('import ');
  930. asd_extern :
  931. AsmWrite('EXTERN ');
  932. else
  933. internalerror(200509191);
  934. end;
  935. if tai_directive(hp).name<>'' then
  936. begin
  937. if SmartAsm then
  938. AddSymbol(tai_directive(hp).name,false);
  939. AsmWrite(tai_directive(hp).name);
  940. end;
  941. AsmLn;
  942. end;
  943. ait_seh_directive :
  944. { Ignore for now };
  945. else
  946. internalerror(10000);
  947. end;
  948. hp:=tai(hp.next);
  949. end;
  950. end;
  951. procedure T386NasmAssembler.WriteExternals;
  952. var
  953. sym : TAsmSymbol;
  954. i : longint;
  955. begin
  956. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  957. begin
  958. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  959. if sym.bind=AB_EXTERNAL then
  960. AsmWriteln('EXTERN'#9+sym.name);
  961. end;
  962. end;
  963. procedure T386NasmAssembler.WriteSmartExternals;
  964. var
  965. EC : PExternChain;
  966. begin
  967. EC:=FEC;
  968. while assigned(EC) do
  969. begin
  970. if not EC^.is_defined then
  971. AsmWriteln('EXTERN'#9+EC^.psym^);
  972. EC:=EC^.next;
  973. end;
  974. end;
  975. procedure T386NasmAssembler.WriteHeader;
  976. begin
  977. {$ifdef i8086}
  978. AsmWriteLn('BITS 16');
  979. case current_settings.cputype of
  980. cpu_8086: AsmWriteLn('CPU 8086');
  981. cpu_186: AsmWriteLn('CPU 186');
  982. cpu_286: AsmWriteLn('CPU 286');
  983. cpu_386: AsmWriteLn('CPU 386');
  984. cpu_Pentium: AsmWriteLn('CPU PENTIUM');
  985. cpu_Pentium2: AsmWriteLn('CPU P2');
  986. cpu_Pentium3: AsmWriteLn('CPU P3');
  987. cpu_Pentium4: AsmWriteLn('CPU P4');
  988. cpu_PentiumM: AsmWriteLn('CPU P4');
  989. else
  990. internalerror(2013050101);
  991. end;
  992. if current_settings.x86memorymodel in [mm_small,mm_tiny] then
  993. begin
  994. { NASM complains if you put a missing section in the GROUP directive, so }
  995. { we add empty declarations to make sure they exist, even if empty }
  996. if current_settings.x86memorymodel=mm_tiny then
  997. AsmWriteLn('SECTION .text');
  998. AsmWriteLn('SECTION .rodata');
  999. AsmWriteLn('SECTION .data');
  1000. { WLINK requires class=bss in order to leave the BSS section out of the executable }
  1001. AsmWriteLn('SECTION .bss class=bss');
  1002. { group these sections in the same segment }
  1003. if current_settings.x86memorymodel=mm_tiny then
  1004. AsmWriteLn('GROUP dgroup text rodata data bss')
  1005. else
  1006. AsmWriteLn('GROUP dgroup rodata data bss');
  1007. AsmWriteLn('SECTION .text');
  1008. end;
  1009. {$else i8086}
  1010. AsmWriteLn('BITS 32');
  1011. {$endif i8086}
  1012. end;
  1013. procedure T386NasmAssembler.WriteAsmList;
  1014. var
  1015. hal : tasmlisttype;
  1016. begin
  1017. {$ifdef EXTDEBUG}
  1018. if current_module.mainsource<>'' then
  1019. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource);
  1020. {$endif}
  1021. WriteHeader;
  1022. AsmLn;
  1023. WriteExternals;
  1024. for hal:=low(TasmlistType) to high(TasmlistType) do
  1025. begin
  1026. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1027. writetree(current_asmdata.asmlists[hal]);
  1028. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1029. end;
  1030. AsmLn;
  1031. if SmartAsm then
  1032. begin
  1033. WriteSmartExternals;
  1034. FreeExternChainList;
  1035. end;
  1036. {$ifdef EXTDEBUG}
  1037. if current_module.mainsource<>'' then
  1038. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource);
  1039. {$endif EXTDEBUG}
  1040. end;
  1041. {*****************************************************************************
  1042. Initialize
  1043. *****************************************************************************}
  1044. const
  1045. as_i386_nasmcoff_info : tasminfo =
  1046. (
  1047. id : as_i386_nasmcoff;
  1048. idtxt : 'NASMCOFF';
  1049. asmbin : 'nasm';
  1050. asmcmd : '-f coff -o $OBJ $ASM';
  1051. supported_targets : [system_i386_go32v2];
  1052. flags : [af_needar,af_no_debug];
  1053. labelprefix : '..@';
  1054. comment : '; ';
  1055. dollarsign: '$';
  1056. );
  1057. as_i386_nasmwin32_info : tasminfo =
  1058. (
  1059. id : as_i386_nasmwin32;
  1060. idtxt : 'NASMWIN32';
  1061. asmbin : 'nasm';
  1062. asmcmd : '-f win32 -o $OBJ $ASM';
  1063. supported_targets : [system_i386_win32];
  1064. flags : [af_needar,af_no_debug];
  1065. labelprefix : '..@';
  1066. comment : '; ';
  1067. dollarsign: '$';
  1068. );
  1069. as_i386_nasmobj_info : tasminfo =
  1070. (
  1071. id : as_i386_nasmobj;
  1072. idtxt : 'NASMOBJ';
  1073. asmbin : 'nasm';
  1074. asmcmd : '-f obj -o $OBJ $ASM';
  1075. supported_targets : [system_i386_embedded, system_i8086_msdos];
  1076. flags : [af_needar,af_no_debug];
  1077. labelprefix : '..@';
  1078. comment : '; ';
  1079. dollarsign: '$';
  1080. );
  1081. as_i386_nasmwdosx_info : tasminfo =
  1082. (
  1083. id : as_i386_nasmwdosx;
  1084. idtxt : 'NASMWDOSX';
  1085. asmbin : 'nasm';
  1086. asmcmd : '-f win32 -o $OBJ $ASM';
  1087. supported_targets : [system_i386_wdosx];
  1088. flags : [af_needar,af_no_debug];
  1089. labelprefix : '..@';
  1090. comment : '; ';
  1091. dollarsign: '$';
  1092. );
  1093. as_i386_nasmelf_info : tasminfo =
  1094. (
  1095. id : as_i386_nasmelf;
  1096. idtxt : 'NASMELF';
  1097. asmbin : 'nasm';
  1098. asmcmd : '-f elf -o $OBJ $ASM';
  1099. supported_targets : [system_i386_linux];
  1100. flags : [af_needar,af_no_debug];
  1101. labelprefix : '..@';
  1102. comment : '; ';
  1103. dollarsign: '$';
  1104. );
  1105. as_i386_nasmbeos_info : tasminfo =
  1106. (
  1107. id : as_i386_nasmbeos;
  1108. idtxt : 'NASMELF';
  1109. asmbin : 'nasm';
  1110. asmcmd : '-f elf -o $OBJ $ASM';
  1111. supported_targets : [system_i386_beos];
  1112. flags : [af_needar,af_no_debug];
  1113. labelprefix : '..@';
  1114. comment : '; ';
  1115. dollarsign: '$';
  1116. );
  1117. as_i386_nasmhaiku_info : tasminfo =
  1118. (
  1119. id : as_i386_nasmhaiku;
  1120. idtxt : 'NASMELF';
  1121. asmbin : 'nasm';
  1122. asmcmd : '-f elf -o $OBJ $ASM';
  1123. supported_targets : [system_i386_haiku];
  1124. flags : [af_needar,af_no_debug];
  1125. labelprefix : '..@';
  1126. comment : '; ';
  1127. dollarsign: '$';
  1128. );
  1129. initialization
  1130. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1131. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1132. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1133. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1134. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1135. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1136. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1137. end.