cg386ld.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for load/assignment nodes
  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 cg386ld;
  19. interface
  20. uses
  21. tree;
  22. procedure secondload(var p : ptree);
  23. procedure secondassignment(var p : ptree);
  24. procedure secondfuncret(var p : ptree);
  25. procedure secondarrayconstruct(var p : ptree);
  26. implementation
  27. uses
  28. globtype,systems,
  29. cobjects,verbose,globals,
  30. symtable,aasm,types,
  31. hcodegen,temp_gen,pass_2,
  32. i386base,i386asm,
  33. cgai386,tgeni386,cg386cnv;
  34. {*****************************************************************************
  35. SecondLoad
  36. *****************************************************************************}
  37. procedure secondload(var p : ptree);
  38. var
  39. hregister : tregister;
  40. symtabletype : tsymtabletype;
  41. i : longint;
  42. hp : preference;
  43. s : pasmsymbol;
  44. popeax : boolean;
  45. begin
  46. simple_loadn:=true;
  47. reset_reference(p^.location.reference);
  48. case p^.symtableentry^.typ of
  49. { this is only for toasm and toaddr }
  50. absolutesym :
  51. begin
  52. p^.location.reference.symbol:=nil;
  53. if (pabsolutesym(p^.symtableentry)^.abstyp=toaddr) then
  54. begin
  55. if pabsolutesym(p^.symtableentry)^.absseg then
  56. p^.location.reference.segment:=R_FS;
  57. p^.location.reference.offset:=pabsolutesym(p^.symtableentry)^.address;
  58. end
  59. else
  60. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  61. end;
  62. varsym :
  63. begin
  64. hregister:=R_NO;
  65. { C variable }
  66. if (pvarsym(p^.symtableentry)^.var_options and vo_is_C_var)<>0 then
  67. begin
  68. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  69. end
  70. { DLL variable }
  71. else if (pvarsym(p^.symtableentry)^.var_options and vo_is_dll_var)<>0 then
  72. begin
  73. hregister:=getregister32;
  74. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  75. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.location.reference),hregister)));
  76. p^.location.reference.symbol:=nil;
  77. p^.location.reference.base:=hregister;
  78. end
  79. { external variable }
  80. else if (pvarsym(p^.symtableentry)^.var_options and vo_is_external)<>0 then
  81. begin
  82. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  83. end
  84. { thread variable }
  85. else if (pvarsym(p^.symtableentry)^.var_options and vo_is_thread_var)<>0 then
  86. begin
  87. popeax:=not(R_EAX in unused);
  88. if popeax then
  89. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  90. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  91. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(p^.location.reference))));
  92. { the called procedure isn't allowed to change }
  93. { any register except EAX }
  94. emitcall('FPC_RELOCATE_THREADVAR');
  95. reset_reference(p^.location.reference);
  96. p^.location.reference.base:=getregister32;
  97. emit_reg_reg(A_MOV,S_L,R_EAX,p^.location.reference.base);
  98. if popeax then
  99. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  100. end
  101. { normal variable }
  102. else
  103. begin
  104. symtabletype:=p^.symtable^.symtabletype;
  105. { in case it is a register variable: }
  106. if pvarsym(p^.symtableentry)^.reg<>R_NO then
  107. begin
  108. p^.location.loc:=LOC_CREGISTER;
  109. p^.location.register:=pvarsym(p^.symtableentry)^.reg;
  110. unused:=unused-[pvarsym(p^.symtableentry)^.reg];
  111. end
  112. else
  113. begin
  114. { first handle local and temporary variables }
  115. if (symtabletype in [parasymtable,inlinelocalsymtable,
  116. inlineparasymtable,localsymtable]) then
  117. begin
  118. p^.location.reference.base:=procinfo.framepointer;
  119. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address+p^.symtable^.address_fixup;
  120. if (symtabletype in [localsymtable,inlinelocalsymtable]) then
  121. p^.location.reference.offset:=-p^.location.reference.offset;
  122. if (lexlevel>(p^.symtable^.symtablelevel)) then
  123. begin
  124. hregister:=getregister32;
  125. { make a reference }
  126. hp:=new_reference(procinfo.framepointer,
  127. procinfo.framepointer_offset);
  128. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hregister)));
  129. simple_loadn:=false;
  130. i:=lexlevel-1;
  131. while i>(p^.symtable^.symtablelevel) do
  132. begin
  133. { make a reference }
  134. hp:=new_reference(hregister,8);
  135. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hregister)));
  136. dec(i);
  137. end;
  138. p^.location.reference.base:=hregister;
  139. end;
  140. end
  141. else
  142. case symtabletype of
  143. unitsymtable,globalsymtable,
  144. staticsymtable :
  145. begin
  146. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  147. end;
  148. stt_exceptsymtable:
  149. begin
  150. p^.location.reference.base:=procinfo.framepointer;
  151. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address;
  152. end;
  153. objectsymtable:
  154. begin
  155. if (pvarsym(p^.symtableentry)^.properties and sp_static)<>0 then
  156. begin
  157. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  158. end
  159. else
  160. begin
  161. p^.location.reference.base:=R_ESI;
  162. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address;
  163. end;
  164. end;
  165. withsymtable:
  166. begin
  167. { make a reference }
  168. { symtable datasize field
  169. contains the offset of the temp
  170. stored }
  171. { hp:=new_reference(procinfo.framepointer,
  172. p^.symtable^.datasize);
  173. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hregister)));}
  174. if ptree(pwithsymtable(p^.symtable)^.withnode)^.islocal then
  175. begin
  176. p^.location.reference:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  177. end
  178. else
  179. begin
  180. hregister:=getregister32;
  181. p^.location.reference.base:=hregister;
  182. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  183. newreference(ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^),
  184. hregister)));
  185. end;
  186. inc(p^.location.reference.offset,pvarsym(p^.symtableentry)^.address);
  187. end;
  188. end;
  189. end;
  190. { in case call by reference, then calculate. Open array
  191. is always an reference! }
  192. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  193. is_open_array(pvarsym(p^.symtableentry)^.definition) or
  194. is_array_of_const(pvarsym(p^.symtableentry)^.definition) or
  195. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  196. push_addr_param(pvarsym(p^.symtableentry)^.definition)) then
  197. begin
  198. simple_loadn:=false;
  199. if hregister=R_NO then
  200. hregister:=getregister32;
  201. {$ifdef OLDHIGH}
  202. if is_open_array(pvarsym(p^.symtableentry)^.definition) or
  203. is_open_string(pvarsym(p^.symtableentry)^.definition) then
  204. begin
  205. if (p^.location.reference.base=procinfo.framepointer) then
  206. begin
  207. highframepointer:=p^.location.reference.base;
  208. highoffset:=p^.location.reference.offset;
  209. end
  210. else
  211. begin
  212. highframepointer:=R_EDI;
  213. highoffset:=p^.location.reference.offset;
  214. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  215. p^.location.reference.base,R_EDI)));
  216. end;
  217. end;
  218. {$endif}
  219. if p^.location.loc=LOC_CREGISTER then
  220. begin
  221. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,
  222. p^.location.register,hregister)));
  223. p^.location.loc:=LOC_REFERENCE;
  224. end
  225. else
  226. begin
  227. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  228. newreference(p^.location.reference),
  229. hregister)));
  230. end;
  231. reset_reference(p^.location.reference);
  232. p^.location.reference.base:=hregister;
  233. end;
  234. end;
  235. end;
  236. procsym:
  237. begin
  238. if assigned(p^.left) then
  239. begin
  240. secondpass(p^.left);
  241. p^.location.loc:=LOC_MEM;
  242. gettempofsizereference(8,p^.location.reference);
  243. { load class instance address }
  244. case p^.left^.location.loc of
  245. LOC_CREGISTER,
  246. LOC_REGISTER:
  247. begin
  248. hregister:=p^.left^.location.register;
  249. ungetregister32(p^.left^.location.register);
  250. { such code is allowed !
  251. CGMessage(cg_e_illegal_expression); }
  252. end;
  253. LOC_MEM,
  254. LOC_REFERENCE:
  255. begin
  256. hregister:=R_EDI;
  257. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  258. newreference(p^.left^.location.reference),R_EDI)));
  259. del_reference(p^.left^.location.reference);
  260. ungetiftemp(p^.left^.location.reference);
  261. end;
  262. else internalerror(26019);
  263. end;
  264. { store the class instance address }
  265. new(hp);
  266. hp^:=p^.location.reference;
  267. inc(hp^.offset,4);
  268. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  269. hregister,hp)));
  270. { virtual method ? }
  271. if (pprocsym(p^.symtableentry)^.definition^.options and povirtualmethod)<>0 then
  272. begin
  273. new(hp);
  274. reset_reference(hp^);
  275. hp^.base:=hregister;
  276. { load vmt pointer }
  277. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  278. hp,R_EDI)));
  279. {$IfDef regallocfix}
  280. del_reference(hp^);
  281. {$EndIf regallocfix}
  282. { load method address }
  283. new(hp);
  284. reset_reference(hp^);
  285. hp^.base:=R_EDI;
  286. hp^.offset:=pprocsym(p^.symtableentry)^.definition^.extnumber*4+12;
  287. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  288. hp,R_EDI)));
  289. { ... and store it }
  290. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  291. R_EDI,newreference(p^.location.reference))));
  292. end
  293. else
  294. begin
  295. s:=newasmsymbol(pprocsym(p^.symtableentry)^.definition^.mangledname);
  296. exprasmlist^.concat(new(pai386,op_sym_ofs_ref(A_MOV,S_L,s,0,
  297. newreference(p^.location.reference))));
  298. end;
  299. end
  300. else
  301. begin
  302. {!!!!! Be aware, work on virtual methods too }
  303. p^.location.reference.symbol:=newasmsymbol(pprocsym(p^.symtableentry)^.definition^.mangledname);
  304. end;
  305. end;
  306. typedconstsym :
  307. begin
  308. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  309. end;
  310. else internalerror(4);
  311. end;
  312. end;
  313. {*****************************************************************************
  314. SecondAssignment
  315. *****************************************************************************}
  316. procedure secondassignment(var p : ptree);
  317. var
  318. opsize : topsize;
  319. otlabel,hlabel,oflabel : pasmlabel;
  320. hregister : tregister;
  321. loc : tloc;
  322. r : preference;
  323. ai : pai386;
  324. begin
  325. otlabel:=truelabel;
  326. oflabel:=falselabel;
  327. getlabel(truelabel);
  328. getlabel(falselabel);
  329. { calculate left sides }
  330. if not(p^.concat_string) then
  331. secondpass(p^.left);
  332. if codegenerror then
  333. exit;
  334. case p^.left^.location.loc of
  335. LOC_REFERENCE : begin
  336. { in case left operator uses to register }
  337. { but to few are free then LEA }
  338. if (p^.left^.location.reference.base<>R_NO) and
  339. (p^.left^.location.reference.index<>R_NO) and
  340. (usablereg32<p^.right^.registers32) then
  341. begin
  342. del_reference(p^.left^.location.reference);
  343. hregister:=getregister32;
  344. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(
  345. p^.left^.location.reference),
  346. hregister)));
  347. reset_reference(p^.left^.location.reference);
  348. p^.left^.location.reference.base:=hregister;
  349. p^.left^.location.reference.index:=R_NO;
  350. end;
  351. loc:=LOC_REFERENCE;
  352. end;
  353. LOC_CREGISTER:
  354. loc:=LOC_CREGISTER;
  355. LOC_MMXREGISTER:
  356. loc:=LOC_MMXREGISTER;
  357. LOC_CMMXREGISTER:
  358. loc:=LOC_CMMXREGISTER;
  359. else
  360. begin
  361. CGMessage(cg_e_illegal_expression);
  362. exit;
  363. end;
  364. end;
  365. { lets try to optimize this (PM) }
  366. { define a dest_loc that is the location }
  367. { and a ptree to verify that it is the right }
  368. { place to insert it }
  369. {$ifdef test_dest_loc}
  370. if (aktexprlevel<4) then
  371. begin
  372. dest_loc_known:=true;
  373. dest_loc:=p^.left^.location;
  374. dest_loc_tree:=p^.right;
  375. end;
  376. {$endif test_dest_loc}
  377. secondpass(p^.right);
  378. if codegenerror then
  379. exit;
  380. {$ifdef test_dest_loc}
  381. dest_loc_known:=false;
  382. if in_dest_loc then
  383. begin
  384. truelabel:=otlabel;
  385. falselabel:=oflabel;
  386. in_dest_loc:=false;
  387. exit;
  388. end;
  389. {$endif test_dest_loc}
  390. if p^.left^.resulttype^.deftype=stringdef then
  391. begin
  392. if is_ansistring(p^.left^.resulttype) then
  393. begin
  394. { the source and destinations are released
  395. in loadansistring, because an ansi string can
  396. also be in a register
  397. }
  398. loadansistring(p);
  399. end
  400. else
  401. if is_shortstring(p^.left^.resulttype) and
  402. not (p^.concat_string) then
  403. begin
  404. if is_ansistring(p^.right^.resulttype) then
  405. begin
  406. if (p^.right^.treetype=stringconstn) and
  407. (p^.right^.length=0) then
  408. begin
  409. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_B,
  410. 0,newreference(p^.left^.location.reference))));
  411. {$IfDef regallocfix}
  412. del_reference(p^.left^.location.reference);
  413. {$EndIf regallocfix}
  414. end
  415. else
  416. loadansi2short(p^.right,p^.left);
  417. end
  418. else
  419. begin
  420. { we do not need destination anymore }
  421. del_reference(p^.left^.location.reference);
  422. del_reference(p^.right^.location.reference);
  423. loadshortstring(p);
  424. ungetiftemp(p^.right^.location.reference);
  425. end;
  426. end
  427. else
  428. begin
  429. { its the only thing we have to do }
  430. del_reference(p^.right^.location.reference);
  431. end
  432. end
  433. else case p^.right^.location.loc of
  434. LOC_REFERENCE,
  435. LOC_MEM : begin
  436. { extra handling for ordinal constants }
  437. if (p^.right^.treetype in [ordconstn,fixconstn]) or
  438. (loc=LOC_CREGISTER) then
  439. begin
  440. case p^.left^.resulttype^.size of
  441. 1 : opsize:=S_B;
  442. 2 : opsize:=S_W;
  443. 4 : opsize:=S_L;
  444. { S_L is correct, the copy is done }
  445. { with two moves }
  446. 8 : opsize:=S_L;
  447. end;
  448. if loc=LOC_CREGISTER then
  449. begin
  450. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  451. newreference(p^.right^.location.reference),
  452. p^.left^.location.register)));
  453. if is_64bitint(p^.right^.resulttype) then
  454. begin
  455. r:=newreference(p^.right^.location.reference);
  456. inc(r^.offset,4);
  457. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,r,
  458. p^.left^.location.registerhigh)));
  459. end;
  460. {$IfDef regallocfix}
  461. del_reference(p^.right^.location.reference);
  462. {$EndIf regallocfix}
  463. end
  464. else
  465. begin
  466. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,
  467. p^.right^.location.reference.offset,
  468. newreference(p^.left^.location.reference))));
  469. if is_64bitint(p^.right^.resulttype) then
  470. begin
  471. r:=newreference(p^.left^.location.reference);
  472. inc(r^.offset,4);
  473. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,opsize,
  474. 0,r)));
  475. end;
  476. {$IfDef regallocfix}
  477. del_reference(p^.left^.location.reference);
  478. {$EndIf regallocfix}
  479. {exprasmlist^.concat(new(pai386,op_const_loc(A_MOV,opsize,
  480. p^.right^.location.reference.offset,
  481. p^.left^.location)));}
  482. end;
  483. end
  484. else
  485. begin
  486. if (p^.right^.resulttype^.needs_inittable) and
  487. ( (p^.right^.resulttype^.deftype<>objectdef) or
  488. not(pobjectdef(p^.right^.resulttype)^.isclass)) then
  489. begin
  490. { this would be a problem }
  491. if not(p^.left^.resulttype^.needs_inittable) then
  492. internalerror(3457);
  493. { increment source reference counter }
  494. new(r);
  495. reset_reference(r^);
  496. r^.symbol:=p^.right^.resulttype^.get_inittable_label;
  497. emitpushreferenceaddr(r^);
  498. emitpushreferenceaddr(p^.right^.location.reference);
  499. exprasmlist^.concat(new(pai386,
  500. op_sym(A_CALL,S_NO,newasmsymbol('FPC_ADDREF'))));
  501. { decrement destination reference counter }
  502. new(r);
  503. reset_reference(r^);
  504. r^.symbol:=p^.left^.resulttype^.get_inittable_label;
  505. emitpushreferenceaddr(r^);
  506. emitpushreferenceaddr(p^.left^.location.reference);
  507. exprasmlist^.concat(new(pai386,
  508. op_sym(A_CALL,S_NO,newasmsymbol('FPC_DECREF'))));
  509. end;
  510. {$ifdef regallocfix}
  511. concatcopy(p^.right^.location.reference,
  512. p^.left^.location.reference,p^.left^.resulttype^.size,true,false);
  513. ungetiftemp(p^.right^.location.reference);
  514. {$Else regallocfix}
  515. concatcopy(p^.right^.location.reference,
  516. p^.left^.location.reference,p^.left^.resulttype^.size,false,false);
  517. ungetiftemp(p^.right^.location.reference);
  518. {$endif regallocfix}
  519. end;
  520. end;
  521. {$ifdef SUPPORT_MMX}
  522. LOC_CMMXREGISTER,
  523. LOC_MMXREGISTER:
  524. begin
  525. if loc=LOC_CMMXREGISTER then
  526. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVQ,S_NO,
  527. p^.right^.location.register,p^.left^.location.register)))
  528. else
  529. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
  530. p^.right^.location.register,newreference(p^.left^.location.reference))));
  531. end;
  532. {$endif SUPPORT_MMX}
  533. LOC_REGISTER,
  534. LOC_CREGISTER : begin
  535. case p^.right^.resulttype^.size of
  536. 1 : opsize:=S_B;
  537. 2 : opsize:=S_W;
  538. 4 : opsize:=S_L;
  539. 8 : opsize:=S_L;
  540. end;
  541. { simplified with op_reg_loc }
  542. if loc=LOC_CREGISTER then
  543. begin
  544. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,opsize,
  545. p^.right^.location.register,
  546. p^.left^.location.register)));
  547. {$IfDef regallocfix}
  548. ungetregister(p^.right^.location.register);
  549. {$EndIf regallocfix}
  550. end
  551. else
  552. Begin
  553. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,
  554. p^.right^.location.register,
  555. newreference(p^.left^.location.reference))));
  556. {$IfDef regallocfix}
  557. ungetregister(p^.right^.location.register);
  558. del_reference(p^.left^.location.reference);
  559. {$EndIf regallocfix}
  560. end;
  561. if is_64bitint(p^.right^.resulttype) then
  562. begin
  563. { simplified with op_reg_loc }
  564. if loc=LOC_CREGISTER then
  565. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,opsize,
  566. p^.right^.location.registerhigh,
  567. p^.left^.location.registerhigh)))
  568. else
  569. begin
  570. r:=newreference(p^.left^.location.reference);
  571. inc(r^.offset,4);
  572. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,opsize,
  573. p^.right^.location.registerhigh,r)));
  574. end;
  575. end;
  576. {exprasmlist^.concat(new(pai386,op_reg_loc(A_MOV,opsize,
  577. p^.right^.location.register,
  578. p^.left^.location))); }
  579. end;
  580. LOC_FPU : begin
  581. if loc<>LOC_REFERENCE then
  582. internalerror(10010)
  583. else
  584. floatstore(pfloatdef(p^.left^.resulttype)^.typ,
  585. p^.left^.location.reference);
  586. end;
  587. LOC_JUMP : begin
  588. getlabel(hlabel);
  589. emitlab(truelabel);
  590. if loc=LOC_CREGISTER then
  591. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_B,
  592. 1,p^.left^.location.register)))
  593. else
  594. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_B,
  595. 1,newreference(p^.left^.location.reference))));
  596. {exprasmlist^.concat(new(pai386,op_const_loc(A_MOV,S_B,
  597. 1,p^.left^.location)));}
  598. emitjmp(C_None,hlabel);
  599. emitlab(falselabel);
  600. if loc=LOC_CREGISTER then
  601. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_B,
  602. p^.left^.location.register,
  603. p^.left^.location.register)))
  604. else
  605. begin
  606. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_B,
  607. 0,newreference(p^.left^.location.reference))));
  608. {$IfDef regallocfix}
  609. del_reference(p^.left^.location.reference);
  610. {$EndIf regallocfix}
  611. end;
  612. emitlab(hlabel);
  613. end;
  614. LOC_FLAGS : begin
  615. if loc=LOC_CREGISTER then
  616. emit_flag2reg(p^.right^.location.resflags,p^.left^.location.register)
  617. else
  618. begin
  619. ai:=new(pai386,op_ref(A_Setcc,S_B,newreference(p^.left^.location.reference)));
  620. ai^.SetCondition(flag_2_cond[p^.right^.location.resflags]);
  621. exprasmlist^.concat(ai);
  622. end;
  623. {$IfDef regallocfix}
  624. del_reference(p^.left^.location.reference);
  625. {$EndIf regallocfix}
  626. end;
  627. end;
  628. freelabel(truelabel);
  629. freelabel(falselabel);
  630. truelabel:=otlabel;
  631. falselabel:=oflabel;
  632. end;
  633. {*****************************************************************************
  634. SecondFuncRet
  635. *****************************************************************************}
  636. procedure secondfuncret(var p : ptree);
  637. var
  638. hr : tregister;
  639. hp : preference;
  640. pp : pprocinfo;
  641. hr_valid : boolean;
  642. begin
  643. reset_reference(p^.location.reference);
  644. hr_valid:=false;
  645. if @procinfo<>pprocinfo(p^.funcretprocinfo) then
  646. begin
  647. hr:=getregister32;
  648. hr_valid:=true;
  649. hp:=new_reference(procinfo.framepointer,
  650. procinfo.framepointer_offset);
  651. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hr)));
  652. pp:=procinfo.parent;
  653. { walk up the stack frame }
  654. while pp<>pprocinfo(p^.funcretprocinfo) do
  655. begin
  656. hp:=new_reference(hr,
  657. pp^.framepointer_offset);
  658. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hp,hr)));
  659. pp:=pp^.parent;
  660. end;
  661. p^.location.reference.base:=hr;
  662. end
  663. else
  664. p^.location.reference.base:=procinfo.framepointer;
  665. p^.location.reference.offset:=procinfo.retoffset;
  666. if ret_in_param(p^.retdef) then
  667. begin
  668. if not hr_valid then
  669. hr:=getregister32;
  670. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.location.reference),hr)));
  671. p^.location.reference.base:=hr;
  672. p^.location.reference.offset:=0;
  673. end;
  674. end;
  675. {*****************************************************************************
  676. SecondArrayConstruct
  677. *****************************************************************************}
  678. const
  679. vtInteger = 0;
  680. vtBoolean = 1;
  681. vtChar = 2;
  682. vtExtended = 3;
  683. vtString = 4;
  684. vtPointer = 5;
  685. vtPChar = 6;
  686. vtObject = 7;
  687. vtClass = 8;
  688. vtWideChar = 9;
  689. vtPWideChar = 10;
  690. vtAnsiString = 11;
  691. vtCurrency = 12;
  692. vtVariant = 13;
  693. vtInterface = 14;
  694. vtWideString = 15;
  695. vtInt64 = 16;
  696. procedure secondarrayconstruct(var p : ptree);
  697. var
  698. hp : ptree;
  699. href : treference;
  700. lt : pdef;
  701. vaddr : boolean;
  702. vtype : longint;
  703. begin
  704. if not p^.cargs then
  705. begin
  706. reset_reference(p^.location.reference);
  707. { Allocate always a temp, also if no elements are required, to
  708. ensure that location is valid (PFV) }
  709. if parraydef(p^.resulttype)^.highrange=-1 then
  710. gettempofsizereference(8,p^.location.reference)
  711. else
  712. gettempofsizereference((parraydef(p^.resulttype)^.highrange+1)*8,p^.location.reference);
  713. href:=p^.location.reference;
  714. end;
  715. hp:=p;
  716. while assigned(hp) do
  717. begin
  718. if assigned(hp^.left) then
  719. begin
  720. secondpass(hp^.left);
  721. if codegenerror then
  722. exit;
  723. { find the correct vtype value }
  724. vtype:=$ff;
  725. vaddr:=false;
  726. lt:=hp^.left^.resulttype;
  727. case lt^.deftype of
  728. enumdef,
  729. orddef :
  730. begin
  731. if (lt^.deftype=enumdef) or
  732. is_integer(lt) then
  733. vtype:=vtInteger
  734. else
  735. if is_boolean(lt) then
  736. vtype:=vtBoolean
  737. else
  738. if (lt^.deftype=orddef) and (porddef(lt)^.typ=uchar) then
  739. vtype:=vtChar;
  740. end;
  741. floatdef :
  742. begin
  743. vtype:=vtExtended;
  744. vaddr:=true;
  745. end;
  746. procvardef,
  747. pointerdef :
  748. begin
  749. if is_pchar(lt) then
  750. vtype:=vtPChar
  751. else
  752. vtype:=vtPointer;
  753. end;
  754. classrefdef :
  755. vtype:=vtClass;
  756. objectdef :
  757. begin
  758. vtype:=vtObject;
  759. end;
  760. stringdef :
  761. begin
  762. if is_shortstring(lt) then
  763. begin
  764. vtype:=vtString;
  765. vaddr:=true;
  766. end
  767. else
  768. if is_ansistring(lt) then
  769. vtype:=vtAnsiString;
  770. end;
  771. end;
  772. if vtype=$ff then
  773. internalerror(14357);
  774. { write C style pushes or an pascal array }
  775. if p^.cargs then
  776. begin
  777. if vaddr then
  778. begin
  779. emit_to_reference(hp^.left);
  780. emit_push_lea_loc(hp^.left^.location);
  781. end
  782. else
  783. emit_push_loc(hp^.left^.location);
  784. end
  785. else
  786. begin
  787. { update href to the vtype field and write it }
  788. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,
  789. vtype,newreference(href))));
  790. inc(href.offset,4);
  791. { write changing field update href to the next element }
  792. if vaddr then
  793. begin
  794. emit_to_reference(hp^.left);
  795. emit_lea_loc_ref(hp^.left^.location,href);
  796. end
  797. else
  798. emit_mov_loc_ref(hp^.left^.location,href);
  799. inc(href.offset,4);
  800. end;
  801. end;
  802. { load next entry }
  803. hp:=hp^.right;
  804. end;
  805. end;
  806. end.
  807. {
  808. $Log$
  809. Revision 1.62 1999-06-30 15:43:18 florian
  810. * two bugs regarding method variables fixed
  811. - if you take in a method the address of another method
  812. don't need self anymore
  813. - if the class pointer was in a register, wrong code for a method
  814. variable load was generated
  815. Revision 1.61 1999/06/28 22:29:11 florian
  816. * qword division fixed
  817. + code for qword/int64 type casting added:
  818. range checking isn't implemented yet
  819. Revision 1.60 1999/05/31 12:42:43 peter
  820. * fixed crash with empty array constructor
  821. Revision 1.59 1999/05/27 19:44:14 peter
  822. * removed oldasm
  823. * plabel -> pasmlabel
  824. * -a switches to source writing automaticly
  825. * assembler readers OOPed
  826. * asmsymbol automaticly external
  827. * jumptables and other label fixes for asm readers
  828. Revision 1.58 1999/05/23 18:42:02 florian
  829. * better error recovering in typed constants
  830. * some problems with arrays of const fixed, some problems
  831. due my previous
  832. - the location type of array constructor is now LOC_MEM
  833. - the pushing of high fixed
  834. - parameter copying fixed
  835. - zero temp. allocation removed
  836. * small problem in the assembler writers fixed:
  837. ref to nil wasn't written correctly
  838. Revision 1.57 1999/05/21 13:54:51 peter
  839. * NEWLAB for label as symbol
  840. Revision 1.56 1999/05/17 23:51:38 peter
  841. * with temp vars now use a reference with a persistant temp instead
  842. of setting datasize
  843. Revision 1.55 1999/05/17 21:57:04 florian
  844. * new temporary ansistring handling
  845. Revision 1.54 1999/05/12 00:19:43 peter
  846. * removed R_DEFAULT_SEG
  847. * uniform float names
  848. Revision 1.53 1999/05/06 09:05:16 peter
  849. * generic write_float and str_float
  850. * fixed constant float conversions
  851. Revision 1.52 1999/05/01 13:24:10 peter
  852. * merged nasm compiler
  853. * old asm moved to oldasm/
  854. Revision 1.51 1999/04/28 06:01:55 florian
  855. * changes of Bruessel:
  856. + message handler can now take an explicit self
  857. * typinfo fixed: sometimes the type names weren't written
  858. * the type checking for pointer comparisations and subtraction
  859. and are now more strict (was also buggy)
  860. * small bug fix to link.pas to support compiling on another
  861. drive
  862. * probable bug in popt386 fixed: call/jmp => push/jmp
  863. transformation didn't count correctly the jmp references
  864. + threadvar support
  865. * warning if ln/sqrt gets an invalid constant argument
  866. Revision 1.50 1999/04/16 13:42:26 jonas
  867. * more regalloc fixes (still not complete)
  868. Revision 1.49 1999/04/13 18:57:48 florian
  869. * classes which contain ansistring get unnecessary calls
  870. to addref/decref when they are assigned, fixed
  871. Revision 1.48 1999/04/09 15:48:47 jonas
  872. * added fix for missing register deallocation (-dregallocfix)
  873. Revision 1.47 1999/03/31 13:55:07 peter
  874. * assembler inlining working for ag386bin
  875. Revision 1.46 1999/03/24 23:16:52 peter
  876. * fixed bugs 212,222,225,227,229,231,233
  877. Revision 1.45 1999/02/25 21:02:28 peter
  878. * ag386bin updates
  879. + coff writer
  880. Revision 1.44 1999/02/22 02:15:12 peter
  881. * updates for ag386bin
  882. Revision 1.43 1999/01/27 00:13:54 florian
  883. * "procedure of object"-stuff fixed
  884. Revision 1.42 1999/01/21 22:10:40 peter
  885. * fixed array of const
  886. * generic platform independent high() support
  887. Revision 1.41 1999/01/20 10:20:18 peter
  888. * don't make localvar copies for assembler procedures
  889. Revision 1.40 1998/12/30 13:41:07 peter
  890. * released valuepara
  891. Revision 1.39 1998/12/19 00:23:45 florian
  892. * ansistring memory leaks fixed
  893. Revision 1.38 1998/12/11 00:02:51 peter
  894. + globtype,tokens,version unit splitted from globals
  895. Revision 1.37 1998/12/10 09:47:17 florian
  896. + basic operations with int64/qord (compiler with -dint64)
  897. + rtti of enumerations extended: names are now written
  898. Revision 1.36 1998/12/04 10:18:06 florian
  899. * some stuff for procedures of object added
  900. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  901. Revision 1.35 1998/11/30 09:43:04 pierre
  902. * some range check bugs fixed (still not working !)
  903. + added DLL writing support for win32 (also accepts variables)
  904. + TempAnsi for code that could be used for Temporary ansi strings
  905. handling
  906. Revision 1.34 1998/11/28 16:20:48 peter
  907. + support for dll variables
  908. Revision 1.33 1998/11/27 14:50:33 peter
  909. + open strings, $P switch support
  910. Revision 1.32 1998/11/26 09:53:36 florian
  911. * for classes no init/final. code is necessary, fixed
  912. Revision 1.31 1998/11/20 15:35:54 florian
  913. * problems with rtti fixed, hope it works
  914. Revision 1.30 1998/11/18 17:45:24 peter
  915. * fixes for VALUEPARA
  916. Revision 1.29 1998/11/18 15:44:11 peter
  917. * VALUEPARA for tp7 compatible value parameters
  918. Revision 1.28 1998/11/17 11:32:44 peter
  919. * optimize str:='' in H+ mode
  920. + -! to test ansistrings
  921. Revision 1.27 1998/11/16 15:35:39 peter
  922. * rename laod/copystring -> load/copyshortstring
  923. * fixed int-bool cnv bug
  924. + char-ansistring conversion
  925. Revision 1.26 1998/11/10 10:09:10 peter
  926. * va_list -> array of const
  927. Revision 1.25 1998/11/05 12:02:35 peter
  928. * released useansistring
  929. * removed -Sv, its now available in fpc modes
  930. Revision 1.24 1998/10/14 08:47:14 pierre
  931. * bugs in secondfuncret for result in subprocedures removed
  932. Revision 1.23 1998/10/06 17:16:44 pierre
  933. * some memory leaks fixed (thanks to Peter for heaptrc !)
  934. Revision 1.22 1998/10/01 09:22:53 peter
  935. * fixed value openarray
  936. * ungettemp of arrayconstruct
  937. Revision 1.21 1998/09/28 11:07:39 peter
  938. + floatdef support for array of const
  939. Revision 1.20 1998/09/24 14:26:03 peter
  940. * updated for new tvarrec
  941. Revision 1.19 1998/09/23 17:49:59 peter
  942. * high(arrayconstructor) is now correct
  943. * procvardef support for variant record
  944. Revision 1.18 1998/09/23 09:58:48 peter
  945. * first working array of const things
  946. Revision 1.17 1998/09/20 18:00:19 florian
  947. * small compiling problems fixed
  948. Revision 1.16 1998/09/20 17:46:48 florian
  949. * some things regarding ansistrings fixed
  950. Revision 1.15 1998/09/17 09:42:16 peter
  951. + pass_2 for cg386
  952. * Message() -> CGMessage() for pass_1/pass_2
  953. Revision 1.14 1998/09/14 10:43:50 peter
  954. * all internal RTL functions start with FPC_
  955. Revision 1.13 1998/09/04 12:24:24 florian
  956. * bug0159 fixed
  957. Revision 1.12 1998/09/04 11:55:17 florian
  958. * problem with -Or fixed
  959. Revision 1.11 1998/09/03 16:03:14 florian
  960. + rtti generation
  961. * init table generation changed
  962. Revision 1.10 1998/08/21 14:08:40 pierre
  963. + TEST_FUNCRET now default (old code removed)
  964. works also for m68k (at least compiles)
  965. Revision 1.9 1998/08/20 09:26:37 pierre
  966. + funcret setting in underproc testing
  967. compile with _dTEST_FUNCRET
  968. Revision 1.8 1998/08/10 14:49:48 peter
  969. + localswitches, moduleswitches, globalswitches splitting
  970. Revision 1.7 1998/07/30 13:30:33 florian
  971. * final implemenation of exception support, maybe it needs
  972. some fixes :)
  973. Revision 1.6 1998/07/26 21:58:57 florian
  974. + better support for switch $H
  975. + index access to ansi strings added
  976. + assigment of data (records/arrays) containing ansi strings
  977. Revision 1.5 1998/07/24 22:16:54 florian
  978. * internal error 10 together with array access fixed. I hope
  979. that's the final fix.
  980. Revision 1.4 1998/06/11 13:58:45 peter
  981. * fixed too long line
  982. Revision 1.3 1998/06/09 16:01:35 pierre
  983. + added procedure directive parsing for procvars
  984. (accepted are popstack cdecl and pascal)
  985. + added C vars with the following syntax
  986. var C calias 'true_c_name';(can be followed by external)
  987. reason is that you must add the Cprefix
  988. which is target dependent
  989. Revision 1.2 1998/06/08 13:13:34 pierre
  990. + temporary variables now in temp_gen.pas unit
  991. because it is processor independent
  992. * mppc68k.bat modified to undefine i386 and support_mmx
  993. (which are defaults for i386)
  994. Revision 1.1 1998/06/05 17:44:12 peter
  995. * splitted cgi386
  996. }