ag386nsm.pas 37 KB

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