ag386nsm.pas 35 KB

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