ag386nsm.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit implements an asmoutput class for the Nasm assembler with
  5. Intel syntax for the i386+
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ag386nsm;
  20. {$i defines.inc}
  21. interface
  22. uses aasm,assemble;
  23. type
  24. T386NasmAssembler = class(texternalassembler)
  25. procedure WriteTree(p:taasmoutput);override;
  26. procedure WriteAsmList;override;
  27. procedure WriteExternals;
  28. end;
  29. implementation
  30. uses
  31. {$ifdef delphi}
  32. sysutils,
  33. {$endif}
  34. cutils,globtype,globals,systems,cclasses,
  35. fmodule,finput,verbose,cpubase,cpuasm
  36. ;
  37. const
  38. line_length = 64;
  39. var
  40. lastfileinfo : tfileposinfo;
  41. infile,
  42. lastinfile : tinputfile;
  43. function fixline(s:string):string;
  44. {
  45. return s with all leading and ending spaces and tabs removed
  46. }
  47. var
  48. i,j,k : longint;
  49. begin
  50. i:=length(s);
  51. while (i>0) and (s[i] in [#9,' ']) do
  52. dec(i);
  53. j:=1;
  54. while (j<i) and (s[j] in [#9,' ']) do
  55. inc(j);
  56. for k:=j to i do
  57. if s[k] in [#0..#31,#127..#255] then
  58. s[k]:='.';
  59. fixline:=Copy(s,j,i-j+1);
  60. end;
  61. function single2str(d : single) : string;
  62. var
  63. hs : string;
  64. p : byte;
  65. begin
  66. str(d,hs);
  67. { nasm expects a lowercase e }
  68. p:=pos('E',hs);
  69. if p>0 then
  70. hs[p]:='e';
  71. p:=pos('+',hs);
  72. if p>0 then
  73. delete(hs,p,1);
  74. single2str:=lower(hs);
  75. end;
  76. function double2str(d : double) : string;
  77. var
  78. hs : string;
  79. p : byte;
  80. begin
  81. str(d,hs);
  82. { nasm expects a lowercase e }
  83. p:=pos('E',hs);
  84. if p>0 then
  85. hs[p]:='e';
  86. p:=pos('+',hs);
  87. if p>0 then
  88. delete(hs,p,1);
  89. double2str:=lower(hs);
  90. end;
  91. function extended2str(e : extended) : string;
  92. var
  93. hs : string;
  94. p : byte;
  95. begin
  96. str(e,hs);
  97. { nasm expects a lowercase e }
  98. p:=pos('E',hs);
  99. if p>0 then
  100. hs[p]:='e';
  101. p:=pos('+',hs);
  102. if p>0 then
  103. delete(hs,p,1);
  104. extended2str:=lower(hs);
  105. end;
  106. function comp2str(d : bestreal) : string;
  107. type
  108. pdouble = ^double;
  109. var
  110. c : comp;
  111. dd : pdouble;
  112. begin
  113. {$ifdef FPC}
  114. c:=comp(d);
  115. {$else}
  116. c:=d;
  117. {$endif}
  118. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  119. comp2str:=double2str(dd^);
  120. end;
  121. function getreferencestring(var ref : treference) : string;
  122. var
  123. s : string;
  124. first : boolean;
  125. begin
  126. if ref.is_immediate then
  127. begin
  128. getreferencestring:=tostr(ref.offset);
  129. exit;
  130. end
  131. else
  132. with ref do
  133. begin
  134. first:=true;
  135. inc(offset,offsetfixup);
  136. offsetfixup:=0;
  137. if ref.segment<>R_NO then
  138. s:='['+int_reg2str[segment]+':'
  139. else
  140. s:='[';
  141. if assigned(symbol) then
  142. begin
  143. s:=s+symbol.name;
  144. first:=false;
  145. end;
  146. if (base<>R_NO) then
  147. begin
  148. if not(first) then
  149. s:=s+'+'
  150. else
  151. first:=false;
  152. s:=s+int_reg2str[base];
  153. end;
  154. if (index<>R_NO) then
  155. begin
  156. if not(first) then
  157. s:=s+'+'
  158. else
  159. first:=false;
  160. s:=s+int_reg2str[index];
  161. if scalefactor<>0 then
  162. s:=s+'*'+tostr(scalefactor);
  163. end;
  164. if offset<0 then
  165. s:=s+tostr(offset)
  166. else if (offset>0) then
  167. s:=s+'+'+tostr(offset);
  168. if s[length(s)]='[' then
  169. s:=s+'0';
  170. s:=s+']';
  171. end;
  172. getreferencestring:=s;
  173. end;
  174. function sizestr(s:topsize;dest:boolean):string;
  175. begin
  176. case s of
  177. S_B : sizestr:='byte ';
  178. S_W : sizestr:='word ';
  179. S_L : sizestr:='dword ';
  180. S_IS : sizestr:='word ';
  181. S_IL : sizestr:='dword ';
  182. S_IQ : sizestr:='qword ';
  183. S_FS : sizestr:='dword ';
  184. S_FL : sizestr:='qword ';
  185. S_FX : sizestr:='tword ';
  186. S_BW : if dest then
  187. sizestr:='word '
  188. else
  189. sizestr:='byte ';
  190. S_BL : if dest then
  191. sizestr:='dword '
  192. else
  193. sizestr:='byte ';
  194. S_WL : if dest then
  195. sizestr:='dword '
  196. else
  197. sizestr:='word ';
  198. else { S_NO }
  199. sizestr:='';
  200. end;
  201. end;
  202. function getopstr(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean) : string;
  203. var
  204. hs : string;
  205. begin
  206. case o.typ of
  207. top_reg :
  208. getopstr:=int_nasmreg2str[o.reg];
  209. top_const :
  210. begin
  211. if (ops=1) and (opcode<>A_RET) then
  212. getopstr:=sizestr(s,dest)+tostr(o.val)
  213. else
  214. getopstr:=tostr(o.val);
  215. end;
  216. top_symbol :
  217. begin
  218. if assigned(o.sym) then
  219. hs:='dword '+o.sym.name
  220. else
  221. hs:='dword ';
  222. if o.symofs>0 then
  223. hs:=hs+'+'+tostr(o.symofs)
  224. else
  225. if o.symofs<0 then
  226. hs:=hs+tostr(o.symofs)
  227. else
  228. if not(assigned(o.sym)) then
  229. hs:=hs+'0';
  230. getopstr:=hs;
  231. end;
  232. top_ref :
  233. begin
  234. hs:=getreferencestring(o.ref^);
  235. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  236. (opcode = A_LSS) or (opcode = A_LFS) or
  237. (opcode = A_LES) or (opcode = A_LDS) or
  238. (opcode = A_SHR) or (opcode = A_SHL) or
  239. (opcode = A_SAR) or (opcode = A_SAL) or
  240. (opcode = A_OUT) or (opcode = A_IN)) then
  241. begin
  242. hs:=sizestr(s,dest)+hs;
  243. end;
  244. getopstr:=hs;
  245. end;
  246. else
  247. internalerror(10001);
  248. end;
  249. end;
  250. function getopstr_jmp(const o:toper; op : tasmop) : string;
  251. var
  252. hs : string;
  253. begin
  254. case o.typ of
  255. top_reg :
  256. getopstr_jmp:=int_nasmreg2str[o.reg];
  257. top_ref :
  258. getopstr_jmp:=getreferencestring(o.ref^);
  259. top_const :
  260. getopstr_jmp:=tostr(o.val);
  261. top_symbol :
  262. begin
  263. hs:=o.sym.name;
  264. if o.symofs>0 then
  265. hs:=hs+'+'+tostr(o.symofs)
  266. else
  267. if o.symofs<0 then
  268. hs:=hs+tostr(o.symofs);
  269. if (op=A_JCXZ) or (op=A_JECXZ) or
  270. (op=A_LOOP) or (op=A_LOOPE) or
  271. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  272. (op=A_LOOPZ) then
  273. getopstr_jmp:=hs
  274. else
  275. getopstr_jmp:='NEAR '+hs;
  276. end;
  277. else
  278. internalerror(10001);
  279. end;
  280. end;
  281. {****************************************************************************
  282. T386NasmAssembler
  283. ****************************************************************************}
  284. var
  285. LastSec : tsection;
  286. const
  287. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  288. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  289. Function PadTabs(const p:string;addch:char):string;
  290. var
  291. s : string;
  292. i : longint;
  293. begin
  294. i:=length(p);
  295. if addch<>#0 then
  296. begin
  297. inc(i);
  298. s:=p+addch;
  299. end
  300. else
  301. s:=p;
  302. if i<8 then
  303. PadTabs:=s+#9#9
  304. else
  305. PadTabs:=s+#9;
  306. end;
  307. procedure T386NasmAssembler.WriteTree(p:taasmoutput);
  308. const
  309. allocstr : array[boolean] of string[10]=(' released',' allocated');
  310. nolinetai =[ait_label,
  311. ait_regalloc,ait_tempalloc,
  312. ait_stabn,ait_stabs,ait_section,
  313. ait_cut,ait_marker,ait_align,ait_stab_function_name];
  314. var
  315. s : string;
  316. {prefix,
  317. suffix : string; no need here }
  318. hp : tai;
  319. counter,
  320. lines,
  321. i,j,l : longint;
  322. InlineLevel : longint;
  323. consttyp : tait;
  324. found,
  325. do_line,
  326. quoted : boolean;
  327. sep : char;
  328. begin
  329. if not assigned(p) then
  330. exit;
  331. InlineLevel:=0;
  332. { lineinfo is only needed for codesegment (PFV) }
  333. do_line:=(cs_asm_source in aktglobalswitches) or
  334. ((cs_lineinfo in aktmoduleswitches)
  335. and (p=codesegment));
  336. hp:=tai(p.first);
  337. while assigned(hp) do
  338. begin
  339. aktfilepos:=hp.fileinfo;
  340. if not(hp.typ in nolinetai) then
  341. begin
  342. if do_line then
  343. begin
  344. { load infile }
  345. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  346. begin
  347. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  348. if assigned(infile) then
  349. begin
  350. { open only if needed !! }
  351. if (cs_asm_source in aktglobalswitches) then
  352. infile.open;
  353. end;
  354. { avoid unnecessary reopens of the same file !! }
  355. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  356. { be sure to change line !! }
  357. lastfileinfo.line:=-1;
  358. end;
  359. { write source }
  360. if (cs_asm_source in aktglobalswitches) and
  361. assigned(infile) then
  362. begin
  363. if (infile<>lastinfile) then
  364. begin
  365. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  366. if assigned(lastinfile) then
  367. lastinfile.close;
  368. end;
  369. if (hp.fileinfo.line<>lastfileinfo.line) and
  370. ((hp.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  371. begin
  372. if (hp.fileinfo.line<>0) and
  373. ((infile.linebuf^[hp.fileinfo.line]>=0) or (InlineLevel>0)) then
  374. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  375. fixline(infile.GetLineStr(hp.fileinfo.line)));
  376. { set it to a negative value !
  377. to make that is has been read already !! PM }
  378. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  379. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  380. end;
  381. end;
  382. lastfileinfo:=hp.fileinfo;
  383. lastinfile:=infile;
  384. end;
  385. end;
  386. case hp.typ of
  387. ait_comment :
  388. Begin
  389. AsmWrite(target_asm.comment);
  390. AsmWritePChar(tai_asm_comment(hp).str);
  391. AsmLn;
  392. End;
  393. ait_regalloc :
  394. begin
  395. if (cs_asm_regalloc in aktglobalswitches) then
  396. AsmWriteLn(target_asm.comment+'Register '+att_reg2str[tairegalloc(hp).reg]+
  397. allocstr[tairegalloc(hp).allocation]);
  398. end;
  399. ait_tempalloc :
  400. begin
  401. if (cs_asm_tempalloc in aktglobalswitches) then
  402. AsmWriteLn(target_asm.comment+'Temp '+tostr(taitempalloc(hp).temppos)+','+
  403. tostr(taitempalloc(hp).tempsize)+allocstr[taitempalloc(hp).allocation]);
  404. end;
  405. ait_section :
  406. begin
  407. if tai_section(hp).sec<>sec_none then
  408. begin
  409. AsmLn;
  410. AsmWriteLn('SECTION '+target_asm.secnames[tai_section(hp).sec]);
  411. end;
  412. LastSec:=tai_section(hp).sec;
  413. end;
  414. ait_align :
  415. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  416. ait_datablock :
  417. begin
  418. if tai_datablock(hp).is_global then
  419. begin
  420. AsmWrite(#9'GLOBAL ');
  421. AsmWriteLn(tai_datablock(hp).sym.name);
  422. end;
  423. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  424. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  425. end;
  426. ait_const_32bit,
  427. ait_const_16bit,
  428. ait_const_8bit :
  429. begin
  430. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  431. consttyp:=hp.typ;
  432. l:=0;
  433. repeat
  434. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  435. if found then
  436. begin
  437. hp:=tai(hp.next);
  438. s:=','+tostr(tai_const(hp).value);
  439. AsmWrite(s);
  440. inc(l,length(s));
  441. end;
  442. until (not found) or (l>line_length);
  443. AsmLn;
  444. end;
  445. ait_const_symbol :
  446. begin
  447. AsmWrite(#9#9'DD'#9);
  448. AsmWrite(tai_const_symbol(hp).sym.name);
  449. if tai_const_symbol(hp).offset>0 then
  450. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  451. else if tai_const_symbol(hp).offset<0 then
  452. AsmWrite(tostr(tai_const_symbol(hp).offset));
  453. AsmLn;
  454. end;
  455. ait_const_rva :
  456. begin
  457. AsmWrite(#9#9'RVA'#9);
  458. AsmWriteLn(tai_const_symbol(hp).sym.name);
  459. end;
  460. ait_real_32bit :
  461. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  462. ait_real_64bit :
  463. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  464. ait_real_80bit :
  465. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  466. ait_comp_64bit :
  467. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  468. ait_string :
  469. begin
  470. counter := 0;
  471. lines := tai_string(hp).len div line_length;
  472. { separate lines in different parts }
  473. if tai_string(hp).len > 0 then
  474. Begin
  475. for j := 0 to lines-1 do
  476. begin
  477. AsmWrite(#9#9'DB'#9);
  478. quoted:=false;
  479. for i:=counter to counter+line_length-1 do
  480. begin
  481. { it is an ascii character. }
  482. if (ord(tai_string(hp).str[i])>31) and
  483. (ord(tai_string(hp).str[i])<128) and
  484. (tai_string(hp).str[i]<>'"') then
  485. begin
  486. if not(quoted) then
  487. begin
  488. if i>counter then
  489. AsmWrite(',');
  490. AsmWrite('"');
  491. end;
  492. AsmWrite(tai_string(hp).str[i]);
  493. quoted:=true;
  494. end { if > 31 and < 128 and ord('"') }
  495. else
  496. begin
  497. if quoted then
  498. AsmWrite('"');
  499. if i>counter then
  500. AsmWrite(',');
  501. quoted:=false;
  502. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  503. end;
  504. end; { end for i:=0 to... }
  505. if quoted then AsmWrite('"');
  506. AsmWrite(target_info.newline);
  507. inc(counter,line_length);
  508. end; { end for j:=0 ... }
  509. { do last line of lines }
  510. if counter<tai_string(hp).len then
  511. AsmWrite(#9#9'DB'#9);
  512. quoted:=false;
  513. for i:=counter to tai_string(hp).len-1 do
  514. begin
  515. { it is an ascii character. }
  516. if (ord(tai_string(hp).str[i])>31) and
  517. (ord(tai_string(hp).str[i])<128) and
  518. (tai_string(hp).str[i]<>'"') then
  519. begin
  520. if not(quoted) then
  521. begin
  522. if i>counter then
  523. AsmWrite(',');
  524. AsmWrite('"');
  525. end;
  526. AsmWrite(tai_string(hp).str[i]);
  527. quoted:=true;
  528. end { if > 31 and < 128 and " }
  529. else
  530. begin
  531. if quoted then
  532. AsmWrite('"');
  533. if i>counter then
  534. AsmWrite(',');
  535. quoted:=false;
  536. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  537. end;
  538. end; { end for i:=0 to... }
  539. if quoted then
  540. AsmWrite('"');
  541. end;
  542. AsmLn;
  543. end;
  544. ait_label :
  545. begin
  546. if tai_label(hp).l.is_used then
  547. AsmWriteLn(tai_label(hp).l.name+':');
  548. end;
  549. ait_direct :
  550. begin
  551. AsmWritePChar(tai_direct(hp).str);
  552. AsmLn;
  553. end;
  554. ait_symbol :
  555. begin
  556. if tai_symbol(hp).is_global then
  557. begin
  558. AsmWrite(#9'GLOBAL ');
  559. AsmWriteLn(tai_symbol(hp).sym.name);
  560. end;
  561. AsmWrite(tai_symbol(hp).sym.name);
  562. if assigned(hp.next) and not(tai(hp.next).typ in
  563. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  564. ait_const_symbol,ait_const_rva,
  565. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  566. AsmWriteLn(':')
  567. end;
  568. ait_symbol_end : ;
  569. ait_instruction :
  570. begin
  571. { Must be done with args in ATT order }
  572. taicpu(hp).SetOperandOrder(op_att);
  573. taicpu(hp).CheckNonCommutativeOpcodes;
  574. { We need intel order, no At&t }
  575. taicpu(hp).SetOperandOrder(op_intel);
  576. { Reset
  577. suffix:='';
  578. prefix:='';}
  579. s:='';
  580. if ((taicpu(hp).opcode=A_FADDP) or
  581. (taicpu(hp).opcode=A_FMULP))
  582. and (taicpu(hp).ops=0) then
  583. begin
  584. taicpu(hp).ops:=2;
  585. taicpu(hp).oper[0].typ:=top_reg;
  586. taicpu(hp).oper[0].reg:=R_ST1;
  587. taicpu(hp).oper[1].typ:=top_reg;
  588. taicpu(hp).oper[1].reg:=R_ST;
  589. end;
  590. if taicpu(hp).ops<>0 then
  591. begin
  592. if is_calljmp(taicpu(hp).opcode) then
  593. s:=#9+getopstr_jmp(taicpu(hp).oper[0],taicpu(hp).opcode)
  594. else
  595. begin
  596. { We need to explicitely set
  597. word prefix to get selectors
  598. to be pushed in 2 bytes PM }
  599. if (taicpu(hp).opsize=S_W) and
  600. ((taicpu(hp).opcode=A_PUSH) or
  601. (taicpu(hp).opcode=A_POP)) and
  602. (taicpu(hp).oper[0].typ=top_reg) and
  603. ((taicpu(hp).oper[0].reg>=firstsreg) and
  604. (taicpu(hp).oper[0].reg<=lastsreg)) then
  605. AsmWriteln(#9#9'DB'#9'066h');
  606. for i:=0 to taicpu(hp).ops-1 do
  607. begin
  608. if i=0 then
  609. sep:=#9
  610. else
  611. sep:=',';
  612. s:=s+sep+getopstr(taicpu(hp).oper[i],taicpu(hp).opsize,taicpu(hp).opcode,
  613. taicpu(hp).ops,(i=2));
  614. end;
  615. end;
  616. end;
  617. if taicpu(hp).opcode=A_FWAIT then
  618. AsmWriteln(#9#9'DB'#9'09bh')
  619. else
  620. AsmWriteLn(#9#9+{prefix+}int_op2str[taicpu(hp).opcode]+
  621. cond2str[taicpu(hp).condition]+{suffix+}s);
  622. end;
  623. {$ifdef GDB}
  624. ait_stabn,
  625. ait_stabs,
  626. ait_force_line,
  627. ait_stab_function_name : ;
  628. {$endif GDB}
  629. ait_cut :
  630. begin
  631. { only reset buffer if nothing has changed }
  632. if AsmSize=AsmStartSize then
  633. AsmClear
  634. else
  635. begin
  636. AsmClose;
  637. DoAssemble;
  638. AsmCreate(tai_cut(hp).place);
  639. end;
  640. { avoid empty files }
  641. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  642. begin
  643. if tai(hp.next).typ=ait_section then
  644. lastsec:=tai_section(hp.next).sec;
  645. hp:=tai(hp.next);
  646. end;
  647. if lastsec<>sec_none then
  648. AsmWriteLn('SECTION '+target_asm.secnames[lastsec]);
  649. AsmStartSize:=AsmSize;
  650. end;
  651. ait_marker :
  652. if tai_marker(hp).kind=InlineStart then
  653. inc(InlineLevel)
  654. else if tai_marker(hp).kind=InlineEnd then
  655. dec(InlineLevel);
  656. else
  657. internalerror(10000);
  658. end;
  659. hp:=tai(hp.next);
  660. end;
  661. end;
  662. var
  663. currentasmlist : TExternalAssembler;
  664. procedure writeexternal(p:tnamedindexitem);
  665. begin
  666. if tasmsymbol(p).defbind=AB_EXTERNAL then
  667. currentasmlist.AsmWriteln('EXTERN'#9+p.name);
  668. end;
  669. procedure T386NasmAssembler.WriteExternals;
  670. begin
  671. currentasmlist:=self;
  672. AsmSymbolList.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal);
  673. end;
  674. procedure T386NasmAssembler.WriteAsmList;
  675. begin
  676. {$ifdef EXTDEBUG}
  677. if assigned(current_module.mainsource) then
  678. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  679. {$endif}
  680. LastSec:=sec_none;
  681. AsmWriteLn('BITS 32');
  682. AsmLn;
  683. countlabelref:=false;
  684. lastfileinfo.line:=-1;
  685. lastfileinfo.fileindex:=0;
  686. lastinfile:=nil;
  687. WriteExternals;
  688. { Nasm doesn't support stabs
  689. WriteTree(debuglist);}
  690. WriteTree(codesegment);
  691. WriteTree(datasegment);
  692. WriteTree(consts);
  693. WriteTree(rttilist);
  694. WriteTree(resourcestringlist);
  695. WriteTree(bsssegment);
  696. Writetree(importssection);
  697. { exports are written by DLLTOOL
  698. if we use it so don't insert it twice (PM) }
  699. if not UseDeffileForExport and assigned(exportssection) then
  700. Writetree(exportssection);
  701. Writetree(resourcesection);
  702. countlabelref:=true;
  703. AsmLn;
  704. {$ifdef EXTDEBUG}
  705. if assigned(current_module.mainsource) then
  706. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  707. {$endif EXTDEBUG}
  708. end;
  709. {*****************************************************************************
  710. Initialize
  711. *****************************************************************************}
  712. const
  713. as_i386_nasmcoff_info : tasminfo =
  714. (
  715. id : as_i386_nasmcoff;
  716. idtxt : 'NASMCOFF';
  717. asmbin : 'nasm';
  718. asmcmd : '-f coff -o $OBJ $ASM';
  719. supported_target : target_i386_go32v2;
  720. outputbinary: false;
  721. allowdirect : true;
  722. externals : true;
  723. needar : true;
  724. labelprefix_only_inside_procedure: false;
  725. labelprefix : '..@';
  726. comment : '; ';
  727. secnames : ('',
  728. '.text','.data','.bss',
  729. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  730. '.stab','.stabstr')
  731. );
  732. as_i386_nasmwin32_info : tasminfo =
  733. (
  734. id : as_i386_nasmwin32;
  735. idtxt : 'NASMWIN32';
  736. asmbin : 'nasm';
  737. asmcmd : '-f win32 -o $OBJ $ASM';
  738. supported_target : target_i386_win32;
  739. outputbinary: false;
  740. allowdirect : true;
  741. externals : true;
  742. needar : true;
  743. labelprefix_only_inside_procedure: false;
  744. labelprefix : '..@';
  745. comment : '; ';
  746. secnames : ('',
  747. '.text','.data','.bss',
  748. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  749. '.stab','.stabstr')
  750. );
  751. as_i386_nasmobj_info : tasminfo =
  752. (
  753. id : as_i386_nasmobj;
  754. idtxt : 'NASMOBJ';
  755. asmbin : 'nasm';
  756. asmcmd : '-f obj -o $OBJ $ASM';
  757. supported_target : target_any; { what should I write here ?? }
  758. outputbinary: false;
  759. allowdirect : true;
  760. externals : true;
  761. needar : true;
  762. labelprefix_only_inside_procedure: false;
  763. labelprefix : '..@';
  764. comment : '; ';
  765. secnames : ('',
  766. '.text','.data','.bss',
  767. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  768. '.stab','.stabstr')
  769. );
  770. as_i386_nasmelf_info : tasminfo =
  771. (
  772. id : as_i386_nasmelf;
  773. idtxt : 'NASMELF';
  774. asmbin : 'nasm';
  775. asmcmd : '-f elf -o $OBJ $ASM';
  776. supported_target : target_i386_linux;
  777. outputbinary: false;
  778. allowdirect : true;
  779. externals : true;
  780. needar : true;
  781. labelprefix_only_inside_procedure: false;
  782. labelprefix : '..@';
  783. comment : '; ';
  784. secnames : ('',
  785. '.text','.data','.bss',
  786. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  787. '.stab','.stabstr')
  788. );
  789. initialization
  790. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  791. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  792. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  793. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  794. end.
  795. {
  796. $Log$
  797. Revision 1.11 2001-05-06 17:13:23 jonas
  798. * completed incomplete typed constant records
  799. Revision 1.10 2001/04/21 15:33:03 peter
  800. * stupid bug, finalization to initialization renaming
  801. Revision 1.9 2001/04/21 12:09:00 peter
  802. * fixed bug 1472 (merged)
  803. Revision 1.8 2001/04/18 22:02:00 peter
  804. * registration of targets and assemblers
  805. Revision 1.7 2001/04/13 01:22:17 peter
  806. * symtable change to classes
  807. * range check generation and errors fixed, make cycle DEBUG=1 works
  808. * memory leaks fixed
  809. Revision 1.6 2001/03/05 21:39:11 peter
  810. * changed to class with common TAssembler also for internal assembler
  811. Revision 1.5 2001/02/20 21:36:39 peter
  812. * tasm/masm fixes merged
  813. Revision 1.4 2001/01/13 20:24:24 peter
  814. * fixed operand order that got mixed up for external writers after
  815. my previous assembler block valid instruction check
  816. Revision 1.3 2000/12/25 00:07:31 peter
  817. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  818. tlinkedlist objects)
  819. Revision 1.2 2000/11/29 00:30:43 florian
  820. * unused units removed from uses clause
  821. * some changes for widestrings
  822. Revision 1.1 2000/10/15 09:47:42 peter
  823. * moved to i386/
  824. Revision 1.6 2000/09/24 15:06:11 peter
  825. * use defines.inc
  826. Revision 1.5 2000/08/27 16:11:49 peter
  827. * moved some util functions from globals,cobjects to cutils
  828. * splitted files into finput,fmodule
  829. Revision 1.4 2000/08/20 17:38:21 peter
  830. * smartlinking fixed for linux (merged)
  831. Revision 1.3 2000/07/13 12:08:24 michael
  832. + patched to 1.1.0 with former 1.09patch from peter
  833. Revision 1.2 2000/07/13 11:32:30 michael
  834. + removed logs
  835. }