cg386cal.pas 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for in call/inline 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 cg386cal;
  19. interface
  20. uses
  21. symtable,tree;
  22. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  23. push_from_left_to_right,inlined : boolean;para_offset : longint);
  24. procedure secondcalln(var p : ptree);
  25. procedure secondinline(var p : ptree);
  26. procedure secondprocinline(var p : ptree);
  27. implementation
  28. uses
  29. cobjects,verbose,globals,systems,
  30. aasm,i386,types,
  31. cgi386,cgai386,temp_gen,tgeni386,hcodegen,
  32. cg386ld;
  33. {*****************************************************************************
  34. SecondCallParaN
  35. *****************************************************************************}
  36. { save the size of pushed parameter }
  37. var
  38. pushedparasize : longint;
  39. procedure secondcallparan(var p : ptree;defcoll : pdefcoll;
  40. push_from_left_to_right,inlined : boolean;para_offset : longint);
  41. procedure maybe_push_open_array_high;
  42. var
  43. r : preference;
  44. begin
  45. { open array ? }
  46. { defcoll^.data can be nil for read/write }
  47. if assigned(defcoll^.data) and
  48. is_open_array(defcoll^.data) then
  49. begin
  50. inc(pushedparasize,4);
  51. { push high }
  52. if is_open_array(p^.left^.resulttype) then
  53. begin
  54. r:=new_reference(highframepointer,highoffset+4);
  55. if inlined then
  56. begin
  57. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  58. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  59. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  60. end
  61. else
  62. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)));
  63. end
  64. else
  65. begin
  66. if inlined then
  67. begin
  68. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  69. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_L,
  70. parraydef(p^.left^.resulttype)^.highrange-
  71. parraydef(p^.left^.resulttype)^.lowrange,r)));
  72. end
  73. else
  74. push_int(parraydef(p^.left^.resulttype)^.highrange-
  75. parraydef(p^.left^.resulttype)^.lowrange);
  76. end;
  77. end;
  78. end;
  79. var
  80. size : longint;
  81. stackref : treference;
  82. otlabel,hlabel,oflabel : plabel;
  83. { temporary variables: }
  84. tempdeftype : tdeftype;
  85. tempreference : treference;
  86. r : preference;
  87. s : topsize;
  88. op : tasmop;
  89. begin
  90. { push from left to right if specified }
  91. if push_from_left_to_right and assigned(p^.right) then
  92. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,inlined,para_offset);
  93. otlabel:=truelabel;
  94. oflabel:=falselabel;
  95. getlabel(truelabel);
  96. getlabel(falselabel);
  97. secondpass(p^.left);
  98. { in codegen.handleread.. defcoll^.data is set to nil }
  99. if assigned(defcoll^.data) and
  100. (defcoll^.data^.deftype=formaldef) then
  101. begin
  102. { allow @var }
  103. inc(pushedparasize,4);
  104. if p^.left^.treetype=addrn then
  105. begin
  106. { always a register }
  107. if inlined then
  108. begin
  109. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  110. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  111. p^.left^.location.register,r)));
  112. end
  113. else
  114. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
  115. ungetregister32(p^.left^.location.register);
  116. end
  117. else
  118. begin
  119. if not(p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  120. Message(sym_e_type_mismatch)
  121. else
  122. begin
  123. if inlined then
  124. begin
  125. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  126. newreference(p^.left^.location.reference),R_EDI)));
  127. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  128. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  129. end
  130. else
  131. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  132. del_reference(p^.left^.location.reference);
  133. end;
  134. end;
  135. end
  136. { handle call by reference parameter }
  137. else if (defcoll^.paratyp=vs_var) then
  138. begin
  139. if (p^.left^.location.loc<>LOC_REFERENCE) then
  140. Message(cg_e_var_must_be_reference);
  141. maybe_push_open_array_high;
  142. inc(pushedparasize,4);
  143. if inlined then
  144. begin
  145. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  146. newreference(p^.left^.location.reference),R_EDI)));
  147. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  148. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  149. end
  150. else
  151. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  152. del_reference(p^.left^.location.reference);
  153. end
  154. else
  155. begin
  156. tempdeftype:=p^.resulttype^.deftype;
  157. if tempdeftype=filedef then
  158. Message(cg_e_file_must_call_by_reference);
  159. if (defcoll^.paratyp=vs_const) and
  160. dont_copy_const_param(p^.resulttype) then
  161. begin
  162. maybe_push_open_array_high;
  163. inc(pushedparasize,4);
  164. if inlined then
  165. begin
  166. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  167. newreference(p^.left^.location.reference),R_EDI)));
  168. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  169. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  170. R_EDI,r)));
  171. end
  172. else
  173. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  174. del_reference(p^.left^.location.reference);
  175. end
  176. else
  177. case p^.left^.location.loc of
  178. LOC_REGISTER,
  179. LOC_CREGISTER:
  180. begin
  181. case p^.left^.location.register of
  182. R_EAX,R_EBX,R_ECX,R_EDX,R_ESI,
  183. R_EDI,R_ESP,R_EBP :
  184. begin
  185. inc(pushedparasize,4);
  186. if inlined then
  187. begin
  188. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  189. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  190. p^.left^.location.register,r)));
  191. end
  192. else
  193. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,p^.left^.location.register)));
  194. ungetregister32(p^.left^.location.register);
  195. end;
  196. R_AX,R_BX,R_CX,R_DX,R_SI,R_DI:
  197. begin
  198. inc(pushedparasize,2);
  199. if inlined then
  200. begin
  201. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  202. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,
  203. p^.left^.location.register,r)));
  204. end
  205. else
  206. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,p^.left^.location.register)));
  207. ungetregister32(reg16toreg32(p^.left^.location.register));
  208. end;
  209. R_AL,R_BL,R_CL,R_DL:
  210. begin
  211. inc(pushedparasize,2);
  212. { we must push always 16 bit }
  213. if inlined then
  214. begin
  215. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  216. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  217. reg8toreg16(p^.left^.location.register),r)));
  218. end
  219. else
  220. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,
  221. reg8toreg16(p^.left^.location.register))));
  222. ungetregister32(reg8toreg32(p^.left^.location.register));
  223. end;
  224. end;
  225. end;
  226. LOC_FPU:
  227. begin
  228. size:=pfloatdef(p^.left^.resulttype)^.size;
  229. inc(pushedparasize,size); { must be before for inlined }
  230. if not inlined then
  231. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  232. r:=new_reference(R_ESP,0);
  233. floatstoreops(pfloatdef(p^.left^.resulttype)^.typ,op,s);
  234. { this is the easiest case for inlined !! }
  235. if inlined then
  236. begin
  237. r^.base:=procinfo.framepointer;
  238. r^.offset:=para_offset-pushedparasize;
  239. end;
  240. exprasmlist^.concat(new(pai386,op_ref(op,s,r)));
  241. end;
  242. LOC_REFERENCE,LOC_MEM:
  243. begin
  244. tempreference:=p^.left^.location.reference;
  245. del_reference(p^.left^.location.reference);
  246. case p^.resulttype^.deftype of
  247. orddef :
  248. begin
  249. case porddef(p^.resulttype)^.typ of
  250. s32bit,u32bit,bool32bit :
  251. begin
  252. inc(pushedparasize,4);
  253. if inlined then
  254. begin
  255. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  256. newreference(tempreference),R_EDI)));
  257. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  258. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  259. end
  260. else
  261. emit_push_mem(tempreference);
  262. end;
  263. s8bit,u8bit,uchar,bool8bit,
  264. bool16bit,s16bit,u16bit :
  265. begin
  266. inc(pushedparasize,2);
  267. if inlined then
  268. begin
  269. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  270. newreference(tempreference),R_DI)));
  271. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  272. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,R_DI,r)));
  273. end
  274. else
  275. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,
  276. newreference(tempreference))));
  277. end;
  278. end;
  279. end;
  280. floatdef :
  281. begin
  282. case pfloatdef(p^.resulttype)^.typ of
  283. f32bit,
  284. s32real :
  285. begin
  286. inc(pushedparasize,4);
  287. if inlined then
  288. begin
  289. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  290. newreference(tempreference),R_EDI)));
  291. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  292. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  293. end
  294. else
  295. emit_push_mem(tempreference);
  296. end;
  297. s64real,
  298. s64bit :
  299. begin
  300. inc(pushedparasize,4);
  301. inc(tempreference.offset,4);
  302. if inlined then
  303. begin
  304. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  305. newreference(tempreference),R_EDI)));
  306. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  307. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  308. end
  309. else
  310. emit_push_mem(tempreference);
  311. inc(pushedparasize,4);
  312. dec(tempreference.offset,4);
  313. if inlined then
  314. begin
  315. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  316. newreference(tempreference),R_EDI)));
  317. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  318. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  319. end
  320. else
  321. emit_push_mem(tempreference);
  322. end;
  323. s80real :
  324. begin
  325. inc(pushedparasize,4);
  326. inc(tempreference.offset,6);
  327. if inlined then
  328. begin
  329. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  330. newreference(tempreference),R_EDI)));
  331. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  332. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  333. end
  334. else
  335. emit_push_mem(tempreference);
  336. dec(tempreference.offset,4);
  337. inc(pushedparasize,4);
  338. if inlined then
  339. begin
  340. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  341. newreference(tempreference),R_EDI)));
  342. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  343. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  344. end
  345. else
  346. emit_push_mem(tempreference);
  347. dec(tempreference.offset,2);
  348. inc(pushedparasize,2);
  349. if inlined then
  350. begin
  351. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,
  352. newreference(tempreference),R_DI)));
  353. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  354. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,R_DI,r)));
  355. end
  356. else
  357. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_W,
  358. newreference(tempreference))));
  359. end;
  360. end;
  361. end;
  362. pointerdef,procvardef,
  363. enumdef,classrefdef:
  364. begin
  365. inc(pushedparasize,4);
  366. if inlined then
  367. begin
  368. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  369. newreference(tempreference),R_EDI)));
  370. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  371. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,r)));
  372. end
  373. else
  374. emit_push_mem(tempreference);
  375. end;
  376. arraydef,recorddef,stringdef,setdef,objectdef :
  377. begin
  378. { small set ? }
  379. if ((p^.resulttype^.deftype=setdef) and
  380. (psetdef(p^.resulttype)^.settype=smallset)) then
  381. begin
  382. inc(pushedparasize,4);
  383. if inlined then
  384. begin
  385. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  386. concatcopy(tempreference,r^,4,false);
  387. end
  388. else
  389. emit_push_mem(tempreference);
  390. end
  391. { call by value open array ? }
  392. else
  393. if (p^.resulttype^.deftype=arraydef) and
  394. assigned(defcoll^.data) and
  395. is_open_array(defcoll^.data) then
  396. begin
  397. { first, push high }
  398. maybe_push_open_array_high;
  399. inc(pushedparasize,4);
  400. if inlined then
  401. begin
  402. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  403. newreference(p^.left^.location.reference),R_EDI)));
  404. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  405. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  406. R_EDI,r)));
  407. end
  408. else
  409. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  410. end
  411. else
  412. begin
  413. size:=p^.resulttype^.size;
  414. { Word Alignment }
  415. if Odd(size) then
  416. inc(size);
  417. { create stack space }
  418. if not inlined then
  419. exprasmlist^.concat(new(pai386,op_const_reg(A_SUB,S_L,size,R_ESP)));
  420. inc(pushedparasize,size);
  421. { create stack reference }
  422. stackref.symbol := nil;
  423. if not inlined then
  424. begin
  425. clear_reference(stackref);
  426. stackref.base:=R_ESP;
  427. end
  428. else
  429. begin
  430. clear_reference(stackref);
  431. stackref.base:=procinfo.framepointer;
  432. stackref.offset:=para_offset-pushedparasize;
  433. end;
  434. { produce copy }
  435. if p^.resulttype^.deftype=stringdef then
  436. begin
  437. copystring(stackref,p^.left^.location.reference,
  438. pstringdef(p^.resulttype)^.len);
  439. end
  440. else
  441. begin
  442. concatcopy(p^.left^.location.reference,
  443. stackref,p^.resulttype^.size,true);
  444. end;
  445. end;
  446. end;
  447. else
  448. Message(cg_e_illegal_expression);
  449. end;
  450. end;
  451. LOC_JUMP:
  452. begin
  453. getlabel(hlabel);
  454. inc(pushedparasize,2);
  455. emitl(A_LABEL,truelabel);
  456. if inlined then
  457. begin
  458. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  459. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_W,1,r)));
  460. end
  461. else
  462. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_W,1)));
  463. emitl(A_JMP,hlabel);
  464. emitl(A_LABEL,falselabel);
  465. if inlined then
  466. begin
  467. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  468. exprasmlist^.concat(new(pai386,op_const_ref(A_MOV,S_W,0,r)));
  469. end
  470. else
  471. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_W,0)));
  472. emitl(A_LABEL,hlabel);
  473. end;
  474. LOC_FLAGS:
  475. begin
  476. if not(R_EAX in unused) then
  477. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));
  478. { clear full EAX is faster }
  479. { but dont you set the equal flag ? }
  480. {exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EAX,R_EAX)));}
  481. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  482. R_AL)));
  483. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOVZX,S_BW,R_AL,R_AX)));
  484. {exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EAX,R_EAX)));}
  485. inc(pushedparasize,2);
  486. if inlined then
  487. begin
  488. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  489. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_W,
  490. R_AX,r)));
  491. end
  492. else
  493. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_W,R_AX)));
  494. { this is also false !!!
  495. if not(R_EAX in unused) then
  496. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EAX,R_EDI)));}
  497. if not(R_EAX in unused) then
  498. exprasmlist^.concat(new(pai386,op_reg_reg(A_MOV,S_L,R_EDI,R_EAX)));
  499. end;
  500. {$ifdef SUPPORT_MMX}
  501. LOC_MMXREGISTER,
  502. LOC_CMMXREGISTER:
  503. begin
  504. inc(pushedparasize,8); { was missing !!! (PM) }
  505. exprasmlist^.concat(new(pai386,op_const_reg(
  506. A_SUB,S_L,8,R_ESP)));
  507. if inlined then
  508. begin
  509. r:=new_reference(procinfo.framepointer,para_offset-pushedparasize);
  510. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOVQ,S_NO,
  511. p^.left^.location.register,r)));
  512. end
  513. else
  514. begin
  515. r:=new_reference(R_ESP,0);
  516. exprasmlist^.concat(new(pai386,op_reg_ref(
  517. A_MOVQ,S_NO,p^.left^.location.register,r)));
  518. end;
  519. end;
  520. {$endif SUPPORT_MMX}
  521. end;
  522. end;
  523. truelabel:=otlabel;
  524. falselabel:=oflabel;
  525. { push from right to left }
  526. if not push_from_left_to_right and assigned(p^.right) then
  527. secondcallparan(p^.right,defcoll^.next,push_from_left_to_right,inlined,para_offset);
  528. end;
  529. {*****************************************************************************
  530. SecondCallN
  531. *****************************************************************************}
  532. procedure secondcalln(var p : ptree);
  533. var
  534. unusedregisters : tregisterset;
  535. pushed : tpushed;
  536. funcretref : treference;
  537. hregister : tregister;
  538. oldpushedparasize : longint;
  539. { true if ESI must be loaded again after the subroutine }
  540. loadesi : boolean;
  541. { true if a virtual method must be called directly }
  542. no_virtual_call : boolean;
  543. { true if we produce a con- or destrutor in a call }
  544. is_con_or_destructor : boolean;
  545. { true if a constructor is called again }
  546. extended_new : boolean;
  547. { adress returned from an I/O-error }
  548. iolabel : plabel;
  549. { lexlevel count }
  550. i : longint;
  551. { help reference pointer }
  552. r : preference;
  553. pp,params : ptree;
  554. inlined : boolean;
  555. inlinecode : ptree;
  556. para_offset : longint;
  557. { instruction for alignement correction }
  558. corr : pai386;
  559. { we must pop this size also after !! }
  560. must_pop : boolean;
  561. pop_size : longint;
  562. label
  563. dont_call;
  564. begin
  565. extended_new:=false;
  566. iolabel:=nil;
  567. inlinecode:=nil;
  568. inlined:=false;
  569. loadesi:=true;
  570. no_virtual_call:=false;
  571. unusedregisters:=unused;
  572. if not assigned(p^.procdefinition) then
  573. exit;
  574. if (p^.procdefinition^.options and poinline)<>0 then
  575. begin
  576. inlined:=true;
  577. inlinecode:=p^.right;
  578. { set it to the same lexical level }
  579. p^.procdefinition^.parast^.symtablelevel:=
  580. aktprocsym^.definition^.parast^.symtablelevel;
  581. if assigned(p^.left) then
  582. inlinecode^.para_offset:=
  583. gettempofsizepersistant(inlinecode^.para_size);
  584. p^.procdefinition^.parast^.call_offset:=
  585. inlinecode^.para_offset;
  586. {$ifdef extdebug}
  587. Comment(V_debug,
  588. 'inlined parasymtable is at offset '
  589. +tostr(p^.procdefinition^.parast^.call_offset));
  590. exprasmlist^.concat(new(pai_asm_comment,init(
  591. strpnew('inlined parasymtable is at offset '
  592. +tostr(p^.procdefinition^.parast^.call_offset)))));
  593. {$endif extdebug}
  594. p^.right:=nil;
  595. { disable further inlining of the same proc
  596. in the args }
  597. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  598. end;
  599. { only if no proc var }
  600. if not(assigned(p^.right)) then
  601. is_con_or_destructor:=((p^.procdefinition^.options and poconstructor)<>0)
  602. or ((p^.procdefinition^.options and podestructor)<>0);
  603. { proc variables destroy all registers }
  604. if (p^.right=nil) and
  605. { virtual methods too }
  606. ((p^.procdefinition^.options and povirtualmethod)=0) then
  607. begin
  608. if ((p^.procdefinition^.options and poiocheck)<>0)
  609. and (cs_iocheck in aktswitches) then
  610. begin
  611. getlabel(iolabel);
  612. emitl(A_LABEL,iolabel);
  613. end
  614. else iolabel:=nil;
  615. { save all used registers }
  616. pushusedregisters(pushed,p^.procdefinition^.usedregisters);
  617. { give used registers through }
  618. usedinproc:=usedinproc or p^.procdefinition^.usedregisters;
  619. end
  620. else
  621. begin
  622. pushusedregisters(pushed,$ff);
  623. usedinproc:=$ff;
  624. { no IO check for methods and procedure variables }
  625. iolabel:=nil;
  626. end;
  627. { generate the code for the parameter and push them }
  628. oldpushedparasize:=pushedparasize;
  629. pushedparasize:=0;
  630. corr:=new(pai386,op_const_reg(A_SUB,S_L,0,R_ESP));
  631. exprasmlist^.concat(corr);
  632. if (p^.resulttype<>pdef(voiddef)) and
  633. ret_in_param(p^.resulttype) then
  634. begin
  635. funcretref.symbol:=nil;
  636. {$ifdef test_dest_loc}
  637. if dest_loc_known and (dest_loc_tree=p) and
  638. (dest_loc.loc in [LOC_REFERENCE,LOC_MEM]) then
  639. begin
  640. funcretref:=dest_loc.reference;
  641. if assigned(dest_loc.reference.symbol) then
  642. funcretref.symbol:=stringdup(dest_loc.reference.symbol^);
  643. in_dest_loc:=true;
  644. end
  645. else
  646. {$endif test_dest_loc}
  647. if inlined then
  648. begin
  649. reset_reference(funcretref);
  650. funcretref.offset:=gettempofsizepersistant(p^.procdefinition^.retdef^.size);
  651. funcretref.base:=procinfo.framepointer;
  652. end
  653. else
  654. gettempofsizereference(p^.procdefinition^.retdef^.size,funcretref);
  655. end;
  656. if assigned(p^.left) then
  657. begin
  658. pushedparasize:=0;
  659. { be found elsewhere }
  660. if inlined then
  661. para_offset:=p^.procdefinition^.parast^.call_offset+
  662. p^.procdefinition^.parast^.datasize
  663. else
  664. para_offset:=0;
  665. if assigned(p^.right) then
  666. secondcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1,
  667. (p^.procdefinition^.options and poleftright)<>0,inlined,para_offset)
  668. else
  669. secondcallparan(p^.left,p^.procdefinition^.para1,
  670. (p^.procdefinition^.options and poleftright)<>0,inlined,para_offset);
  671. end;
  672. params:=p^.left;
  673. p^.left:=nil;
  674. if inlined then
  675. inlinecode^.retoffset:=gettempofsizepersistant(4);
  676. if ret_in_param(p^.resulttype) then
  677. begin
  678. inc(pushedparasize,4);
  679. if inlined then
  680. begin
  681. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  682. newreference(funcretref),R_EDI)));
  683. r:=new_reference(procinfo.framepointer,inlinecode^.retoffset);
  684. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,
  685. R_EDI,r)));
  686. end
  687. else
  688. emitpushreferenceaddr(exprasmlist,funcretref);
  689. end;
  690. { procedure variable ? }
  691. if (p^.right=nil) then
  692. begin
  693. { overloaded operator have no symtable }
  694. { push self }
  695. if assigned(p^.symtable) and
  696. (p^.symtable^.symtabletype=withsymtable) then
  697. begin
  698. { dirty trick to avoid the secondcall below }
  699. p^.methodpointer:=genzeronode(callparan);
  700. p^.methodpointer^.location.loc:=LOC_REGISTER;
  701. p^.methodpointer^.location.register:=R_ESI;
  702. { make a reference }
  703. new(r);
  704. reset_reference(r^);
  705. r^.offset:=p^.symtable^.datasize;
  706. r^.base:=procinfo.framepointer;
  707. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  708. end;
  709. { push self }
  710. if assigned(p^.symtable) and
  711. ((p^.symtable^.symtabletype=objectsymtable) or
  712. (p^.symtable^.symtabletype=withsymtable)) then
  713. begin
  714. if assigned(p^.methodpointer) then
  715. begin
  716. {
  717. if p^.methodpointer^.resulttype=classrefdef then
  718. begin
  719. two possibilities:
  720. 1. constructor
  721. 2. class method
  722. end
  723. else }
  724. begin
  725. case p^.methodpointer^.treetype of
  726. typen:
  727. begin
  728. { direct call to inherited method }
  729. if (p^.procdefinition^.options and poabstractmethod)<>0 then
  730. begin
  731. Message(cg_e_cant_call_abstract_method);
  732. goto dont_call;
  733. end;
  734. { generate no virtual call }
  735. no_virtual_call:=true;
  736. if (p^.symtableprocentry^.properties and sp_static)<>0 then
  737. begin
  738. { well lets put the VMT address directly into ESI }
  739. { it is kind of dirty but that is the simplest }
  740. { way to accept virtual static functions (PM) }
  741. loadesi:=true;
  742. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,S_L,
  743. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0),R_ESI)));
  744. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  745. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  746. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  747. end
  748. else
  749. { this is a member call, so ESI isn't modfied }
  750. loadesi:=false;
  751. if not(is_con_or_destructor and
  752. pobjectdef(p^.methodpointer^.resulttype)^.isclass and
  753. assigned(aktprocsym) and
  754. ((aktprocsym^.definition^.options and
  755. (poconstructor or podestructor))<>0)) then
  756. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  757. { if an inherited con- or destructor should be }
  758. { called in a con- or destructor then a warning }
  759. { will be made }
  760. { con- and destructors need a pointer to the vmt }
  761. if is_con_or_destructor and
  762. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) and
  763. assigned(aktprocsym) then
  764. begin
  765. if not ((aktprocsym^.definition^.options
  766. and (poconstructor or podestructor))<>0) then
  767. Message(cg_w_member_cd_call_from_method);
  768. end;
  769. if is_con_or_destructor then
  770. push_int(0)
  771. end;
  772. hnewn:
  773. begin
  774. { extended syntax of new }
  775. { ESI must be zero }
  776. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_ESI,R_ESI)));
  777. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  778. { insert the vmt }
  779. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
  780. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  781. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  782. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  783. extended_new:=true;
  784. end;
  785. hdisposen:
  786. begin
  787. secondpass(p^.methodpointer);
  788. { destructor with extended syntax called from dispose }
  789. { hdisposen always deliver LOC_REFERENCE }
  790. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  791. newreference(p^.methodpointer^.location.reference),R_ESI)));
  792. del_reference(p^.methodpointer^.location.reference);
  793. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  794. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
  795. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,0))));
  796. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  797. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  798. end;
  799. else
  800. begin
  801. { call to an instance member }
  802. if (p^.symtable^.symtabletype<>withsymtable) then
  803. begin
  804. secondpass(p^.methodpointer);
  805. case p^.methodpointer^.location.loc of
  806. LOC_REGISTER:
  807. begin
  808. ungetregister32(p^.methodpointer^.location.register);
  809. emit_reg_reg(A_MOV,S_L,p^.methodpointer^.location.register,R_ESI);
  810. end;
  811. else
  812. begin
  813. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  814. pobjectdef(p^.methodpointer^.resulttype)^.isclass then
  815. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  816. newreference(p^.methodpointer^.location.reference),R_ESI)))
  817. else
  818. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,
  819. newreference(p^.methodpointer^.location.reference),R_ESI)));
  820. del_reference(p^.methodpointer^.location.reference);
  821. end;
  822. end;
  823. end;
  824. { when calling a class method, we have
  825. to load ESI with the VMT !
  826. But that's wrong, if we call a class method via self
  827. }
  828. if ((p^.procdefinition^.options and poclassmethod)<>0)
  829. and not(p^.methodpointer^.treetype=selfn) then
  830. begin
  831. { class method needs current VMT }
  832. new(r);
  833. reset_reference(r^);
  834. r^.base:=R_ESI;
  835. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  836. end;
  837. { direct call to class constructor, don't allocate memory }
  838. if is_con_or_destructor and (p^.methodpointer^.resulttype^.deftype=objectdef) and
  839. (pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  840. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,0)))
  841. else
  842. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  843. if is_con_or_destructor then
  844. begin
  845. { classes don't get a VMT pointer pushed }
  846. if (p^.methodpointer^.resulttype^.deftype=objectdef) and
  847. not(pobjectdef(p^.methodpointer^.resulttype)^.isclass) then
  848. begin
  849. if ((p^.procdefinition^.options and poconstructor)<>0) then
  850. begin
  851. { it's no bad idea, to insert the VMT }
  852. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,
  853. newcsymbol(pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname,
  854. 0))));
  855. maybe_concat_external(pobjectdef(p^.methodpointer^.resulttype)^.owner,
  856. pobjectdef(p^.methodpointer^.resulttype)^.vmt_mangledname);
  857. end
  858. { destructors haven't to dispose the instance, if this is }
  859. { a direct call }
  860. else
  861. push_int(0);
  862. end;
  863. end;
  864. end;
  865. end;
  866. end;
  867. end
  868. else
  869. begin
  870. if ((p^.procdefinition^.options and poclassmethod)<>0) and
  871. not(
  872. assigned(aktprocsym) and
  873. ((aktprocsym^.definition^.options and poclassmethod)<>0)
  874. ) then
  875. begin
  876. { class method needs current VMT }
  877. new(r);
  878. reset_reference(r^);
  879. r^.base:=R_ESI;
  880. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_ESI)));
  881. end
  882. else
  883. begin
  884. { member call, ESI isn't modified }
  885. loadesi:=false;
  886. end;
  887. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  888. { but a con- or destructor here would probably almost }
  889. { always be placed wrong }
  890. if is_con_or_destructor then
  891. begin
  892. Message(cg_w_member_cd_call_from_method);
  893. push_int(0);
  894. end;
  895. end;
  896. end;
  897. { push base pointer ?}
  898. if (lexlevel>1) and assigned(pprocdef(p^.procdefinition)^.parast) and
  899. ((p^.procdefinition^.parast^.symtablelevel)>2) then
  900. begin
  901. { if we call a nested function in a method, we must }
  902. { push also SELF! }
  903. { THAT'S NOT TRUE, we have to load ESI via frame pointer }
  904. { access }
  905. {
  906. begin
  907. loadesi:=false;
  908. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  909. end;
  910. }
  911. if lexlevel=(p^.procdefinition^.parast^.symtablelevel) then
  912. begin
  913. new(r);
  914. reset_reference(r^);
  915. r^.offset:=procinfo.framepointer_offset;
  916. r^.base:=procinfo.framepointer;
  917. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,r)))
  918. end
  919. { this is only true if the difference is one !!
  920. but it cannot be more !! }
  921. else if (lexlevel=p^.procdefinition^.parast^.symtablelevel-1) then
  922. begin
  923. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,procinfo.framepointer)))
  924. end
  925. else if (lexlevel>p^.procdefinition^.parast^.symtablelevel) then
  926. begin
  927. hregister:=getregister32;
  928. new(r);
  929. reset_reference(r^);
  930. r^.offset:=procinfo.framepointer_offset;
  931. r^.base:=procinfo.framepointer;
  932. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  933. for i:=(p^.procdefinition^.parast^.symtablelevel) to lexlevel-1 do
  934. begin
  935. new(r);
  936. reset_reference(r^);
  937. {we should get the correct frame_pointer_offset at each level
  938. how can we do this !!! }
  939. r^.offset:=procinfo.framepointer_offset;
  940. r^.base:=hregister;
  941. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,hregister)));
  942. end;
  943. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,hregister)));
  944. ungetregister32(hregister);
  945. end
  946. else
  947. internalerror(25000);
  948. end;
  949. { exported methods should be never called direct.
  950. Why? Bp7 Allows it (PFV)
  951. if (p^.procdefinition^.options and poexports)<>0 then
  952. Message(cg_e_dont_call_exported_direct); }
  953. if (not inlined) and ((pushedparasize mod 4)<>0) then
  954. begin
  955. corr^.op1:=pointer(4-(pushedparasize mod 4));
  956. must_pop:=true;
  957. pop_size:=4-(pushedparasize mod 4);
  958. end
  959. else
  960. begin
  961. exprasmlist^.remove(corr);
  962. must_pop:=false;
  963. pop_size:=0;
  964. end;
  965. if ((p^.procdefinition^.options and povirtualmethod)<>0) and
  966. not(no_virtual_call) then
  967. begin
  968. { static functions contain the vmt_address in ESI }
  969. { also class methods }
  970. if assigned(aktprocsym) then
  971. begin
  972. if ((aktprocsym^.properties and sp_static)<>0) or
  973. ((aktprocsym^.definition^.options and poclassmethod)<>0) or
  974. ((p^.procdefinition^.options and postaticmethod)<>0) or
  975. ((p^.procdefinition^.options and poconstructor)<>0) or
  976. { ESI is loaded earlier }
  977. ((p^.procdefinition^.options and poclassmethod)<>0)then
  978. begin
  979. new(r);
  980. reset_reference(r^);
  981. r^.base:=R_ESI;
  982. end
  983. else
  984. begin
  985. new(r);
  986. reset_reference(r^);
  987. r^.base:=R_ESI;
  988. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  989. new(r);
  990. reset_reference(r^);
  991. r^.base:=R_EDI;
  992. end;
  993. end
  994. else
  995. { aktprocsym should be assigned, also in main program }
  996. internalerror(12345);
  997. {
  998. begin
  999. new(r);
  1000. reset_reference(r^);
  1001. r^.base:=R_ESI;
  1002. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,R_EDI)));
  1003. new(r);
  1004. reset_reference(r^);
  1005. r^.base:=R_EDI;
  1006. end;
  1007. }
  1008. if p^.procdefinition^.extnumber=-1 then
  1009. internalerror($Da);
  1010. r^.offset:=p^.procdefinition^.extnumber*4+12;
  1011. if (cs_rangechecking in aktswitches) then
  1012. begin
  1013. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r^.base)));
  1014. emitcall('CHECK_OBJECT',true);
  1015. end;
  1016. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,r)));
  1017. end
  1018. else if not inlined then
  1019. emitcall(p^.procdefinition^.mangledname,
  1020. (p^.symtableproc^.symtabletype=unitsymtable) or
  1021. ((p^.symtableproc^.symtabletype=objectsymtable) and
  1022. (pobjectdef(p^.symtableproc^.defowner)^.owner^.symtabletype=unitsymtable)))
  1023. else { inlined proc }
  1024. { inlined code is in inlinecode }
  1025. begin
  1026. secondpass(inlinecode);
  1027. { set poinline again }
  1028. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  1029. { free the args }
  1030. ungetpersistanttemp(p^.procdefinition^.parast^.call_offset,
  1031. p^.procdefinition^.parast^.datasize);
  1032. end;
  1033. end
  1034. else
  1035. { now procedure variable case }
  1036. begin
  1037. if (pushedparasize mod 4)<>0 then
  1038. begin
  1039. corr^.op1:=pointer(4-(pushedparasize mod 4));
  1040. must_pop:=true;
  1041. pop_size:=4-(pushedparasize mod 4);
  1042. end
  1043. else
  1044. begin
  1045. exprasmlist^.remove(corr);
  1046. must_pop:=false;
  1047. pop_size:=0;
  1048. end;
  1049. secondpass(p^.right);
  1050. { method pointer ? }
  1051. if (p^.procdefinition^.options and pomethodpointer)<>0 then
  1052. begin
  1053. { method pointer can't be in a register }
  1054. inc(p^.right^.location.reference.offset,4);
  1055. { push self pointer }
  1056. exprasmlist^.concat(new(pai386,op_ref(A_PUSH,S_L,newreference(p^.right^.location.reference))));
  1057. del_reference(p^.right^.location.reference);
  1058. dec(p^.right^.location.reference.offset,4);
  1059. end;
  1060. case p^.right^.location.loc of
  1061. LOC_REGISTER,LOC_CREGISTER:
  1062. begin
  1063. exprasmlist^.concat(new(pai386,op_reg(A_CALL,S_NO,p^.right^.location.register)));
  1064. ungetregister32(p^.right^.location.register);
  1065. end
  1066. else
  1067. exprasmlist^.concat(new(pai386,op_ref(A_CALL,S_NO,newreference(p^.right^.location.reference))));
  1068. del_reference(p^.right^.location.reference);
  1069. end;
  1070. end;
  1071. { this was only for normal functions
  1072. displaced here so we also get
  1073. it to work for procvars PM }
  1074. if (not inlined) and ((p^.procdefinition^.options and poclearstack)<>0) then
  1075. begin
  1076. { consider the alignment with the rest (PM) }
  1077. pushedparasize:=pushedparasize+pop_size;
  1078. must_pop:=false;
  1079. if pushedparasize=4 then
  1080. { better than an add on all processors }
  1081. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)))
  1082. { the pentium has two pipes and pop reg is pairable }
  1083. { but the registers must be different! }
  1084. else if (pushedparasize=8) and
  1085. not(cs_littlesize in aktswitches) and
  1086. (aktoptprocessor=pentium) and
  1087. (procinfo._class=nil) then
  1088. begin
  1089. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  1090. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  1091. end
  1092. else exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pushedparasize,R_ESP)));
  1093. end;
  1094. dont_call:
  1095. pushedparasize:=oldpushedparasize;
  1096. unused:=unusedregisters;
  1097. { handle function results }
  1098. { structured results are easy to handle.... }
  1099. { needed also when result_no_used !! }
  1100. if (p^.resulttype<>pdef(voiddef)) and ret_in_param(p^.resulttype) then
  1101. begin
  1102. p^.location.loc:=LOC_MEM;
  1103. stringdispose(p^.location.reference.symbol);
  1104. p^.location.reference:=funcretref;
  1105. end;
  1106. if (p^.resulttype<>pdef(voiddef)) and p^.return_value_used then
  1107. begin
  1108. { a contructor could be a function with boolean result }
  1109. if (p^.right=nil) and
  1110. ((p^.procdefinition^.options and poconstructor)<>0) and
  1111. { quick'n'dirty check if it is a class or an object }
  1112. (p^.resulttype^.deftype=orddef) then
  1113. begin
  1114. p^.location.loc:=LOC_FLAGS;
  1115. p^.location.resflags:=F_NE;
  1116. if extended_new then
  1117. begin
  1118. {$ifdef test_dest_loc}
  1119. if dest_loc_known and (dest_loc_tree=p) then
  1120. mov_reg_to_dest(p,S_L,R_EAX)
  1121. else
  1122. {$endif test_dest_loc}
  1123. begin
  1124. hregister:=getregister32;
  1125. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1126. p^.location.register:=hregister;
  1127. end;
  1128. end;
  1129. end
  1130. { structed results are easy to handle.... }
  1131. else if ret_in_param(p^.resulttype) then
  1132. begin
  1133. {p^.location.loc:=LOC_MEM;
  1134. stringdispose(p^.location.reference.symbol);
  1135. p^.location.reference:=funcretref;
  1136. already done above (PM) }
  1137. end
  1138. else
  1139. begin
  1140. if (p^.resulttype^.deftype=orddef) then
  1141. begin
  1142. p^.location.loc:=LOC_REGISTER;
  1143. case porddef(p^.resulttype)^.typ of
  1144. s32bit,u32bit,bool32bit :
  1145. begin
  1146. {$ifdef test_dest_loc}
  1147. if dest_loc_known and (dest_loc_tree=p) then
  1148. mov_reg_to_dest(p,S_L,R_EAX)
  1149. else
  1150. {$endif test_dest_loc}
  1151. begin
  1152. hregister:=getregister32;
  1153. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1154. p^.location.register:=hregister;
  1155. end;
  1156. end;
  1157. uchar,u8bit,bool8bit,s8bit :
  1158. begin
  1159. {$ifdef test_dest_loc}
  1160. if dest_loc_known and (dest_loc_tree=p) then
  1161. mov_reg_to_dest(p,S_B,R_AL)
  1162. else
  1163. {$endif test_dest_loc}
  1164. begin
  1165. hregister:=getregister32;
  1166. emit_reg_reg(A_MOV,S_B,R_AL,reg32toreg8(hregister));
  1167. p^.location.register:=reg32toreg8(hregister);
  1168. end;
  1169. end;
  1170. s16bit,u16bit,bool16bit :
  1171. begin
  1172. {$ifdef test_dest_loc}
  1173. if dest_loc_known and (dest_loc_tree=p) then
  1174. mov_reg_to_dest(p,S_W,R_AX)
  1175. else
  1176. {$endif test_dest_loc}
  1177. begin
  1178. hregister:=getregister32;
  1179. emit_reg_reg(A_MOV,S_W,R_AX,reg32toreg16(hregister));
  1180. p^.location.register:=reg32toreg16(hregister);
  1181. end;
  1182. end;
  1183. else internalerror(7);
  1184. end
  1185. end
  1186. else if (p^.resulttype^.deftype=floatdef) then
  1187. case pfloatdef(p^.resulttype)^.typ of
  1188. f32bit : begin
  1189. p^.location.loc:=LOC_REGISTER;
  1190. {$ifdef test_dest_loc}
  1191. if dest_loc_known and (dest_loc_tree=p) then
  1192. mov_reg_to_dest(p,S_L,R_EAX)
  1193. else
  1194. {$endif test_dest_loc}
  1195. begin
  1196. hregister:=getregister32;
  1197. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1198. p^.location.register:=hregister;
  1199. end;
  1200. end;
  1201. else
  1202. p^.location.loc:=LOC_FPU;
  1203. end
  1204. else
  1205. begin
  1206. p^.location.loc:=LOC_REGISTER;
  1207. {$ifdef test_dest_loc}
  1208. if dest_loc_known and (dest_loc_tree=p) then
  1209. mov_reg_to_dest(p,S_L,R_EAX)
  1210. else
  1211. {$endif test_dest_loc}
  1212. begin
  1213. hregister:=getregister32;
  1214. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  1215. p^.location.register:=hregister;
  1216. end;
  1217. end;
  1218. end;
  1219. end;
  1220. { perhaps i/o check ? }
  1221. if iolabel<>nil then
  1222. begin
  1223. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  1224. { this was wrong, probably an error due to diff3
  1225. emitcall(p^.procdefinition^.mangledname);}
  1226. emitcall('IOCHECK',true);
  1227. end;
  1228. if must_pop then
  1229. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,pop_size,R_ESP)));
  1230. { restore registers }
  1231. popusedregisters(pushed);
  1232. { at last, restore instance pointer (SELF) }
  1233. if loadesi then
  1234. maybe_loadesi;
  1235. pp:=params;
  1236. while assigned(pp) do
  1237. begin
  1238. if assigned(pp^.left) then
  1239. if (pp^.left^.location.loc=LOC_REFERENCE) or
  1240. (pp^.left^.location.loc=LOC_MEM) then
  1241. ungetiftemp(pp^.left^.location.reference);
  1242. pp:=pp^.right;
  1243. end;
  1244. if inlined then
  1245. ungetpersistanttemp(inlinecode^.retoffset,4);
  1246. disposetree(params);
  1247. { from now on the result can be freed normally }
  1248. if inlined and ret_in_param(p^.resulttype) then
  1249. persistanttemptonormal(funcretref.offset);
  1250. { if return value is not used }
  1251. if (not p^.return_value_used) and (p^.resulttype<>pdef(voiddef)) then
  1252. begin
  1253. if p^.location.loc in [LOC_MEM,LOC_REFERENCE] then
  1254. { release unused temp }
  1255. ungetiftemp(p^.location.reference)
  1256. else if p^.location.loc=LOC_FPU then
  1257. { release FPU stack }
  1258. exprasmlist^.concat(new(pai386,op_none(A_FDECSTP,S_NO)));
  1259. end;
  1260. end;
  1261. { reverts the parameter list }
  1262. var nb_para : integer;
  1263. function reversparameter(p : ptree) : ptree;
  1264. var
  1265. hp1,hp2 : ptree;
  1266. begin
  1267. hp1:=nil;
  1268. nb_para := 0;
  1269. while assigned(p) do
  1270. begin
  1271. { pull out }
  1272. hp2:=p;
  1273. p:=p^.right;
  1274. inc(nb_para);
  1275. { pull in }
  1276. hp2^.right:=hp1;
  1277. hp1:=hp2;
  1278. end;
  1279. reversparameter:=hp1;
  1280. end;
  1281. {*****************************************************************************
  1282. SecondInLine
  1283. *****************************************************************************}
  1284. procedure secondinline(var p : ptree);
  1285. const
  1286. {$ifdef OLDINC}
  1287. decisize:array[in_inc_byte..in_dec_dword] of Topsize=
  1288. (S_B,S_W,S_L,S_B,S_W,S_L);
  1289. in2instr:array[in_inc_byte..in_dec_dword] of Tasmop=
  1290. (A_INC,A_INC,A_INC,A_DEC,A_DEC,A_DEC);
  1291. ad2instr:array[in_inc_byte..in_dec_dword] of Tasmop=
  1292. (A_ADD,A_ADD,A_ADD,A_SUB,A_SUB,A_SUB);
  1293. {$endif OLDINC}
  1294. { tfloattype = (f32bit,s32real,s64real,s80real,s64bit); }
  1295. float_name: array[tfloattype] of string[8]=
  1296. ('FIXED','SINGLE','REAL','EXTENDED','COMP','FIXED16');
  1297. incdecop:array[in_inc_x..in_dec_x] of tasmop=(A_INC,A_DEC);
  1298. addsubop:array[in_inc_x..in_dec_x] of tasmop=(A_ADD,A_SUB);
  1299. var
  1300. aktfile : treference;
  1301. ft : tfiletype;
  1302. opsize : topsize;
  1303. asmop : tasmop;
  1304. pushed : tpushed;
  1305. {inc/dec}
  1306. addconstant : boolean;
  1307. addvalue : longint;
  1308. procedure handlereadwrite(doread,doln : boolean);
  1309. { produces code for READ(LN) and WRITE(LN) }
  1310. procedure loadstream;
  1311. const
  1312. io:array[0..1] of string[7]=('_OUTPUT','_INPUT');
  1313. var
  1314. r : preference;
  1315. begin
  1316. new(r);
  1317. reset_reference(r^);
  1318. r^.symbol:=stringdup('U_'+upper(target_info.system_unit)+io[byte(doread)]);
  1319. concat_external(r^.symbol^,EXT_NEAR);
  1320. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,r,R_EDI)))
  1321. end;
  1322. var
  1323. node,hp : ptree;
  1324. typedtyp,
  1325. pararesult : pdef;
  1326. has_length : boolean;
  1327. dummycoll : tdefcoll;
  1328. iolabel : plabel;
  1329. npara : longint;
  1330. begin
  1331. { I/O check }
  1332. if cs_iocheck in aktswitches then
  1333. begin
  1334. getlabel(iolabel);
  1335. emitl(A_LABEL,iolabel);
  1336. end
  1337. else
  1338. iolabel:=nil;
  1339. { for write of real with the length specified }
  1340. has_length:=false;
  1341. hp:=nil;
  1342. { reserve temporary pointer to data variable }
  1343. aktfile.symbol:=nil;
  1344. gettempofsizereference(4,aktfile);
  1345. { first state text data }
  1346. ft:=ft_text;
  1347. { and state a parameter ? }
  1348. if p^.left=nil then
  1349. begin
  1350. { the following instructions are for "writeln;" }
  1351. loadstream;
  1352. { save @aktfile in temporary variable }
  1353. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  1354. end
  1355. else
  1356. begin
  1357. { revers paramters }
  1358. node:=reversparameter(p^.left);
  1359. p^.left := node;
  1360. npara := nb_para;
  1361. { calculate data variable }
  1362. { is first parameter a file type ? }
  1363. if node^.left^.resulttype^.deftype=filedef then
  1364. begin
  1365. ft:=pfiledef(node^.left^.resulttype)^.filetype;
  1366. if ft=ft_typed then
  1367. typedtyp:=pfiledef(node^.left^.resulttype)^.typed_as;
  1368. secondpass(node^.left);
  1369. if codegenerror then
  1370. exit;
  1371. { save reference in temporary variables }
  1372. if node^.left^.location.loc<>LOC_REFERENCE then
  1373. begin
  1374. Message(cg_e_illegal_expression);
  1375. exit;
  1376. end;
  1377. exprasmlist^.concat(new(pai386,op_ref_reg(A_LEA,S_L,newreference(node^.left^.location.reference),R_EDI)));
  1378. { skip to the next parameter }
  1379. node:=node^.right;
  1380. end
  1381. else
  1382. begin
  1383. { load stdin/stdout stream }
  1384. loadstream;
  1385. end;
  1386. { save @aktfile in temporary variable }
  1387. exprasmlist^.concat(new(pai386,op_reg_ref(A_MOV,S_L,R_EDI,newreference(aktfile))));
  1388. if doread then
  1389. { parameter by READ gives call by reference }
  1390. dummycoll.paratyp:=vs_var
  1391. { an WRITE Call by "Const" }
  1392. else
  1393. dummycoll.paratyp:=vs_const;
  1394. { because of secondcallparan, which otherwise attaches }
  1395. if ft=ft_typed then
  1396. { this is to avoid copy of simple const parameters }
  1397. dummycoll.data:=new(pformaldef,init)
  1398. else
  1399. { I think, this isn't a good solution (FK) }
  1400. dummycoll.data:=nil;
  1401. while assigned(node) do
  1402. begin
  1403. pushusedregisters(pushed,$ff);
  1404. hp:=node;
  1405. node:=node^.right;
  1406. hp^.right:=nil;
  1407. if hp^.is_colon_para then
  1408. Message(parser_e_illegal_colon_qualifier);
  1409. if ft=ft_typed then
  1410. never_copy_const_param:=true;
  1411. secondcallparan(hp,@dummycoll,false,false,0);
  1412. if ft=ft_typed then
  1413. never_copy_const_param:=false;
  1414. hp^.right:=node;
  1415. if codegenerror then
  1416. exit;
  1417. emit_push_mem(aktfile);
  1418. if (ft=ft_typed) then
  1419. begin
  1420. { OK let's try this }
  1421. { first we must only allow the right type }
  1422. { we have to call blockread or blockwrite }
  1423. { but the real problem is that }
  1424. { reset and rewrite should have set }
  1425. { the type size }
  1426. { as recordsize for that file !!!! }
  1427. { how can we make that }
  1428. { I think that is only possible by adding }
  1429. { reset and rewrite to the inline list a call }
  1430. { allways read only one record by element }
  1431. push_int(typedtyp^.size);
  1432. if doread then
  1433. emitcall('TYPED_READ',true)
  1434. else
  1435. emitcall('TYPED_WRITE',true);
  1436. end
  1437. else
  1438. begin
  1439. { save current position }
  1440. pararesult:=hp^.left^.resulttype;
  1441. { handle possible field width }
  1442. { of course only for write(ln) }
  1443. if not doread then
  1444. begin
  1445. { handle total width parameter }
  1446. if assigned(node) and node^.is_colon_para then
  1447. begin
  1448. hp:=node;
  1449. node:=node^.right;
  1450. hp^.right:=nil;
  1451. secondcallparan(hp,@dummycoll,false,false,0);
  1452. hp^.right:=node;
  1453. if codegenerror then
  1454. exit;
  1455. has_length:=true;
  1456. end
  1457. else
  1458. if pararesult^.deftype<>floatdef then
  1459. push_int(0)
  1460. else
  1461. push_int(-32767);
  1462. { a second colon para for a float ? }
  1463. if assigned(node) and node^.is_colon_para then
  1464. begin
  1465. hp:=node;
  1466. node:=node^.right;
  1467. hp^.right:=nil;
  1468. secondcallparan(hp,@dummycoll,false,false,0);
  1469. hp^.right:=node;
  1470. if pararesult^.deftype<>floatdef then
  1471. Message(parser_e_illegal_colon_qualifier);
  1472. if codegenerror then
  1473. exit;
  1474. end
  1475. else
  1476. begin
  1477. if pararesult^.deftype=floatdef then
  1478. push_int(-1);
  1479. end
  1480. end;
  1481. case pararesult^.deftype of
  1482. stringdef : begin
  1483. if doread then
  1484. begin
  1485. { push maximum string length }
  1486. push_int(pstringdef(pararesult)^.len);
  1487. case pstringdef(pararesult)^.string_typ of
  1488. shortstring: emitcall ('READ_TEXT_STRING',true);
  1489. ansistring : emitcall ('READ_TEXT_ANSISTRING',true);
  1490. longstring : emitcall ('READ_TEXT_LONGSTRING',true);
  1491. widestring : emitcall ('READ_TEXT_ANSISTRING',true);
  1492. end
  1493. end
  1494. else
  1495. Case pstringdef(Pararesult)^.string_typ of
  1496. shortstring: emitcall ('WRITE_TEXT_STRING',true);
  1497. ansistring : emitcall ('WRITE_TEXT_ANSISTRING',true);
  1498. longstring : emitcall ('WRITE_TEXT_LONGSTRING',true);
  1499. widestring : emitcall ('WRITE_TEXT_ANSISTRING',true);
  1500. end;
  1501. end;
  1502. pointerdef : begin
  1503. if is_equal(ppointerdef(pararesult)^.definition,cchardef) then
  1504. begin
  1505. if doread then
  1506. emitcall('READ_TEXT_PCHAR_AS_POINTER',true)
  1507. else
  1508. emitcall('WRITE_TEXT_PCHAR_AS_POINTER',true);
  1509. end
  1510. else
  1511. Message(parser_e_illegal_parameter_list);
  1512. end;
  1513. arraydef : begin
  1514. if (parraydef(pararesult)^.lowrange=0) and
  1515. is_equal(parraydef(pararesult)^.definition,cchardef) then
  1516. begin
  1517. if doread then
  1518. emitcall('READ_TEXT_PCHAR_AS_ARRAY',true)
  1519. else
  1520. emitcall('WRITE_TEXT_PCHAR_AS_ARRAY',true);
  1521. end
  1522. else
  1523. Message(parser_e_illegal_parameter_list);
  1524. end;
  1525. floatdef : begin
  1526. if doread then
  1527. emitcall('READ_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true)
  1528. else
  1529. emitcall('WRITE_TEXT_'+float_name[pfloatdef(pararesult)^.typ],true);
  1530. end;
  1531. orddef : begin
  1532. case porddef(pararesult)^.typ of
  1533. u8bit : if doread then
  1534. emitcall('READ_TEXT_BYTE',true);
  1535. s8bit : if doread then
  1536. emitcall('READ_TEXT_SHORTINT',true);
  1537. u16bit : if doread then
  1538. emitcall('READ_TEXT_WORD',true);
  1539. s16bit : if doread then
  1540. emitcall('READ_TEXT_INTEGER',true);
  1541. s32bit : if doread then
  1542. emitcall('READ_TEXT_LONGINT',true)
  1543. else
  1544. emitcall('WRITE_TEXT_LONGINT',true);
  1545. u32bit : if doread then
  1546. emitcall('READ_TEXT_CARDINAL',true)
  1547. else
  1548. emitcall('WRITE_TEXT_CARDINAL',true);
  1549. uchar : if doread then
  1550. emitcall('READ_TEXT_CHAR',true)
  1551. else
  1552. emitcall('WRITE_TEXT_CHAR',true);
  1553. bool8bit,
  1554. bool16bit,
  1555. bool32bit : if doread then
  1556. { emitcall('READ_TEXT_BOOLEAN',true) }
  1557. Message(parser_e_illegal_parameter_list)
  1558. else
  1559. emitcall('WRITE_TEXT_BOOLEAN',true);
  1560. else
  1561. Message(parser_e_illegal_parameter_list);
  1562. end;
  1563. end;
  1564. else
  1565. Message(parser_e_illegal_parameter_list);
  1566. end;
  1567. end;
  1568. { load ESI in methods again }
  1569. popusedregisters(pushed);
  1570. maybe_loadesi;
  1571. end;
  1572. end;
  1573. { Insert end of writing for textfiles }
  1574. if ft=ft_text then
  1575. begin
  1576. pushusedregisters(pushed,$ff);
  1577. emit_push_mem(aktfile);
  1578. if doread then
  1579. begin
  1580. if doln then
  1581. emitcall('READLN_END',true)
  1582. else
  1583. emitcall('READ_END',true);
  1584. end
  1585. else
  1586. begin
  1587. if doln then
  1588. emitcall('WRITELN_END',true)
  1589. else
  1590. emitcall('WRITE_END',true);
  1591. end;
  1592. popusedregisters(pushed);
  1593. maybe_loadesi;
  1594. end;
  1595. { Insert IOCheck if set }
  1596. if assigned(iolabel) then
  1597. begin
  1598. { registers are saved in the procedure }
  1599. exprasmlist^.concat(new(pai386,op_csymbol(A_PUSH,S_L,newcsymbol(lab2str(iolabel),0))));
  1600. emitcall('IOCHECK',true);
  1601. end;
  1602. { Freeup all used temps }
  1603. ungetiftemp(aktfile);
  1604. if assigned(p^.left) then
  1605. begin
  1606. p^.left:=reversparameter(p^.left);
  1607. if npara<>nb_para then
  1608. Message(cg_f_internal_error_in_secondinline);
  1609. hp:=p^.left;
  1610. while assigned(hp) do
  1611. begin
  1612. if assigned(hp^.left) then
  1613. if (hp^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  1614. ungetiftemp(hp^.left^.location.reference);
  1615. hp:=hp^.right;
  1616. end;
  1617. end;
  1618. end;
  1619. procedure handle_str;
  1620. var
  1621. hp,node : ptree;
  1622. dummycoll : tdefcoll;
  1623. is_real,has_length : boolean;
  1624. begin
  1625. pushusedregisters(pushed,$ff);
  1626. node:=p^.left;
  1627. is_real:=false;
  1628. has_length:=false;
  1629. while assigned(node^.right) do node:=node^.right;
  1630. { if a real parameter somewhere then call REALSTR }
  1631. if (node^.left^.resulttype^.deftype=floatdef) then
  1632. is_real:=true;
  1633. node:=p^.left;
  1634. { we have at least two args }
  1635. { with at max 2 colon_para in between }
  1636. { first arg longint or float }
  1637. hp:=node;
  1638. node:=node^.right;
  1639. hp^.right:=nil;
  1640. dummycoll.data:=hp^.resulttype;
  1641. { string arg }
  1642. dummycoll.paratyp:=vs_var;
  1643. secondcallparan(hp,@dummycoll,false
  1644. ,false,0
  1645. );
  1646. if codegenerror then
  1647. exit;
  1648. dummycoll.paratyp:=vs_const;
  1649. { second arg }
  1650. hp:=node;
  1651. node:=node^.right;
  1652. hp^.right:=nil;
  1653. { frac para }
  1654. if hp^.is_colon_para and assigned(node) and
  1655. node^.is_colon_para then
  1656. begin
  1657. dummycoll.data:=hp^.resulttype;
  1658. secondcallparan(hp,@dummycoll,false
  1659. ,false,0
  1660. );
  1661. if codegenerror then
  1662. exit;
  1663. hp:=node;
  1664. node:=node^.right;
  1665. hp^.right:=nil;
  1666. has_length:=true;
  1667. end
  1668. else
  1669. if is_real then
  1670. push_int(-1);
  1671. { third arg, length only if is_real }
  1672. if hp^.is_colon_para then
  1673. begin
  1674. dummycoll.data:=hp^.resulttype;
  1675. secondcallparan(hp,@dummycoll,false
  1676. ,false,0
  1677. );
  1678. if codegenerror then
  1679. exit;
  1680. hp:=node;
  1681. node:=node^.right;
  1682. hp^.right:=nil;
  1683. end
  1684. else
  1685. if is_real then
  1686. push_int(-32767)
  1687. else
  1688. push_int(-1);
  1689. { last arg longint or real }
  1690. secondcallparan(hp,@dummycoll,false
  1691. ,false,0
  1692. );
  1693. if codegenerror then
  1694. exit;
  1695. if is_real then
  1696. emitcall('STR_'+float_name[pfloatdef(hp^.resulttype)^.typ],true)
  1697. else if porddef(hp^.resulttype)^.typ=u32bit then
  1698. emitcall('STR_CARDINAL',true)
  1699. else
  1700. emitcall('STR_LONGINT',true);
  1701. popusedregisters(pushed);
  1702. end;
  1703. var
  1704. r : preference;
  1705. l : longint;
  1706. ispushed : boolean;
  1707. hregister : tregister;
  1708. begin
  1709. case p^.inlinenumber of
  1710. in_lo_word,
  1711. in_hi_word :
  1712. begin
  1713. secondpass(p^.left);
  1714. p^.location.loc:=LOC_REGISTER;
  1715. if p^.left^.location.loc<>LOC_REGISTER then
  1716. begin
  1717. if p^.left^.location.loc=LOC_CREGISTER then
  1718. begin
  1719. p^.location.register:=reg32toreg16(getregister32);
  1720. emit_reg_reg(A_MOV,S_W,p^.left^.location.register,
  1721. p^.location.register);
  1722. end
  1723. else
  1724. begin
  1725. del_reference(p^.left^.location.reference);
  1726. p^.location.register:=reg32toreg16(getregister32);
  1727. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_W,newreference(p^.left^.location.reference),
  1728. p^.location.register)));
  1729. end;
  1730. end
  1731. else p^.location.register:=p^.left^.location.register;
  1732. if p^.inlinenumber=in_hi_word then
  1733. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_W,8,p^.location.register)));
  1734. p^.location.register:=reg16toreg8(p^.location.register);
  1735. end;
  1736. in_high_x :
  1737. begin
  1738. if is_open_array(p^.left^.resulttype) then
  1739. begin
  1740. secondpass(p^.left);
  1741. del_reference(p^.left^.location.reference);
  1742. p^.location.register:=getregister32;
  1743. new(r);
  1744. reset_reference(r^);
  1745. r^.base:=highframepointer;
  1746. r^.offset:=highoffset+4;
  1747. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1748. r,p^.location.register)));
  1749. end
  1750. end;
  1751. in_sizeof_x,
  1752. in_typeof_x :
  1753. begin
  1754. { for both cases load vmt }
  1755. if p^.left^.treetype=typen then
  1756. begin
  1757. p^.location.register:=getregister32;
  1758. exprasmlist^.concat(new(pai386,op_csymbol_reg(A_MOV,
  1759. S_L,newcsymbol(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,0),
  1760. p^.location.register)));
  1761. end
  1762. else
  1763. begin
  1764. secondpass(p^.left);
  1765. del_reference(p^.left^.location.reference);
  1766. p^.location.loc:=LOC_REGISTER;
  1767. p^.location.register:=getregister32;
  1768. { load VMT pointer }
  1769. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1770. newreference(p^.left^.location.reference),
  1771. p^.location.register)));
  1772. end;
  1773. { in sizeof load size }
  1774. if p^.inlinenumber=in_sizeof_x then
  1775. begin
  1776. new(r);
  1777. reset_reference(r^);
  1778. r^.base:=p^.location.register;
  1779. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,r,
  1780. p^.location.register)));
  1781. end;
  1782. end;
  1783. in_lo_long,
  1784. in_hi_long :
  1785. begin
  1786. secondpass(p^.left);
  1787. p^.location.loc:=LOC_REGISTER;
  1788. if p^.left^.location.loc<>LOC_REGISTER then
  1789. begin
  1790. if p^.left^.location.loc=LOC_CREGISTER then
  1791. begin
  1792. p^.location.register:=getregister32;
  1793. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  1794. p^.location.register);
  1795. end
  1796. else
  1797. begin
  1798. del_reference(p^.left^.location.reference);
  1799. p^.location.register:=getregister32;
  1800. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  1801. p^.location.register)));
  1802. end;
  1803. end
  1804. else p^.location.register:=p^.left^.location.register;
  1805. if p^.inlinenumber=in_hi_long then
  1806. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,16,p^.location.register)));
  1807. p^.location.register:=reg32toreg16(p^.location.register);
  1808. end;
  1809. {***CHARBUG}
  1810. {We can now comment them out, as they are handled as typecast.
  1811. Saves an incredible amount of 8 bytes code.
  1812. I'am not lucky about this, because it's _not_ a type cast (FK) }
  1813. { in_ord_char,
  1814. in_chr_byte,}
  1815. {***}
  1816. in_length_string :
  1817. begin
  1818. secondpass(p^.left);
  1819. set_location(p^.location,p^.left^.location);
  1820. { length in ansi strings is at offset -8 }
  1821. {$ifdef UseAnsiString}
  1822. if is_ansistring(p^.left^.resulttype) then
  1823. dec(p^.location.reference.offset,8);
  1824. {$endif UseAnsiString}
  1825. end;
  1826. in_pred_x,
  1827. in_succ_x:
  1828. begin
  1829. secondpass(p^.left);
  1830. if p^.inlinenumber=in_pred_x then
  1831. asmop:=A_DEC
  1832. else
  1833. asmop:=A_INC;
  1834. case p^.resulttype^.size of
  1835. 4 : opsize:=S_L;
  1836. 2 : opsize:=S_W;
  1837. 1 : opsize:=S_B;
  1838. else
  1839. internalerror(10080);
  1840. end;
  1841. p^.location.loc:=LOC_REGISTER;
  1842. if p^.left^.location.loc<>LOC_REGISTER then
  1843. begin
  1844. p^.location.register:=getregister32;
  1845. if (p^.resulttype^.size=2) then
  1846. p^.location.register:=reg32toreg16(p^.location.register);
  1847. if (p^.resulttype^.size=1) then
  1848. p^.location.register:=reg32toreg8(p^.location.register);
  1849. if p^.left^.location.loc=LOC_CREGISTER then
  1850. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,
  1851. p^.location.register)
  1852. else
  1853. if p^.left^.location.loc=LOC_FLAGS then
  1854. exprasmlist^.concat(new(pai386,op_reg(flag_2_set[p^.left^.location.resflags],S_B,
  1855. p^.location.register)))
  1856. else
  1857. begin
  1858. del_reference(p^.left^.location.reference);
  1859. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,newreference(p^.left^.location.reference),
  1860. p^.location.register)));
  1861. end;
  1862. end
  1863. else p^.location.register:=p^.left^.location.register;
  1864. exprasmlist^.concat(new(pai386,op_reg(asmop,opsize,
  1865. p^.location.register)))
  1866. { here we should insert bounds check ? }
  1867. { and direct call to bounds will crash the program }
  1868. { if we are at the limit }
  1869. { we could also simply say that pred(first)=first and succ(last)=last }
  1870. { could this be usefull I don't think so (PM)
  1871. emitoverflowcheck;}
  1872. end;
  1873. in_dec_x,
  1874. in_inc_x :
  1875. begin
  1876. { set defaults }
  1877. addvalue:=1;
  1878. addconstant:=true;
  1879. { load first parameter, must be a reference }
  1880. secondpass(p^.left^.left);
  1881. case p^.left^.left^.resulttype^.deftype of
  1882. orddef,
  1883. enumdef : begin
  1884. case p^.left^.left^.resulttype^.size of
  1885. 1 : opsize:=S_B;
  1886. 2 : opsize:=S_W;
  1887. 4 : opsize:=S_L;
  1888. end;
  1889. end;
  1890. pointerdef : begin
  1891. opsize:=S_L;
  1892. addvalue:=ppointerdef(p^.left^.left^.resulttype)^.definition^.savesize;
  1893. end;
  1894. else
  1895. internalerror(10081);
  1896. end;
  1897. { second argument specified?, must be a s32bit in register }
  1898. if assigned(p^.left^.right) then
  1899. begin
  1900. secondpass(p^.left^.right^.left);
  1901. { when constant, just multiply the addvalue }
  1902. if is_constintnode(p^.left^.right^.left) then
  1903. addvalue:=addvalue*get_ordinal_value(p^.left^.right^.left)
  1904. else
  1905. begin
  1906. case p^.left^.right^.left^.location.loc of
  1907. LOC_REGISTER,
  1908. LOC_CREGISTER : hregister:=p^.left^.right^.left^.location.register;
  1909. LOC_MEM,
  1910. LOC_REFERENCE : begin
  1911. hregister:=getregister32;
  1912. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  1913. newreference(p^.left^.right^.left^.location.reference),hregister)));
  1914. end;
  1915. else
  1916. internalerror(10082);
  1917. end;
  1918. { insert multiply with addvalue if its >1 }
  1919. if addvalue>1 then
  1920. exprasmlist^.concat(new(pai386,op_const_reg(A_IMUL,opsize,
  1921. addvalue,hregister)));
  1922. addconstant:=false;
  1923. end;
  1924. end;
  1925. { write the add instruction }
  1926. if addconstant then
  1927. begin
  1928. if (addvalue=1) and not(cs_check_overflow in aktswitches) then
  1929. exprasmlist^.concat(new(pai386,op_ref(incdecop[p^.inlinenumber],opsize,
  1930. newreference(p^.left^.left^.location.reference))))
  1931. else
  1932. exprasmlist^.concat(new(pai386,op_const_ref(addsubop[p^.inlinenumber],opsize,
  1933. addvalue,newreference(p^.left^.left^.location.reference))));
  1934. end
  1935. else
  1936. begin
  1937. exprasmlist^.concat(new(pai386,op_reg_ref(addsubop[p^.inlinenumber],opsize,
  1938. hregister,newreference(p^.left^.left^.location.reference))));
  1939. ungetregister32(hregister);
  1940. end;
  1941. emitoverflowcheck(p^.left^.left);
  1942. end;
  1943. {$ifdef OLDINC}
  1944. in_inc_byte..in_dec_dword:
  1945. begin
  1946. secondpass(p^.left);
  1947. if cs_check_overflow in aktswitches then
  1948. begin
  1949. { SINCE THE CARRY FLAG IS NEVER SET BY DEC/INC, we must use }
  1950. { ADD and SUB to check for overflow for unsigned operations. }
  1951. exprasmlist^.concat(new(pai386,op_const_ref(ad2instr[p^.inlinenumber],
  1952. in2size[p^.inlinenumber],1,newreference(p^.left^.location.reference))));
  1953. emitoverflowcheck(p^.left);
  1954. end
  1955. else
  1956. exprasmlist^.concat(new(pai386,op_ref(in2instr[p^.inlinenumber],
  1957. in2size[p^.inlinenumber],newreference(p^.left^.location.reference))));
  1958. end;
  1959. {$endif OLDINC}
  1960. in_assigned_x :
  1961. begin
  1962. secondpass(p^.left^.left);
  1963. p^.location.loc:=LOC_FLAGS;
  1964. if (p^.left^.left^.location.loc=LOC_REGISTER) or
  1965. (p^.left^.left^.location.loc=LOC_CREGISTER) then
  1966. begin
  1967. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,
  1968. p^.left^.left^.location.register,
  1969. p^.left^.left^.location.register)));
  1970. ungetregister32(p^.left^.left^.location.register);
  1971. end
  1972. else
  1973. begin
  1974. exprasmlist^.concat(new(pai386,op_const_ref(A_CMP,S_L,0,
  1975. newreference(p^.left^.left^.location.reference))));
  1976. del_reference(p^.left^.left^.location.reference);
  1977. end;
  1978. p^.location.resflags:=F_NE;
  1979. end;
  1980. in_reset_typedfile,in_rewrite_typedfile :
  1981. begin
  1982. pushusedregisters(pushed,$ff);
  1983. exprasmlist^.concat(new(pai386,op_const(A_PUSH,S_L,pfiledef(p^.left^.resulttype)^.typed_as^.size)));
  1984. secondload(p^.left);
  1985. emitpushreferenceaddr(exprasmlist,p^.left^.location.reference);
  1986. if p^.inlinenumber=in_reset_typedfile then
  1987. emitcall('RESET_TYPED',true)
  1988. else
  1989. emitcall('REWRITE_TYPED',true);
  1990. popusedregisters(pushed);
  1991. end;
  1992. in_write_x :
  1993. handlereadwrite(false,false);
  1994. in_writeln_x :
  1995. handlereadwrite(false,true);
  1996. in_read_x :
  1997. handlereadwrite(true,false);
  1998. in_readln_x :
  1999. handlereadwrite(true,true);
  2000. in_str_x_string :
  2001. begin
  2002. handle_str;
  2003. maybe_loadesi;
  2004. end;
  2005. in_include_x_y,
  2006. in_exclude_x_y:
  2007. begin
  2008. secondpass(p^.left^.left);
  2009. if p^.left^.right^.left^.treetype=ordconstn then
  2010. begin
  2011. { calculate bit position }
  2012. l:=1 shl (p^.left^.right^.left^.value mod 32);
  2013. { determine operator }
  2014. if p^.inlinenumber=in_include_x_y then
  2015. asmop:=A_OR
  2016. else
  2017. begin
  2018. asmop:=A_AND;
  2019. l:=not(l);
  2020. end;
  2021. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  2022. begin
  2023. inc(p^.left^.left^.location.reference.offset,(p^.left^.right^.left^.value div 32)*4);
  2024. exprasmlist^.concat(new(pai386,op_const_ref(asmop,S_L,
  2025. l,newreference(p^.left^.left^.location.reference))));
  2026. del_reference(p^.left^.left^.location.reference);
  2027. end
  2028. else
  2029. { LOC_CREGISTER }
  2030. exprasmlist^.concat(new(pai386,op_const_reg(asmop,S_L,
  2031. l,p^.left^.left^.location.register)));
  2032. end
  2033. else
  2034. begin
  2035. { generate code for the element to set }
  2036. ispushed:=maybe_push(p^.left^.right^.left^.registers32,p^.left^.left);
  2037. secondpass(p^.left^.right^.left);
  2038. if ispushed then
  2039. restore(p^.left^.left);
  2040. { determine asm operator }
  2041. if p^.inlinenumber=in_include_x_y then
  2042. asmop:=A_BTS
  2043. else
  2044. asmop:=A_BTR;
  2045. if psetdef(p^.left^.resulttype)^.settype=smallset then
  2046. begin
  2047. if p^.left^.right^.left^.location.loc in
  2048. [LOC_CREGISTER,LOC_REGISTER] then
  2049. hregister:=p^.left^.right^.left^.location.register
  2050. else
  2051. begin
  2052. hregister:=R_EDI;
  2053. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  2054. newreference(p^.left^.right^.left^.location.reference),
  2055. R_EDI)));
  2056. end;
  2057. if (p^.left^.left^.location.loc=LOC_REFERENCE) then
  2058. exprasmlist^.concat(new(pai386,op_reg_ref(asmop,S_L,R_EDI,
  2059. newreference(p^.left^.right^.left^.location.reference))))
  2060. else
  2061. exprasmlist^.concat(new(pai386,op_reg_reg(asmop,S_L,R_EDI,
  2062. p^.left^.right^.left^.location.register)));
  2063. end
  2064. else
  2065. begin
  2066. end;
  2067. end;
  2068. end;
  2069. else internalerror(9);
  2070. end;
  2071. end;
  2072. {*****************************************************************************
  2073. SecondProcInlineN
  2074. *****************************************************************************}
  2075. { implementation not complete yet }
  2076. var
  2077. addr_correction : longint;
  2078. procedure correct_address(p : psym);{$ifndef FPC}far;{$endif}
  2079. begin
  2080. if p^.typ=varsym then
  2081. begin
  2082. inc(pvarsym(p)^.address,addr_correction);
  2083. {$ifdef extdebug}
  2084. Comment(V_debug,pvarsym(p)^.name+' is at offset -'
  2085. +tostr(pvarsym(p)^.address));
  2086. exprasmlist^.concat(new(pai_asm_comment,init(
  2087. strpnew(pvarsym(p)^.name+' is at offset -'
  2088. +tostr(pvarsym(p)^.address)))));
  2089. {$endif extdebug}
  2090. end;
  2091. end;
  2092. procedure secondprocinline(var p : ptree);
  2093. var st : psymtable;
  2094. oldprocsym : pprocsym;
  2095. para_size : longint;
  2096. oldprocinfo : tprocinfo;
  2097. { just dummies for genentrycode }
  2098. nostackframe,make_global : boolean;
  2099. proc_names : tstringcontainer;
  2100. inlineentrycode,inlineexitcode : paasmoutput;
  2101. oldexitlabel,oldexit2label,oldquickexitlabel:Plabel;
  2102. begin
  2103. oldexitlabel:=aktexitlabel;
  2104. oldexit2label:=aktexit2label;
  2105. oldquickexitlabel:=quickexitlabel;
  2106. getlabel(aktexitlabel);
  2107. getlabel(aktexit2label);
  2108. oldprocsym:=aktprocsym;
  2109. oldprocinfo:=procinfo;
  2110. { set the return value }
  2111. procinfo.retdef:=p^.inlineprocdef^.retdef;
  2112. procinfo.retoffset:=p^.retoffset;
  2113. { arg space has been filled by the parent secondcall }
  2114. st:=p^.inlineprocdef^.localst;
  2115. { set it to the same lexical level }
  2116. st^.symtablelevel:=
  2117. oldprocsym^.definition^.localst^.symtablelevel;
  2118. if st^.datasize>0 then
  2119. st^.call_offset:=gettempofsizepersistant(st^.datasize);
  2120. {$ifdef extdebug}
  2121. Comment(V_debug,'local symtable is at offset '
  2122. +tostr(st^.call_offset));
  2123. exprasmlist^.concat(new(pai_asm_comment,init(
  2124. strpnew('local symtable is at offset '
  2125. +tostr(st^.call_offset)))));
  2126. {$endif extdebug}
  2127. addr_correction:=-st^.call_offset-st^.datasize;
  2128. st^.foreach(correct_address);
  2129. {$ifdef extdebug}
  2130. exprasmlist^.concat(new(pai_asm_comment,init('Start of inlined proc')));
  2131. {$endif extdebug}
  2132. { takes care of local data initialization }
  2133. inlineentrycode:=new(paasmoutput,init);
  2134. inlineexitcode:=new(paasmoutput,init);
  2135. proc_names.init;
  2136. para_size:=p^.para_size;
  2137. make_global:=false; { to avoid warning }
  2138. genentrycode(inlineentrycode,proc_names,make_global,0,para_size,nostackframe,true);
  2139. exprasmlist^.concatlist(inlineentrycode);
  2140. secondpass(p^.left);
  2141. genexitcode(inlineexitcode,0,false,true);
  2142. exprasmlist^.concatlist(inlineexitcode);
  2143. {$ifdef extdebug}
  2144. exprasmlist^.concat(new(pai_asm_comment,init('End of inlined proc')));
  2145. {$endif extdebug}
  2146. {we can free the local data now }
  2147. if st^.datasize>0 then
  2148. ungetpersistanttemp(st^.call_offset,st^.datasize);
  2149. { set the real address again }
  2150. addr_correction:=-addr_correction;
  2151. st^.foreach(correct_address);
  2152. aktprocsym:=oldprocsym;
  2153. aktexitlabel:=oldexitlabel;
  2154. aktexit2label:=oldexit2label;
  2155. quickexitlabel:=oldquickexitlabel;
  2156. procinfo:=oldprocinfo;
  2157. end;
  2158. end.
  2159. {
  2160. $Log$
  2161. Revision 1.9 1998-07-07 17:40:37 peter
  2162. * packrecords 4 works
  2163. * word aligning of parameters
  2164. Revision 1.8 1998/07/06 15:51:15 michael
  2165. Added length checking for string reading
  2166. Revision 1.7 1998/07/06 14:19:51 michael
  2167. + Added calls for reading/writing ansistrings
  2168. Revision 1.6 1998/07/01 15:28:48 peter
  2169. + better writeln/readln handling, now 100% like tp7
  2170. Revision 1.5 1998/06/25 14:04:17 peter
  2171. + internal inc/dec
  2172. Revision 1.4 1998/06/25 08:48:06 florian
  2173. * first version of rtti support
  2174. Revision 1.3 1998/06/09 16:01:33 pierre
  2175. + added procedure directive parsing for procvars
  2176. (accepted are popstack cdecl and pascal)
  2177. + added C vars with the following syntax
  2178. var C calias 'true_c_name';(can be followed by external)
  2179. reason is that you must add the Cprefix
  2180. which is target dependent
  2181. Revision 1.2 1998/06/08 13:13:29 pierre
  2182. + temporary variables now in temp_gen.pas unit
  2183. because it is processor independent
  2184. * mppc68k.bat modified to undefine i386 and support_mmx
  2185. (which are defaults for i386)
  2186. Revision 1.1 1998/06/05 17:44:10 peter
  2187. * splitted cgi386
  2188. }