ag386nsm.pas 39 KB

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