agx86nsm.pas 39 KB

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