ag386nsm.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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_indirect_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,#9'FIXMEINDIRECT'#9
  395. );
  396. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  397. const
  398. secnames : array[TAsmSectiontype] of string[length('.objc_meth_var_types')] = ('',
  399. '.text',
  400. '.data',
  401. '.data',
  402. '.rodata',
  403. '.bss',
  404. '.tbss',
  405. '.pdata',
  406. '.text',
  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. );
  442. begin
  443. AsmLn;
  444. AsmWrite('SECTION ');
  445. AsmWrite(secnames[atype]);
  446. if create_smartlink_sections and
  447. (atype<>sec_bss) and
  448. (aname<>'') then
  449. begin
  450. AsmWrite('.');
  451. AsmWrite(aname);
  452. end;
  453. AsmLn;
  454. LasTSecType:=atype;
  455. end;
  456. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  457. {$ifdef cpuextended}
  458. type
  459. t80bitarray = array[0..9] of byte;
  460. {$endif cpuextended}
  461. var
  462. s : string;
  463. hp : tai;
  464. hp1 : tailineinfo;
  465. counter,
  466. lines,
  467. i,j,l : longint;
  468. InlineLevel : longint;
  469. consttype : taiconst_type;
  470. do_line,
  471. quoted : boolean;
  472. co : comp;
  473. sin : single;
  474. d : double;
  475. {$ifdef cpuextended}
  476. e : extended;
  477. {$endif cpuextended}
  478. begin
  479. if not assigned(p) then
  480. exit;
  481. InlineLevel:=0;
  482. { lineinfo is only needed for al_procedures (PFV) }
  483. do_line:=(cs_asm_source in current_settings.globalswitches) or
  484. ((cs_lineinfo in current_settings.moduleswitches)
  485. and (p=current_asmdata.asmlists[al_procedures]));
  486. hp:=tai(p.first);
  487. while assigned(hp) do
  488. begin
  489. if not(hp.typ in SkipLineInfo) then
  490. begin
  491. hp1:=hp as tailineinfo;
  492. current_filepos:=hp1.fileinfo;
  493. if do_line then
  494. begin
  495. { load infile }
  496. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  497. begin
  498. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  499. if assigned(infile) then
  500. begin
  501. { open only if needed !! }
  502. if (cs_asm_source in current_settings.globalswitches) then
  503. infile.open;
  504. end;
  505. { avoid unnecessary reopens of the same file !! }
  506. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  507. { be sure to change line !! }
  508. lastfileinfo.line:=-1;
  509. end;
  510. { write source }
  511. if (cs_asm_source in current_settings.globalswitches) and
  512. assigned(infile) then
  513. begin
  514. if (infile<>lastinfile) then
  515. begin
  516. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  517. if assigned(lastinfile) then
  518. lastinfile.close;
  519. end;
  520. if (hp1.fileinfo.line<>lastfileinfo.line) and
  521. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  522. begin
  523. if (hp1.fileinfo.line<>0) and
  524. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  525. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  526. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  527. { set it to a negative value !
  528. to make that is has been read already !! PM }
  529. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  530. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  531. end;
  532. end;
  533. lastfileinfo:=hp1.fileinfo;
  534. lastinfile:=infile;
  535. end;
  536. end;
  537. case hp.typ of
  538. ait_comment :
  539. Begin
  540. AsmWrite(target_asm.comment);
  541. AsmWritePChar(tai_comment(hp).str);
  542. AsmLn;
  543. End;
  544. ait_regalloc :
  545. begin
  546. if (cs_asm_regalloc in current_settings.globalswitches) then
  547. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  548. regallocstr[tai_regalloc(hp).ratype]);
  549. end;
  550. ait_tempalloc :
  551. begin
  552. if (cs_asm_tempalloc in current_settings.globalswitches) then
  553. begin
  554. {$ifdef EXTDEBUG}
  555. if assigned(tai_tempalloc(hp).problem) then
  556. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  557. tostr(tai_tempalloc(hp).tempsize)+')')
  558. else
  559. {$endif EXTDEBUG}
  560. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  561. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  562. end;
  563. end;
  564. ait_section :
  565. begin
  566. if tai_section(hp).sectype<>sec_none then
  567. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  568. LasTSecType:=tai_section(hp).sectype;
  569. end;
  570. ait_align :
  571. begin
  572. { nasm gives warnings when it finds align in bss as it
  573. wants to store data }
  574. if (lastsectype<>sec_bss) and
  575. (tai_align(hp).aligntype>1) then
  576. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  577. end;
  578. ait_datablock :
  579. begin
  580. if tai_datablock(hp).is_global then
  581. begin
  582. AsmWrite(#9'GLOBAL ');
  583. AsmWriteLn(tai_datablock(hp).sym.name);
  584. end;
  585. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  586. if SmartAsm then
  587. AddSymbol(tai_datablock(hp).sym.name,true);
  588. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  589. end;
  590. ait_const:
  591. begin
  592. consttype:=tai_const(hp).consttype;
  593. case consttype of
  594. aitconst_64bit :
  595. begin
  596. if assigned(tai_const(hp).sym) then
  597. internalerror(200404292);
  598. AsmWrite(ait_const2str[aitconst_32bit]);
  599. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  600. AsmWrite(',');
  601. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  602. AsmLn;
  603. end;
  604. aitconst_uleb128bit,
  605. aitconst_sleb128bit,
  606. aitconst_128bit:
  607. begin
  608. end;
  609. aitconst_32bit,
  610. aitconst_16bit,
  611. aitconst_8bit,
  612. aitconst_rva_symbol,
  613. aitconst_secrel32_symbol,
  614. aitconst_indirect_symbol :
  615. begin
  616. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  617. l:=0;
  618. repeat
  619. if assigned(tai_const(hp).sym) then
  620. begin
  621. if SmartAsm then
  622. begin
  623. AddSymbol(tai_const(hp).sym.name,false);
  624. if assigned(tai_const(hp).endsym) then
  625. AddSymbol(tai_const(hp).endsym.name,false);
  626. end;
  627. if assigned(tai_const(hp).endsym) then
  628. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  629. else
  630. s:=tai_const(hp).sym.name;
  631. if tai_const(hp).value<>0 then
  632. s:=s+tostr_with_plus(tai_const(hp).value);
  633. end
  634. else
  635. s:=tostr(tai_const(hp).value);
  636. AsmWrite(s);
  637. inc(l,length(s));
  638. if (l>line_length) or
  639. (hp.next=nil) or
  640. (tai(hp.next).typ<>ait_const) or
  641. (tai_const(hp.next).consttype<>consttype) then
  642. break;
  643. hp:=tai(hp.next);
  644. AsmWrite(',');
  645. until false;
  646. AsmLn;
  647. end;
  648. else
  649. internalerror(200704252);
  650. end;
  651. end;
  652. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  653. ait_real_80bit :
  654. begin
  655. if do_line then
  656. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  657. { Make sure e is a extended type, bestreal could be
  658. a different type (bestreal) !! (PFV) }
  659. e:=tai_real_80bit(hp).value;
  660. AsmWrite(#9#9'DB'#9);
  661. for i:=0 to 9 do
  662. begin
  663. if i<>0 then
  664. AsmWrite(',');
  665. AsmWrite(tostr(t80bitarray(e)[i]));
  666. end;
  667. AsmLn;
  668. end;
  669. {$else cpuextended}
  670. ait_real_80bit :
  671. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  672. {$endif cpuextended}
  673. // ait_real_64bit :
  674. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  675. ait_real_64bit :
  676. begin
  677. if do_line then
  678. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  679. d:=tai_real_64bit(hp).value;
  680. { swap the values to correct endian if required }
  681. if source_info.endian <> target_info.endian then
  682. swap64bitarray(t64bitarray(d));
  683. AsmWrite(#9#9'DB'#9);
  684. {$ifdef arm}
  685. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  686. begin
  687. for i:=4 to 7 do
  688. begin
  689. if i<>4 then
  690. AsmWrite(',');
  691. AsmWrite(tostr(t64bitarray(d)[i]));
  692. end;
  693. for i:=0 to 3 do
  694. begin
  695. AsmWrite(',');
  696. AsmWrite(tostr(t64bitarray(d)[i]));
  697. end;
  698. end
  699. else
  700. {$endif arm}
  701. begin
  702. for i:=0 to 7 do
  703. begin
  704. if i<>0 then
  705. AsmWrite(',');
  706. AsmWrite(tostr(t64bitarray(d)[i]));
  707. end;
  708. end;
  709. AsmLn;
  710. end;
  711. // ait_real_32bit :
  712. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  713. ait_real_32bit :
  714. begin
  715. if do_line then
  716. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  717. sin:=tai_real_32bit(hp).value;
  718. { swap the values to correct endian if required }
  719. if source_info.endian <> target_info.endian then
  720. swap32bitarray(t32bitarray(sin));
  721. AsmWrite(#9#9'DB'#9);
  722. for i:=0 to 3 do
  723. begin
  724. if i<>0 then
  725. AsmWrite(',');
  726. AsmWrite(tostr(t32bitarray(sin)[i]));
  727. end;
  728. AsmLn;
  729. end;
  730. // ait_comp_64bit :
  731. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  732. ait_comp_64bit :
  733. begin
  734. if do_line then
  735. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  736. AsmWrite(#9#9'DB'#9);
  737. co:=comp(tai_comp_64bit(hp).value);
  738. { swap the values to correct endian if required }
  739. if source_info.endian <> target_info.endian then
  740. swap64bitarray(t64bitarray(co));
  741. for i:=0 to 7 do
  742. begin
  743. if i<>0 then
  744. AsmWrite(',');
  745. AsmWrite(tostr(t64bitarray(co)[i]));
  746. end;
  747. AsmLn;
  748. end;
  749. ait_string :
  750. begin
  751. counter := 0;
  752. lines := tai_string(hp).len div line_length;
  753. { separate lines in different parts }
  754. if tai_string(hp).len > 0 then
  755. Begin
  756. for j := 0 to lines-1 do
  757. begin
  758. AsmWrite(#9#9'DB'#9);
  759. quoted:=false;
  760. for i:=counter to counter+line_length-1 do
  761. begin
  762. { it is an ascii character. }
  763. if (ord(tai_string(hp).str[i])>31) and
  764. (ord(tai_string(hp).str[i])<128) and
  765. (tai_string(hp).str[i]<>'"') then
  766. begin
  767. if not(quoted) then
  768. begin
  769. if i>counter then
  770. AsmWrite(',');
  771. AsmWrite('"');
  772. end;
  773. AsmWrite(tai_string(hp).str[i]);
  774. quoted:=true;
  775. end { if > 31 and < 128 and ord('"') }
  776. else
  777. begin
  778. if quoted then
  779. AsmWrite('"');
  780. if i>counter then
  781. AsmWrite(',');
  782. quoted:=false;
  783. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  784. end;
  785. end; { end for i:=0 to... }
  786. if quoted then AsmWrite('"');
  787. AsmWrite(target_info.newline);
  788. inc(counter,line_length);
  789. end; { end for j:=0 ... }
  790. { do last line of lines }
  791. if counter<tai_string(hp).len then
  792. AsmWrite(#9#9'DB'#9);
  793. quoted:=false;
  794. for i:=counter to tai_string(hp).len-1 do
  795. begin
  796. { it is an ascii character. }
  797. if (ord(tai_string(hp).str[i])>31) and
  798. (ord(tai_string(hp).str[i])<128) and
  799. (tai_string(hp).str[i]<>'"') then
  800. begin
  801. if not(quoted) then
  802. begin
  803. if i>counter then
  804. AsmWrite(',');
  805. AsmWrite('"');
  806. end;
  807. AsmWrite(tai_string(hp).str[i]);
  808. quoted:=true;
  809. end { if > 31 and < 128 and " }
  810. else
  811. begin
  812. if quoted then
  813. AsmWrite('"');
  814. if i>counter then
  815. AsmWrite(',');
  816. quoted:=false;
  817. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  818. end;
  819. end; { end for i:=0 to... }
  820. if quoted then
  821. AsmWrite('"');
  822. end;
  823. AsmLn;
  824. end;
  825. ait_label :
  826. begin
  827. if tai_label(hp).labsym.is_used then
  828. AsmWriteLn(tai_label(hp).labsym.name+':');
  829. if SmartAsm then
  830. AddSymbol(tai_label(hp).labsym.name,true);
  831. end;
  832. ait_symbol :
  833. begin
  834. if tai_symbol(hp).is_global then
  835. begin
  836. AsmWrite(#9'GLOBAL ');
  837. AsmWriteLn(tai_symbol(hp).sym.name);
  838. end;
  839. AsmWrite(tai_symbol(hp).sym.name);
  840. if SmartAsm then
  841. AddSymbol(tai_symbol(hp).sym.name,true);
  842. if assigned(hp.next) and not(tai(hp.next).typ in
  843. [ait_const,
  844. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  845. AsmWriteLn(':')
  846. end;
  847. ait_symbol_end : ;
  848. ait_instruction :
  849. begin
  850. taicpu(hp).CheckNonCommutativeOpcodes;
  851. { We need intel order, no At&t }
  852. taicpu(hp).SetOperandOrder(op_intel);
  853. s:='';
  854. if ((taicpu(hp).opcode=A_FADDP) or
  855. (taicpu(hp).opcode=A_FMULP))
  856. and (taicpu(hp).ops=0) then
  857. begin
  858. taicpu(hp).allocate_oper(2);
  859. taicpu(hp).oper[0]^.typ:=top_reg;
  860. taicpu(hp).oper[0]^.reg:=NR_ST1;
  861. taicpu(hp).oper[1]^.typ:=top_reg;
  862. taicpu(hp).oper[1]^.reg:=NR_ST;
  863. end;
  864. if taicpu(hp).opcode=A_FWAIT then
  865. AsmWriteln(#9#9'DB'#9'09bh')
  866. else
  867. begin
  868. { We need to explicitely set
  869. word prefix to get selectors
  870. to be pushed in 2 bytes PM }
  871. if (taicpu(hp).opsize=S_W) and
  872. ((taicpu(hp).opcode=A_PUSH) or
  873. (taicpu(hp).opcode=A_POP)) and
  874. (taicpu(hp).oper[0]^.typ=top_reg) and
  875. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  876. AsmWriteln(#9#9'DB'#9'066h');
  877. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  878. if taicpu(hp).ops<>0 then
  879. begin
  880. if is_calljmp(taicpu(hp).opcode) then
  881. begin
  882. AsmWrite(#9);
  883. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  884. end
  885. else
  886. begin
  887. for i:=0 to taicpu(hp).ops-1 do
  888. begin
  889. if i=0 then
  890. AsmWrite(#9)
  891. else
  892. AsmWrite(',');
  893. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  894. end;
  895. end;
  896. end;
  897. AsmLn;
  898. end;
  899. end;
  900. ait_stab,
  901. ait_force_line,
  902. ait_function_name : ;
  903. ait_cutobject :
  904. begin
  905. if SmartAsm then
  906. begin
  907. { only reset buffer if nothing has changed }
  908. if AsmSize=AsmStartSize then
  909. AsmClear
  910. else
  911. begin
  912. if SmartAsm then
  913. begin
  914. WriteSmartExternals;
  915. FreeExternChainList;
  916. end;
  917. AsmClose;
  918. DoAssemble;
  919. AsmCreate(tai_cutobject(hp).place);
  920. end;
  921. { avoid empty files }
  922. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  923. begin
  924. if tai(hp.next).typ=ait_section then
  925. lasTSectype:=tai_section(hp.next).sectype;
  926. hp:=tai(hp.next);
  927. end;
  928. if lasTSectype<>sec_none then
  929. WriteSection(lasTSectype,'');
  930. AsmStartSize:=AsmSize;
  931. end;
  932. end;
  933. ait_marker :
  934. if tai_marker(hp).kind=mark_InlineStart then
  935. inc(InlineLevel)
  936. else if tai_marker(hp).kind=mark_InlineEnd then
  937. dec(InlineLevel);
  938. ait_directive :
  939. begin
  940. case tai_directive(hp).directive of
  941. asd_nasm_import :
  942. AsmWrite('import ');
  943. asd_extern :
  944. AsmWrite('EXTERN ');
  945. else
  946. internalerror(200509191);
  947. end;
  948. if assigned(tai_directive(hp).name) then
  949. begin
  950. if SmartAsm then
  951. AddSymbol(tai_directive(hp).name^,false);
  952. AsmWrite(tai_directive(hp).name^);
  953. end;
  954. AsmLn;
  955. end;
  956. else
  957. internalerror(10000);
  958. end;
  959. hp:=tai(hp.next);
  960. end;
  961. end;
  962. procedure T386NasmAssembler.WriteExternals;
  963. var
  964. sym : TAsmSymbol;
  965. i : longint;
  966. begin
  967. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  968. begin
  969. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  970. if sym.bind=AB_EXTERNAL then
  971. AsmWriteln('EXTERN'#9+sym.name);
  972. end;
  973. end;
  974. procedure T386NasmAssembler.WriteSmartExternals;
  975. var
  976. EC : PExternChain;
  977. begin
  978. EC:=FEC;
  979. while assigned(EC) do
  980. begin
  981. if not EC^.is_defined then
  982. AsmWriteln('EXTERN'#9+EC^.psym^);
  983. EC:=EC^.next;
  984. end;
  985. end;
  986. procedure T386NasmAssembler.WriteAsmList;
  987. var
  988. hal : tasmlisttype;
  989. begin
  990. {$ifdef EXTDEBUG}
  991. if assigned(current_module.mainsource) then
  992. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  993. {$endif}
  994. AsmWriteLn('BITS 32');
  995. AsmLn;
  996. WriteExternals;
  997. for hal:=low(TasmlistType) to high(TasmlistType) do
  998. begin
  999. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1000. writetree(current_asmdata.asmlists[hal]);
  1001. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1002. end;
  1003. AsmLn;
  1004. if SmartAsm then
  1005. begin
  1006. WriteSmartExternals;
  1007. FreeExternChainList;
  1008. end;
  1009. {$ifdef EXTDEBUG}
  1010. if assigned(current_module.mainsource) then
  1011. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  1012. {$endif EXTDEBUG}
  1013. end;
  1014. {*****************************************************************************
  1015. Initialize
  1016. *****************************************************************************}
  1017. const
  1018. as_i386_nasmcoff_info : tasminfo =
  1019. (
  1020. id : as_i386_nasmcoff;
  1021. idtxt : 'NASMCOFF';
  1022. asmbin : 'nasm';
  1023. asmcmd : '-f coff -o $OBJ $ASM';
  1024. supported_targets : [system_i386_go32v2];
  1025. flags : [af_allowdirect,af_needar,af_no_debug];
  1026. labelprefix : '..@';
  1027. comment : '; ';
  1028. );
  1029. as_i386_nasmwin32_info : tasminfo =
  1030. (
  1031. id : as_i386_nasmwin32;
  1032. idtxt : 'NASMWIN32';
  1033. asmbin : 'nasm';
  1034. asmcmd : '-f win32 -o $OBJ $ASM';
  1035. supported_targets : [system_i386_win32];
  1036. flags : [af_allowdirect,af_needar,af_no_debug];
  1037. labelprefix : '..@';
  1038. comment : '; ';
  1039. );
  1040. as_i386_nasmobj_info : tasminfo =
  1041. (
  1042. id : as_i386_nasmobj;
  1043. idtxt : 'NASMOBJ';
  1044. asmbin : 'nasm';
  1045. asmcmd : '-f obj -o $OBJ $ASM';
  1046. supported_targets : [system_i386_embedded];
  1047. flags : [af_allowdirect,af_needar,af_no_debug];
  1048. labelprefix : '..@';
  1049. comment : '; ';
  1050. );
  1051. as_i386_nasmwdosx_info : tasminfo =
  1052. (
  1053. id : as_i386_nasmwdosx;
  1054. idtxt : 'NASMWDOSX';
  1055. asmbin : 'nasm';
  1056. asmcmd : '-f win32 -o $OBJ $ASM';
  1057. supported_targets : [system_i386_wdosx];
  1058. flags : [af_allowdirect,af_needar,af_no_debug];
  1059. labelprefix : '..@';
  1060. comment : '; ';
  1061. );
  1062. as_i386_nasmelf_info : tasminfo =
  1063. (
  1064. id : as_i386_nasmelf;
  1065. idtxt : 'NASMELF';
  1066. asmbin : 'nasm';
  1067. asmcmd : '-f elf -o $OBJ $ASM';
  1068. supported_targets : [system_i386_linux];
  1069. flags : [af_allowdirect,af_needar,af_no_debug];
  1070. labelprefix : '..@';
  1071. comment : '; ';
  1072. );
  1073. as_i386_nasmbeos_info : tasminfo =
  1074. (
  1075. id : as_i386_nasmbeos;
  1076. idtxt : 'NASMELF';
  1077. asmbin : 'nasm';
  1078. asmcmd : '-f elf -o $OBJ $ASM';
  1079. supported_targets : [system_i386_beos];
  1080. flags : [af_allowdirect,af_needar,af_no_debug];
  1081. labelprefix : '..@';
  1082. comment : '; ';
  1083. );
  1084. as_i386_nasmhaiku_info : tasminfo =
  1085. (
  1086. id : as_i386_nasmhaiku;
  1087. idtxt : 'NASMELF';
  1088. asmbin : 'nasm';
  1089. asmcmd : '-f elf -o $OBJ $ASM';
  1090. supported_targets : [system_i386_haiku];
  1091. flags : [af_allowdirect,af_needar,af_no_debug];
  1092. labelprefix : '..@';
  1093. comment : '; ';
  1094. );
  1095. initialization
  1096. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1097. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1098. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1099. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1100. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1101. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1102. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1103. end.