cg386ld.pas 46 KB

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