cg386cal.pas 103 KB

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