ag386nsm.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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 ag386nsm;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cpubase,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. T386NasmAssembler = class(texternalassembler)
  26. private
  27. procedure WriteReference(var ref : treference);
  28. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  29. procedure WriteOper_jmp(const o:toper; op : tasmop);
  30. procedure WriteSection(atype:TAsmSectiontype;const aname:string);
  31. public
  32. procedure WriteTree(p:TAsmList);override;
  33. procedure WriteAsmList;override;
  34. procedure WriteExternals;
  35. end;
  36. implementation
  37. uses
  38. cutils,globtype,globals,systems,cclasses,
  39. fmodule,finput,verbose,cpuinfo,cgbase
  40. ;
  41. type
  42. {$ifdef cpuextended}
  43. t80bitarray = array[0..9] of byte;
  44. {$endif cpuextended}
  45. t64bitarray = array[0..7] of byte;
  46. t32bitarray = array[0..3] of byte;
  47. const
  48. line_length = 64;
  49. nasm_regname_table : array[tregisterindex] of string[7] = (
  50. {r386nasm.inc contains the Nasm name of each register.}
  51. {$i r386nasm.inc}
  52. );
  53. function nasm_regname(r:Tregister):string;
  54. var
  55. p : tregisterindex;
  56. begin
  57. p:=findreg_by_number(r);
  58. if p<>0 then
  59. result:=nasm_regname_table[p]
  60. else
  61. result:=generic_regname(r);
  62. end;
  63. function fixline(s:string):string;
  64. {
  65. return s with all leading and ending spaces and tabs removed
  66. }
  67. var
  68. i,j,k : longint;
  69. begin
  70. i:=length(s);
  71. while (i>0) and (s[i] in [#9,' ']) do
  72. dec(i);
  73. j:=1;
  74. while (j<i) and (s[j] in [#9,' ']) do
  75. inc(j);
  76. for k:=j to i do
  77. if s[k] in [#0..#31,#127..#255] then
  78. s[k]:='.';
  79. fixline:=Copy(s,j,i-j+1);
  80. end;
  81. function single2str(d : single) : string;
  82. var
  83. hs : string;
  84. p : longint;
  85. begin
  86. str(d,hs);
  87. { nasm expects a lowercase e }
  88. p:=pos('E',hs);
  89. if p>0 then
  90. hs[p]:='e';
  91. p:=pos('+',hs);
  92. if p>0 then
  93. delete(hs,p,1);
  94. single2str:=lower(hs);
  95. end;
  96. function double2str(d : double) : string;
  97. var
  98. hs : string;
  99. p : longint;
  100. begin
  101. str(d,hs);
  102. { nasm expects a lowercase e }
  103. p:=pos('E',hs);
  104. if p>0 then
  105. hs[p]:='e';
  106. p:=pos('+',hs);
  107. if p>0 then
  108. delete(hs,p,1);
  109. double2str:=lower(hs);
  110. end;
  111. function extended2str(e : extended) : string;
  112. var
  113. hs : string;
  114. p : longint;
  115. begin
  116. str(e,hs);
  117. { nasm expects a lowercase e }
  118. p:=pos('E',hs);
  119. if p>0 then
  120. hs[p]:='e';
  121. p:=pos('+',hs);
  122. if p>0 then
  123. delete(hs,p,1);
  124. extended2str:=lower(hs);
  125. end;
  126. { convert floating point values }
  127. { to correct endian }
  128. procedure swap64bitarray(var t: t64bitarray);
  129. var
  130. b: byte;
  131. begin
  132. b:= t[7];
  133. t[7] := t[0];
  134. t[0] := b;
  135. b := t[6];
  136. t[6] := t[1];
  137. t[1] := b;
  138. b:= t[5];
  139. t[5] := t[2];
  140. t[2] := b;
  141. b:= t[4];
  142. t[4] := t[3];
  143. t[3] := b;
  144. end;
  145. procedure swap32bitarray(var t: t32bitarray);
  146. var
  147. b: byte;
  148. begin
  149. b:= t[1];
  150. t[1]:= t[2];
  151. t[2]:= b;
  152. b:= t[0];
  153. t[0]:= t[3];
  154. t[3]:= b;
  155. end;
  156. function comp2str(d : bestreal) : string;
  157. type
  158. pdouble = ^double;
  159. var
  160. c : comp;
  161. dd : pdouble;
  162. begin
  163. c:=comp(d);
  164. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  165. comp2str:=double2str(dd^);
  166. end;
  167. function sizestr(s:topsize;dest:boolean):string;
  168. begin
  169. case s of
  170. S_B : sizestr:='byte ';
  171. S_W : sizestr:='word ';
  172. S_L : sizestr:='dword ';
  173. S_Q : sizestr:='qword ';
  174. S_IS : sizestr:='word ';
  175. S_IL : sizestr:='dword ';
  176. S_IQ : sizestr:='qword ';
  177. S_FS : sizestr:='dword ';
  178. S_FL : sizestr:='qword ';
  179. S_FX : sizestr:='tword ';
  180. S_BW : if dest then
  181. sizestr:='word '
  182. else
  183. sizestr:='byte ';
  184. S_BL : if dest then
  185. sizestr:='dword '
  186. else
  187. sizestr:='byte ';
  188. S_WL : if dest then
  189. sizestr:='dword '
  190. else
  191. sizestr:='word ';
  192. else { S_NO }
  193. sizestr:='';
  194. end;
  195. end;
  196. Function PadTabs(const p:string;addch:char):string;
  197. var
  198. s : string;
  199. i : longint;
  200. begin
  201. i:=length(p);
  202. if addch<>#0 then
  203. begin
  204. inc(i);
  205. s:=p+addch;
  206. end
  207. else
  208. s:=p;
  209. if i<8 then
  210. PadTabs:=s+#9#9
  211. else
  212. PadTabs:=s+#9;
  213. end;
  214. {****************************************************************************
  215. T386NasmAssembler
  216. ****************************************************************************}
  217. procedure T386NasmAssembler.WriteReference(var ref : treference);
  218. var
  219. first : boolean;
  220. begin
  221. with ref do
  222. begin
  223. AsmWrite('[');
  224. first:=true;
  225. if (segment<>NR_NO) then
  226. AsmWrite(nasm_regname(segment)+':');
  227. if assigned(symbol) then
  228. begin
  229. AsmWrite(symbol.name);
  230. first:=false;
  231. end;
  232. if (base<>NR_NO) then
  233. begin
  234. if not(first) then
  235. AsmWrite('+')
  236. else
  237. first:=false;
  238. AsmWrite(nasm_regname(base))
  239. end;
  240. if (index<>NR_NO) then
  241. begin
  242. if not(first) then
  243. AsmWrite('+')
  244. else
  245. first:=false;
  246. AsmWrite(nasm_regname(index));
  247. if scalefactor<>0 then
  248. AsmWrite('*'+tostr(scalefactor));
  249. end;
  250. if offset<0 then
  251. begin
  252. AsmWrite(tostr(offset));
  253. first:=false;
  254. end
  255. else if (offset>0) then
  256. begin
  257. AsmWrite('+'+tostr(offset));
  258. first:=false;
  259. end;
  260. if first then
  261. AsmWrite('0');
  262. AsmWrite(']');
  263. end;
  264. end;
  265. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  266. begin
  267. case o.typ of
  268. top_reg :
  269. AsmWrite(nasm_regname(o.reg));
  270. top_const :
  271. begin
  272. if (ops=1) and (opcode<>A_RET) then
  273. AsmWrite(sizestr(s,dest));
  274. AsmWrite(tostr(longint(o.val)));
  275. end;
  276. top_ref :
  277. begin
  278. if o.ref^.refaddr=addr_no then
  279. begin
  280. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  281. (opcode = A_LSS) or (opcode = A_LFS) or
  282. (opcode = A_LES) or (opcode = A_LDS) or
  283. //(opcode = A_SHR) or (opcode = A_SHL) or
  284. (opcode = A_SAR) or (opcode = A_SAL) or
  285. (opcode = A_OUT) or (opcode = A_IN)) then
  286. AsmWrite(sizestr(s,dest));
  287. WriteReference(o.ref^);
  288. end
  289. else
  290. begin
  291. asmwrite('dword ');
  292. if assigned(o.ref^.symbol) then
  293. begin
  294. asmwrite(o.ref^.symbol.name);
  295. if o.ref^.offset=0 then
  296. exit;
  297. end;
  298. if o.ref^.offset>0 then
  299. asmwrite('+');
  300. asmwrite(tostr(o.ref^.offset));
  301. end;
  302. end;
  303. else
  304. internalerror(10001);
  305. end;
  306. end;
  307. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  308. begin
  309. case o.typ of
  310. top_reg :
  311. AsmWrite(nasm_regname(o.reg));
  312. top_ref :
  313. if o.ref^.refaddr=addr_no then
  314. WriteReference(o.ref^)
  315. else
  316. begin
  317. if not(
  318. (op=A_JCXZ) or (op=A_JECXZ) or
  319. {$ifdef x86_64}
  320. (op=A_JRCXZ) or
  321. {$endif x86_64}
  322. (op=A_LOOP) or (op=A_LOOPE) or
  323. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  324. (op=A_LOOPZ)
  325. ) then
  326. AsmWrite('NEAR ');
  327. AsmWrite(o.ref^.symbol.name);
  328. if o.ref^.offset>0 then
  329. AsmWrite('+'+tostr(o.ref^.offset))
  330. else
  331. if o.ref^.offset<0 then
  332. AsmWrite(tostr(o.ref^.offset));
  333. end;
  334. top_const :
  335. AsmWrite(tostr(aint(o.val)));
  336. else
  337. internalerror(10001);
  338. end;
  339. end;
  340. const
  341. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  342. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  343. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  344. #9'RVA'#9,#9'SECREL32'#9,#9'FIXMEINDIRECT'#9
  345. );
  346. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  347. const
  348. secnames : array[TAsmSectiontype] of string[17] = ('',
  349. '.text',
  350. '.data',
  351. '.data',
  352. '.rodata',
  353. '.bss',
  354. '.tbss',
  355. '.pdata',
  356. '.text',
  357. '.stab',
  358. '.stabstr',
  359. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  360. '.eh_frame',
  361. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  362. '.fpc',
  363. '',
  364. '.init',
  365. '.fini'
  366. );
  367. begin
  368. AsmLn;
  369. AsmWrite('SECTION ');
  370. AsmWrite(secnames[atype]);
  371. if create_smartlink_sections and
  372. (atype<>sec_bss) and
  373. (aname<>'') then
  374. begin
  375. AsmWrite('.');
  376. AsmWrite(aname);
  377. end;
  378. AsmLn;
  379. LasTSecType:=atype;
  380. end;
  381. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  382. {$ifdef cpuextended}
  383. type
  384. t80bitarray = array[0..9] of byte;
  385. {$endif cpuextended}
  386. var
  387. s : string;
  388. hp : tai;
  389. hp1 : tailineinfo;
  390. counter,
  391. lines,
  392. i,j,l : longint;
  393. InlineLevel : longint;
  394. consttype : taiconst_type;
  395. do_line,
  396. quoted : boolean;
  397. co : comp;
  398. sin : single;
  399. d : double;
  400. {$ifdef cpuextended}
  401. e : extended;
  402. {$endif cpuextended}
  403. begin
  404. if not assigned(p) then
  405. exit;
  406. InlineLevel:=0;
  407. { lineinfo is only needed for al_procedures (PFV) }
  408. do_line:=(cs_asm_source in current_settings.globalswitches) or
  409. ((cs_lineinfo in current_settings.moduleswitches)
  410. and (p=current_asmdata.asmlists[al_procedures]));
  411. hp:=tai(p.first);
  412. while assigned(hp) do
  413. begin
  414. if not(hp.typ in SkipLineInfo) then
  415. begin
  416. hp1:=hp as tailineinfo;
  417. current_filepos:=hp1.fileinfo;
  418. if do_line then
  419. begin
  420. { load infile }
  421. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  422. begin
  423. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  424. if assigned(infile) then
  425. begin
  426. { open only if needed !! }
  427. if (cs_asm_source in current_settings.globalswitches) then
  428. infile.open;
  429. end;
  430. { avoid unnecessary reopens of the same file !! }
  431. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  432. { be sure to change line !! }
  433. lastfileinfo.line:=-1;
  434. end;
  435. { write source }
  436. if (cs_asm_source in current_settings.globalswitches) and
  437. assigned(infile) then
  438. begin
  439. if (infile<>lastinfile) then
  440. begin
  441. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  442. if assigned(lastinfile) then
  443. lastinfile.close;
  444. end;
  445. if (hp1.fileinfo.line<>lastfileinfo.line) and
  446. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  447. begin
  448. if (hp1.fileinfo.line<>0) and
  449. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  450. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  451. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  452. { set it to a negative value !
  453. to make that is has been read already !! PM }
  454. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  455. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  456. end;
  457. end;
  458. lastfileinfo:=hp1.fileinfo;
  459. lastinfile:=infile;
  460. end;
  461. end;
  462. case hp.typ of
  463. ait_comment :
  464. Begin
  465. AsmWrite(target_asm.comment);
  466. AsmWritePChar(tai_comment(hp).str);
  467. AsmLn;
  468. End;
  469. ait_regalloc :
  470. begin
  471. if (cs_asm_regalloc in current_settings.globalswitches) then
  472. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  473. regallocstr[tai_regalloc(hp).ratype]);
  474. end;
  475. ait_tempalloc :
  476. begin
  477. if (cs_asm_tempalloc in current_settings.globalswitches) then
  478. begin
  479. {$ifdef EXTDEBUG}
  480. if assigned(tai_tempalloc(hp).problem) then
  481. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  482. tostr(tai_tempalloc(hp).tempsize)+')')
  483. else
  484. {$endif EXTDEBUG}
  485. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  486. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  487. end;
  488. end;
  489. ait_section :
  490. begin
  491. if tai_section(hp).sectype<>sec_none then
  492. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  493. LasTSecType:=tai_section(hp).sectype;
  494. end;
  495. ait_align :
  496. begin
  497. { nasm gives warnings when it finds align in bss as it
  498. wants to store data }
  499. if (lastsectype<>sec_bss) and
  500. (tai_align(hp).aligntype>1) then
  501. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  502. end;
  503. ait_datablock :
  504. begin
  505. if tai_datablock(hp).is_global then
  506. begin
  507. AsmWrite(#9'GLOBAL ');
  508. AsmWriteLn(tai_datablock(hp).sym.name);
  509. end;
  510. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  511. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  512. end;
  513. ait_const:
  514. begin
  515. consttype:=tai_const(hp).consttype;
  516. case consttype of
  517. aitconst_64bit :
  518. begin
  519. if assigned(tai_const(hp).sym) then
  520. internalerror(200404292);
  521. AsmWrite(ait_const2str[aitconst_32bit]);
  522. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  523. AsmWrite(',');
  524. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  525. AsmLn;
  526. end;
  527. aitconst_uleb128bit,
  528. aitconst_sleb128bit,
  529. aitconst_128bit:
  530. begin
  531. end;
  532. aitconst_32bit,
  533. aitconst_16bit,
  534. aitconst_8bit,
  535. aitconst_rva_symbol,
  536. aitconst_secrel32_symbol,
  537. aitconst_indirect_symbol :
  538. begin
  539. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  540. l:=0;
  541. repeat
  542. if assigned(tai_const(hp).sym) then
  543. begin
  544. if assigned(tai_const(hp).endsym) then
  545. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  546. else
  547. s:=tai_const(hp).sym.name;
  548. if tai_const(hp).value<>0 then
  549. s:=s+tostr_with_plus(tai_const(hp).value);
  550. end
  551. else
  552. s:=tostr(tai_const(hp).value);
  553. AsmWrite(s);
  554. inc(l,length(s));
  555. if (l>line_length) or
  556. (hp.next=nil) or
  557. (tai(hp.next).typ<>ait_const) or
  558. (tai_const(hp.next).consttype<>consttype) then
  559. break;
  560. hp:=tai(hp.next);
  561. AsmWrite(',');
  562. until false;
  563. AsmLn;
  564. end;
  565. else
  566. internalerror(200704252);
  567. end;
  568. end;
  569. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  570. ait_real_80bit :
  571. begin
  572. if do_line then
  573. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  574. { Make sure e is a extended type, bestreal could be
  575. a different type (bestreal) !! (PFV) }
  576. e:=tai_real_80bit(hp).value;
  577. AsmWrite(#9#9'DB'#9);
  578. for i:=0 to 9 do
  579. begin
  580. if i<>0 then
  581. AsmWrite(',');
  582. AsmWrite(tostr(t80bitarray(e)[i]));
  583. end;
  584. AsmLn;
  585. end;
  586. {$else cpuextended}
  587. ait_real_80bit :
  588. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  589. {$endif cpuextended}
  590. // ait_real_64bit :
  591. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  592. ait_real_64bit :
  593. begin
  594. if do_line then
  595. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  596. d:=tai_real_64bit(hp).value;
  597. { swap the values to correct endian if required }
  598. if source_info.endian <> target_info.endian then
  599. swap64bitarray(t64bitarray(d));
  600. AsmWrite(#9#9'DB'#9);
  601. {$ifdef arm}
  602. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  603. begin
  604. for i:=4 to 7 do
  605. begin
  606. if i<>4 then
  607. AsmWrite(',');
  608. AsmWrite(tostr(t64bitarray(d)[i]));
  609. end;
  610. for i:=0 to 3 do
  611. begin
  612. AsmWrite(',');
  613. AsmWrite(tostr(t64bitarray(d)[i]));
  614. end;
  615. end
  616. else
  617. {$endif arm}
  618. begin
  619. for i:=0 to 7 do
  620. begin
  621. if i<>0 then
  622. AsmWrite(',');
  623. AsmWrite(tostr(t64bitarray(d)[i]));
  624. end;
  625. end;
  626. AsmLn;
  627. end;
  628. // ait_real_32bit :
  629. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  630. ait_real_32bit :
  631. begin
  632. if do_line then
  633. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  634. sin:=tai_real_32bit(hp).value;
  635. { swap the values to correct endian if required }
  636. if source_info.endian <> target_info.endian then
  637. swap32bitarray(t32bitarray(sin));
  638. AsmWrite(#9#9'DB'#9);
  639. for i:=0 to 3 do
  640. begin
  641. if i<>0 then
  642. AsmWrite(',');
  643. AsmWrite(tostr(t32bitarray(sin)[i]));
  644. end;
  645. AsmLn;
  646. end;
  647. // ait_comp_64bit :
  648. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  649. ait_comp_64bit :
  650. begin
  651. if do_line then
  652. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  653. AsmWrite(#9#9'DB'#9);
  654. co:=comp(tai_comp_64bit(hp).value);
  655. { swap the values to correct endian if required }
  656. if source_info.endian <> target_info.endian then
  657. swap64bitarray(t64bitarray(co));
  658. for i:=0 to 7 do
  659. begin
  660. if i<>0 then
  661. AsmWrite(',');
  662. AsmWrite(tostr(t64bitarray(co)[i]));
  663. end;
  664. AsmLn;
  665. end;
  666. ait_string :
  667. begin
  668. counter := 0;
  669. lines := tai_string(hp).len div line_length;
  670. { separate lines in different parts }
  671. if tai_string(hp).len > 0 then
  672. Begin
  673. for j := 0 to lines-1 do
  674. begin
  675. AsmWrite(#9#9'DB'#9);
  676. quoted:=false;
  677. for i:=counter to counter+line_length-1 do
  678. begin
  679. { it is an ascii character. }
  680. if (ord(tai_string(hp).str[i])>31) and
  681. (ord(tai_string(hp).str[i])<128) and
  682. (tai_string(hp).str[i]<>'"') then
  683. begin
  684. if not(quoted) then
  685. begin
  686. if i>counter then
  687. AsmWrite(',');
  688. AsmWrite('"');
  689. end;
  690. AsmWrite(tai_string(hp).str[i]);
  691. quoted:=true;
  692. end { if > 31 and < 128 and ord('"') }
  693. else
  694. begin
  695. if quoted then
  696. AsmWrite('"');
  697. if i>counter then
  698. AsmWrite(',');
  699. quoted:=false;
  700. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  701. end;
  702. end; { end for i:=0 to... }
  703. if quoted then AsmWrite('"');
  704. AsmWrite(target_info.newline);
  705. inc(counter,line_length);
  706. end; { end for j:=0 ... }
  707. { do last line of lines }
  708. if counter<tai_string(hp).len then
  709. AsmWrite(#9#9'DB'#9);
  710. quoted:=false;
  711. for i:=counter to tai_string(hp).len-1 do
  712. begin
  713. { it is an ascii character. }
  714. if (ord(tai_string(hp).str[i])>31) and
  715. (ord(tai_string(hp).str[i])<128) and
  716. (tai_string(hp).str[i]<>'"') then
  717. begin
  718. if not(quoted) then
  719. begin
  720. if i>counter then
  721. AsmWrite(',');
  722. AsmWrite('"');
  723. end;
  724. AsmWrite(tai_string(hp).str[i]);
  725. quoted:=true;
  726. end { if > 31 and < 128 and " }
  727. else
  728. begin
  729. if quoted then
  730. AsmWrite('"');
  731. if i>counter then
  732. AsmWrite(',');
  733. quoted:=false;
  734. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  735. end;
  736. end; { end for i:=0 to... }
  737. if quoted then
  738. AsmWrite('"');
  739. end;
  740. AsmLn;
  741. end;
  742. ait_label :
  743. begin
  744. if tai_label(hp).labsym.is_used then
  745. AsmWriteLn(tai_label(hp).labsym.name+':');
  746. end;
  747. ait_symbol :
  748. begin
  749. if tai_symbol(hp).is_global then
  750. begin
  751. AsmWrite(#9'GLOBAL ');
  752. AsmWriteLn(tai_symbol(hp).sym.name);
  753. end;
  754. AsmWrite(tai_symbol(hp).sym.name);
  755. if assigned(hp.next) and not(tai(hp.next).typ in
  756. [ait_const,
  757. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  758. AsmWriteLn(':')
  759. end;
  760. ait_symbol_end : ;
  761. ait_instruction :
  762. begin
  763. taicpu(hp).CheckNonCommutativeOpcodes;
  764. { We need intel order, no At&t }
  765. taicpu(hp).SetOperandOrder(op_intel);
  766. s:='';
  767. if ((taicpu(hp).opcode=A_FADDP) or
  768. (taicpu(hp).opcode=A_FMULP))
  769. and (taicpu(hp).ops=0) then
  770. begin
  771. taicpu(hp).allocate_oper(2);
  772. taicpu(hp).oper[0]^.typ:=top_reg;
  773. taicpu(hp).oper[0]^.reg:=NR_ST1;
  774. taicpu(hp).oper[1]^.typ:=top_reg;
  775. taicpu(hp).oper[1]^.reg:=NR_ST;
  776. end;
  777. if taicpu(hp).opcode=A_FWAIT then
  778. AsmWriteln(#9#9'DB'#9'09bh')
  779. else
  780. begin
  781. { We need to explicitely set
  782. word prefix to get selectors
  783. to be pushed in 2 bytes PM }
  784. if (taicpu(hp).opsize=S_W) and
  785. ((taicpu(hp).opcode=A_PUSH) or
  786. (taicpu(hp).opcode=A_POP)) and
  787. (taicpu(hp).oper[0]^.typ=top_reg) and
  788. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  789. AsmWriteln(#9#9'DB'#9'066h');
  790. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  791. if taicpu(hp).ops<>0 then
  792. begin
  793. if is_calljmp(taicpu(hp).opcode) then
  794. begin
  795. AsmWrite(#9);
  796. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  797. end
  798. else
  799. begin
  800. for i:=0 to taicpu(hp).ops-1 do
  801. begin
  802. if i=0 then
  803. AsmWrite(#9)
  804. else
  805. AsmWrite(',');
  806. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  807. end;
  808. end;
  809. end;
  810. AsmLn;
  811. end;
  812. end;
  813. ait_stab,
  814. ait_force_line,
  815. ait_function_name : ;
  816. ait_cutobject :
  817. begin
  818. { only reset buffer if nothing has changed }
  819. if AsmSize=AsmStartSize then
  820. AsmClear
  821. else
  822. begin
  823. AsmClose;
  824. DoAssemble;
  825. AsmCreate(tai_cutobject(hp).place);
  826. end;
  827. { avoid empty files }
  828. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  829. begin
  830. if tai(hp.next).typ=ait_section then
  831. lasTSectype:=tai_section(hp.next).sectype;
  832. hp:=tai(hp.next);
  833. end;
  834. if lasTSectype<>sec_none then
  835. WriteSection(lasTSectype,'');
  836. AsmStartSize:=AsmSize;
  837. end;
  838. ait_marker :
  839. if tai_marker(hp).kind=mark_InlineStart then
  840. inc(InlineLevel)
  841. else if tai_marker(hp).kind=mark_InlineEnd then
  842. dec(InlineLevel);
  843. ait_directive :
  844. begin
  845. case tai_directive(hp).directive of
  846. asd_nasm_import :
  847. AsmWrite('import ');
  848. asd_extern :
  849. AsmWrite('EXTERN ');
  850. else
  851. internalerror(200509191);
  852. end;
  853. if assigned(tai_directive(hp).name) then
  854. AsmWrite(tai_directive(hp).name^);
  855. AsmLn;
  856. end;
  857. else
  858. internalerror(10000);
  859. end;
  860. hp:=tai(hp.next);
  861. end;
  862. end;
  863. procedure T386NasmAssembler.WriteExternals;
  864. var
  865. sym : TAsmSymbol;
  866. i : longint;
  867. begin
  868. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  869. begin
  870. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  871. if sym.bind=AB_EXTERNAL then
  872. AsmWriteln('EXTERN'#9+sym.name);
  873. end;
  874. end;
  875. procedure T386NasmAssembler.WriteAsmList;
  876. var
  877. hal : tasmlisttype;
  878. begin
  879. {$ifdef EXTDEBUG}
  880. if assigned(current_module.mainsource) then
  881. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  882. {$endif}
  883. AsmWriteLn('BITS 32');
  884. AsmLn;
  885. WriteExternals;
  886. for hal:=low(TasmlistType) to high(TasmlistType) do
  887. begin
  888. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  889. writetree(current_asmdata.asmlists[hal]);
  890. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  891. end;
  892. AsmLn;
  893. {$ifdef EXTDEBUG}
  894. if assigned(current_module.mainsource) then
  895. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  896. {$endif EXTDEBUG}
  897. end;
  898. {*****************************************************************************
  899. Initialize
  900. *****************************************************************************}
  901. const
  902. as_i386_nasmcoff_info : tasminfo =
  903. (
  904. id : as_i386_nasmcoff;
  905. idtxt : 'NASMCOFF';
  906. asmbin : 'nasm';
  907. asmcmd : '-f coff -o $OBJ $ASM';
  908. supported_target : system_i386_go32v2;
  909. flags : [af_allowdirect,af_needar,af_no_debug];
  910. labelprefix : '..@';
  911. comment : '; ';
  912. );
  913. as_i386_nasmwin32_info : tasminfo =
  914. (
  915. id : as_i386_nasmwin32;
  916. idtxt : 'NASMWIN32';
  917. asmbin : 'nasm';
  918. asmcmd : '-f win32 -o $OBJ $ASM';
  919. supported_target : system_i386_win32;
  920. flags : [af_allowdirect,af_needar,af_no_debug];
  921. labelprefix : '..@';
  922. comment : '; ';
  923. );
  924. as_i386_nasmobj_info : tasminfo =
  925. (
  926. id : as_i386_nasmobj;
  927. idtxt : 'NASMOBJ';
  928. asmbin : 'nasm';
  929. asmcmd : '-f obj -o $OBJ $ASM';
  930. supported_target : system_any; { what should I write here ?? }
  931. flags : [af_allowdirect,af_needar,af_no_debug];
  932. labelprefix : '..@';
  933. comment : '; ';
  934. );
  935. as_i386_nasmwdosx_info : tasminfo =
  936. (
  937. id : as_i386_nasmwdosx;
  938. idtxt : 'NASMWDOSX';
  939. asmbin : 'nasm';
  940. asmcmd : '-f win32 -o $OBJ $ASM';
  941. supported_target : system_i386_wdosx;
  942. flags : [af_allowdirect,af_needar,af_no_debug];
  943. labelprefix : '..@';
  944. comment : '; ';
  945. );
  946. as_i386_nasmelf_info : tasminfo =
  947. (
  948. id : as_i386_nasmelf;
  949. idtxt : 'NASMELF';
  950. asmbin : 'nasm';
  951. asmcmd : '-f elf -o $OBJ $ASM';
  952. supported_target : system_i386_linux;
  953. flags : [af_allowdirect,af_needar,af_no_debug];
  954. labelprefix : '..@';
  955. comment : '; ';
  956. );
  957. as_i386_nasmbeos_info : tasminfo =
  958. (
  959. id : as_i386_nasmbeos;
  960. idtxt : 'NASMELF';
  961. asmbin : 'nasm';
  962. asmcmd : '-f elf -o $OBJ $ASM';
  963. supported_target : system_i386_beos;
  964. flags : [af_allowdirect,af_needar,af_no_debug];
  965. labelprefix : '..@';
  966. comment : '; ';
  967. );
  968. initialization
  969. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  970. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  971. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  972. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  973. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  974. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  975. end.