ninl.pas 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Type checking and register allocation for 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 ninl;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,htypechk,cpuinfo;
  23. {$i compinnr.inc}
  24. type
  25. tinlinenode = class(tunarynode)
  26. inlinenumber : byte;
  27. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  28. function getcopy : tnode;override;
  29. function pass_1 : tnode;override;
  30. function det_resulttype:tnode;override;
  31. function docompare(p: tnode): boolean; override;
  32. private
  33. function handle_str: tnode;
  34. function handle_reset_rewrite_typed: tnode;
  35. function handle_read_write: tnode;
  36. function handle_val: tnode;
  37. end;
  38. tinlinenodeclass = class of tinlinenode;
  39. var
  40. cinlinenode : tinlinenodeclass;
  41. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  42. implementation
  43. uses
  44. verbose,globals,systems,
  45. globtype, cutils,
  46. symbase,symconst,symtype,symdef,symsym,symtable,types,
  47. pass_1,
  48. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,
  49. cpubase,tgobj,cgbase
  50. ;
  51. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  52. begin
  53. geninlinenode:=cinlinenode.create(number,is_const,l);
  54. end;
  55. {*****************************************************************************
  56. TINLINENODE
  57. *****************************************************************************}
  58. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  59. begin
  60. inherited create(inlinen,l);
  61. if is_const then
  62. include(flags,nf_inlineconst);
  63. inlinenumber:=number;
  64. end;
  65. function tinlinenode.getcopy : tnode;
  66. var
  67. n : tinlinenode;
  68. begin
  69. n:=tinlinenode(inherited getcopy);
  70. n.inlinenumber:=inlinenumber;
  71. result:=n;
  72. end;
  73. function tinlinenode.handle_str : tnode;
  74. var
  75. lenpara,
  76. fracpara,
  77. newparas,
  78. dest,
  79. source : tcallparanode;
  80. newnode : tnode;
  81. procname: string;
  82. is_real : boolean;
  83. begin
  84. result := cerrornode.create;
  85. { make sure we got at least two parameters (if we got only one, }
  86. { this parameter may not be encapsulated in a callparan) }
  87. if not assigned(left) or
  88. (left.nodetype <> callparan) then
  89. begin
  90. CGMessage(parser_e_wrong_parameter_size);
  91. exit;
  92. end;
  93. { get destination string }
  94. dest := tcallparanode(left);
  95. { get source para (number) }
  96. source := dest;
  97. while assigned(source.right) do
  98. source := tcallparanode(source.right);
  99. is_real := source.resulttype.def.deftype = floatdef;
  100. if not assigned(dest) or
  101. (dest.left.resulttype.def.deftype<>stringdef) or
  102. not(is_real or
  103. (source.left.resulttype.def.deftype = orddef)) then
  104. begin
  105. { the parser will give this message already because we }
  106. { return an errornode (JM) }
  107. { CGMessagePos(fileinfo,cg_e_illegal_expression); }
  108. exit;
  109. end;
  110. { get len/frac parameters }
  111. lenpara := nil;
  112. fracpara := nil;
  113. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  114. begin
  115. lenpara := tcallparanode(dest.right);
  116. { we can let the callnode do the type checking of these parameters too, }
  117. { but then the error messages aren't as nice }
  118. if not is_integer(lenpara.resulttype.def) then
  119. begin
  120. CGMessagePos1(lenpara.fileinfo,
  121. type_e_integer_expr_expected,lenpara.resulttype.def.typename);
  122. exit;
  123. end;
  124. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  125. begin
  126. { parameters are in reverse order! }
  127. fracpara := lenpara;
  128. lenpara := tcallparanode(lenpara.right);
  129. if not is_real then
  130. begin
  131. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  132. exit
  133. end;
  134. if not is_integer(lenpara.resulttype.def) then
  135. begin
  136. CGMessagePos1(lenpara.fileinfo,
  137. type_e_integer_expr_expected,lenpara.resulttype.def.typename);
  138. exit;
  139. end;
  140. end;
  141. end;
  142. { generate the parameter list for the compilerproc }
  143. newparas := dest;
  144. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  145. if is_real then
  146. begin
  147. { insert realtype parameter }
  148. newparas.right := ccallparanode.create(cordconstnode.create(
  149. ord(tfloatdef(source.left.resulttype.def).typ),s32bittype),newparas.right);
  150. { if necessary, insert a fraction parameter }
  151. if not assigned(fracpara) then
  152. begin
  153. tcallparanode(newparas.right).right := ccallparanode.create(
  154. cordconstnode.create(-1,s32bittype),tcallparanode(newparas.right).right);
  155. fracpara := tcallparanode(tcallparanode(newparas.right).right);
  156. end;
  157. { if necessary, insert a length para }
  158. if not assigned(lenpara) then
  159. fracpara.right := ccallparanode.create(cordconstnode.create(-32767,s32bittype),
  160. fracpara.right);
  161. end
  162. else
  163. { for a normal parameter, insert a only length parameter if one is missing }
  164. if not assigned(lenpara) then
  165. newparas.right := ccallparanode.create(cordconstnode.create(-1,s32bittype),
  166. newparas.right);
  167. { remove the parameters from the original node so they won't get disposed, }
  168. { since they're reused }
  169. left := nil;
  170. { create procedure name }
  171. procname := 'fpc_' + tstringdef(dest.resulttype.def).stringtypname+'_';
  172. if is_real then
  173. procname := procname + 'float'
  174. else
  175. case torddef(source.resulttype.def).typ of
  176. u32bit:
  177. procname := procname + 'cardinal';
  178. u64bit:
  179. procname := procname + 'qword';
  180. s64bit:
  181. procname := procname + 'int64';
  182. else
  183. procname := procname + 'longint';
  184. end;
  185. { create the call node, }
  186. newnode := ccallnode.createintern(procname,newparas);
  187. { resulttypepass it }
  188. resulttypepass(newnode);
  189. { and return it (but first free the errornode we generated in the beginning) }
  190. result.free;
  191. result := newnode;
  192. end;
  193. function tinlinenode.handle_reset_rewrite_typed: tnode;
  194. begin
  195. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  196. { a typed file as argument and we don't have to check it again (JM) }
  197. { add the recsize parameter }
  198. { note: for some reason, the parameter of intern procedures with only one }
  199. { parameter is gets lifted out of its original tcallparanode (see round }
  200. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  201. left := ccallparanode.create(cordconstnode.create(
  202. tfiledef(left.resulttype.def).typedfiletype.def.size,s32bittype),
  203. ccallparanode.create(left,nil));
  204. { create the correct call }
  205. if inlinenumber=in_reset_typedfile then
  206. result := ccallnode.createintern('fpc_reset_typed',left)
  207. else
  208. result := ccallnode.createintern('fpc_rewrite_typed',left);
  209. firstpass(result);
  210. { make sure left doesn't get disposed, since we use it in the new call }
  211. left := nil;
  212. end;
  213. function tinlinenode.handle_read_write: tnode;
  214. const
  215. procnames: array[boolean,boolean] of string[11] =
  216. (('write_text_','read_text_'),('typed_write','typed_read'));
  217. var
  218. filepara,
  219. lenpara,
  220. fracpara,
  221. nextpara,
  222. para : tcallparanode;
  223. newstatement : tstatementnode;
  224. newblock : tblocknode;
  225. p1 : tnode;
  226. filetemp,
  227. temp : ttempcreatenode;
  228. procprefix,
  229. name : string[31];
  230. srsym : tvarsym;
  231. tempowner : tsymtable;
  232. restype : ^ttype;
  233. is_typed,
  234. do_read,
  235. is_real,
  236. error_para,
  237. found_error,
  238. is_ordinal : boolean;
  239. begin
  240. filepara := nil;
  241. is_typed := false;
  242. filetemp := nil;
  243. do_read := inlinenumber in [in_read_x,in_readln_x];
  244. { if we fail, we can quickly exit this way. We must generate something }
  245. { instead of the inline node, because firstpass will bomb with an }
  246. { internalerror if it encounters a read/write }
  247. result := cerrornode.create;
  248. { reverse the parameters (needed to get the colon parameters in the }
  249. { correct order when processing write(ln) }
  250. left := reverseparameters(tcallparanode(left));
  251. if assigned(left) then
  252. begin
  253. { check if we have a file parameter and if yes, what kind it is }
  254. filepara := tcallparanode(left);
  255. if (filepara.resulttype.def.deftype=filedef) then
  256. begin
  257. if (tfiledef(filepara.resulttype.def).filetyp=ft_untyped) then
  258. begin
  259. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  260. exit;
  261. end
  262. else
  263. begin
  264. if (tfiledef(filepara.resulttype.def).filetyp=ft_typed) then
  265. begin
  266. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  267. begin
  268. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  269. exit;
  270. end;
  271. is_typed := true;
  272. end
  273. end;
  274. end
  275. else
  276. filepara := nil;
  277. end;
  278. { create a blocknode in which the successive write/read statements will be }
  279. { put, since they belong together. Also create a dummy statement already to }
  280. { make inserting of additional statements easier }
  281. newstatement := cstatementnode.create(nil,cnothingnode.create);
  282. newblock := cblocknode.create(newstatement);
  283. { if we don't have a filepara, create one containing the default }
  284. if not assigned(filepara) then
  285. begin
  286. { create a loadnode for the standard input/output handle }
  287. if do_read then
  288. name := 'INPUT'
  289. else
  290. name := 'OUTPUT';
  291. { if we are compiling the system unit, the systemunit symtable is nil. }
  292. { however, if we aren't compiling the system unit, another unit could }
  293. { also have defined the INPUT or OUTPUT symbols. Therefore we need the }
  294. { separate cases (JM) }
  295. if not searchsysvar(name,srsym,tempowner) then
  296. internalerror(200108141);
  297. { create the file parameter }
  298. filepara := ccallparanode.create(cloadnode.create(srsym,tempowner),nil);
  299. end
  300. else
  301. { remove filepara from the parameter chain }
  302. begin
  303. left := filepara.right;
  304. filepara.right := nil;
  305. { the file para is a var parameter, but it must be valid already }
  306. set_varstate(filepara,true);
  307. { check if we should make a temp to store the result of a complex }
  308. { expression (better heuristics, anyone?) (JM) }
  309. if (filepara.left.nodetype <> loadn) then
  310. begin
  311. { create a temp which will hold a pointer to the file }
  312. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,true);
  313. { add it to the statements }
  314. newstatement.left := cstatementnode.create(nil,filetemp);
  315. newstatement := tstatementnode(newstatement.left);
  316. { make sure the resulttype of the temp (and as such of the }
  317. { temprefs coming after it) is set (necessary because the }
  318. { temprefs will be part of the filepara, of which we need }
  319. { the resulttype later on and temprefs can only be }
  320. { resulttypepassed if the resulttype of the temp is known) }
  321. resulttypepass(tnode(filetemp));
  322. { assign the address of the file to the temp }
  323. newstatement.left := cstatementnode.create(nil,
  324. cassignmentnode.create(ctemprefnode.create(filetemp),
  325. caddrnode.create(filepara.left)));
  326. newstatement := tstatementnode(newstatement.left);
  327. resulttypepass(newstatement.right);
  328. { create a new fileparameter as follows: file_type(temp^) }
  329. { (so that we pass the value and not the address of the temp }
  330. { to the read/write routine) }
  331. nextpara := ccallparanode.create(ctypeconvnode.create(
  332. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resulttype),nil);
  333. { make sure the type conversion is explicit, otherwise this }
  334. { typecast won't work }
  335. nextpara.left.toggleflag(nf_explizit);
  336. { replace the old file para with the new one }
  337. filepara.left := nil;
  338. filepara.free;
  339. filepara := nextpara;
  340. { the resulttype of the filepara must be set since it's }
  341. { used below }
  342. filepara.get_paratype;
  343. end;
  344. end;
  345. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  346. { if something goes wrong or at the end of the procedure }
  347. { choose the correct procedure prefix }
  348. procprefix := 'fpc_'+procnames[is_typed,do_read];
  349. { we're going to reuse the paranodes, so make sure they don't get freed }
  350. { twice }
  351. para := tcallparanode(left);
  352. left := nil;
  353. { no errors found yet... }
  354. found_error := false;
  355. if is_typed then
  356. begin
  357. { add the typesize to the filepara }
  358. filepara.right := ccallparanode.create(cordconstnode.create(
  359. tfiledef(filepara.resulttype.def).typedfiletype.def.size,s32bittype),nil);
  360. { check for "no parameters" (you need at least one extra para for typed files) }
  361. if not assigned(para) then
  362. begin
  363. CGMessage(parser_e_wrong_parameter_size);
  364. found_error := true;
  365. end;
  366. { process all parameters }
  367. while assigned(para) do
  368. begin
  369. { check if valid parameter }
  370. if para.left.nodetype=typen then
  371. begin
  372. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  373. found_error := true;
  374. end;
  375. { support writeln(procvar) }
  376. if (para.left.resulttype.def.deftype=procvardef) then
  377. begin
  378. p1:=ccallnode.create(nil,nil,nil,nil);
  379. tcallnode(p1).set_procvar(para.left);
  380. resulttypepass(p1);
  381. para.left:=p1;
  382. end;
  383. if not is_equal(para.left.resulttype.def,tfiledef(filepara.resulttype.def).typedfiletype.def) then
  384. begin
  385. CGMessagePos(para.left.fileinfo,type_e_mismatch);
  386. found_error := true;
  387. end;
  388. if assigned(para.right) and
  389. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  390. begin
  391. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  392. { skip all colon para's }
  393. nextpara := tcallparanode(tcallparanode(para.right).right);
  394. while assigned(nextpara) and
  395. (cpf_is_colon_para in nextpara.callparaflags) do
  396. nextpara := tcallparanode(nextpara.right);
  397. found_error := true;
  398. end
  399. else
  400. { get next parameter }
  401. nextpara := tcallparanode(para.right);
  402. { When we have a call, we have a problem: you can't pass the }
  403. { result of a call as a formal const parameter. Solution: }
  404. { assign the result to a temp and pass this temp as parameter }
  405. { This is not very efficient, but write(typedfile,x) is }
  406. { already slow by itself anyway (no buffering) (JM) }
  407. if (para.left.nodetype = calln) then
  408. begin
  409. { create temp for result }
  410. temp := ctempcreatenode.create(para.left.resulttype,
  411. para.left.resulttype.def.size,true);
  412. newstatement.left := cstatementnode.create(nil,temp);
  413. { assign result to temp }
  414. newstatement := tstatementnode(newstatement.left);
  415. newstatement.left := cstatementnode.create(nil,
  416. cassignmentnode.create(ctemprefnode.create(temp),
  417. para.left));
  418. newstatement := tstatementnode(newstatement.left);
  419. { replace (reused) paranode with temp }
  420. para.left := ctemprefnode.create(temp);
  421. end;
  422. { add fileparameter }
  423. para.right := filepara.getcopy;
  424. { create call statment }
  425. { since the parameters are in the correct order, we have to insert }
  426. { the statements always at the end of the current block }
  427. newstatement.left := cstatementnode.create(nil,
  428. ccallnode.createintern(procprefix,para));
  429. newstatement := tstatementnode(newstatement.left);
  430. { if we used a temp, free it }
  431. if para.left.nodetype = temprefn then
  432. begin
  433. newstatement.left := cstatementnode.create(nil,
  434. ctempdeletenode.create(temp));
  435. newstatement := tstatementnode(newstatement.left);
  436. end;
  437. { process next parameter }
  438. para := nextpara;
  439. end;
  440. { free the file parameter }
  441. filepara.free;
  442. end
  443. else
  444. { text read/write }
  445. begin
  446. while assigned(para) do
  447. begin
  448. { is this parameter faulty? }
  449. error_para := false;
  450. { is this parameter an ordinal? }
  451. is_ordinal := false;
  452. { is this parameter a real? }
  453. is_real:=false;
  454. { can't read/write types }
  455. if para.left.nodetype=typen then
  456. begin
  457. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  458. error_para := true;
  459. end;
  460. { support writeln(procvar) }
  461. if (para.left.resulttype.def.deftype=procvardef) then
  462. begin
  463. p1:=ccallnode.create(nil,nil,nil,nil);
  464. tcallnode(p1).set_procvar(para.left);
  465. resulttypepass(p1);
  466. para.left:=p1;
  467. end;
  468. case para.left.resulttype.def.deftype of
  469. stringdef :
  470. begin
  471. name := procprefix+tstringdef(para.left.resulttype.def).stringtypname;
  472. end;
  473. pointerdef :
  474. begin
  475. if not is_pchar(para.left.resulttype.def) then
  476. begin
  477. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  478. error_para := true;
  479. end
  480. else
  481. name := procprefix+'pchar_as_pointer';
  482. end;
  483. floatdef :
  484. begin
  485. is_real:=true;
  486. name := procprefix+'float';
  487. end;
  488. orddef :
  489. begin
  490. is_ordinal := true;
  491. case torddef(para.left.resulttype.def).typ of
  492. s8bit,s16bit,s32bit :
  493. name := procprefix+'sint';
  494. u8bit,u16bit,u32bit :
  495. name := procprefix+'uint';
  496. uchar :
  497. name := procprefix+'char';
  498. uwidechar :
  499. name := procprefix+'widechar';
  500. s64bit :
  501. name := procprefix+'int64';
  502. u64bit :
  503. name := procprefix+'qword';
  504. bool8bit,
  505. bool16bit,
  506. bool32bit :
  507. begin
  508. if do_read then
  509. begin
  510. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  511. error_para := true;
  512. end
  513. else
  514. name := procprefix+'boolean'
  515. end
  516. else
  517. begin
  518. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  519. error_para := true;
  520. end;
  521. end;
  522. end;
  523. arraydef :
  524. begin
  525. if is_chararray(para.left.resulttype.def) then
  526. name := procprefix+'pchar_as_array'
  527. else
  528. begin
  529. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  530. error_para := true;
  531. end
  532. end
  533. else
  534. begin
  535. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  536. error_para := true;
  537. end
  538. end;
  539. { check for length/fractional colon para's }
  540. fracpara := nil;
  541. lenpara := nil;
  542. if assigned(para.right) and
  543. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  544. begin
  545. lenpara := tcallparanode(para.right);
  546. if assigned(lenpara.right) and
  547. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  548. fracpara:=tcallparanode(lenpara.right);
  549. end;
  550. { get the next parameter now already, because we're going }
  551. { to muck around with the pointers }
  552. if assigned(fracpara) then
  553. nextpara := tcallparanode(fracpara.right)
  554. else if assigned(lenpara) then
  555. nextpara := tcallparanode(lenpara.right)
  556. else
  557. nextpara := tcallparanode(para.right);
  558. { check if a fracpara is allowed }
  559. if assigned(fracpara) and not is_real then
  560. begin
  561. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  562. error_para := true;
  563. end
  564. else if assigned(lenpara) and do_read then
  565. begin
  566. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  567. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  568. error_para := true;
  569. end;
  570. { adjust found_error }
  571. found_error := found_error or error_para;
  572. if not error_para then
  573. begin
  574. { create dummy frac/len para's if necessary }
  575. if not do_read then
  576. begin
  577. { difference in default value for floats and the rest :( }
  578. if not is_real then
  579. begin
  580. if not assigned(lenpara) then
  581. lenpara := ccallparanode.create(cordconstnode.create(0,s32bittype),nil)
  582. else
  583. { make sure we don't pass the successive }
  584. { parameters too. We also already have a }
  585. { reference to the next parameter in }
  586. { nextpara }
  587. lenpara.right := nil;
  588. end
  589. else
  590. begin
  591. if not assigned(lenpara) then
  592. lenpara := ccallparanode.create(
  593. cordconstnode.create(-32767,s32bittype),nil);
  594. { also create a default fracpara if necessary }
  595. if not assigned(fracpara) then
  596. fracpara := ccallparanode.create(
  597. cordconstnode.create(-1,s32bittype),nil);
  598. { add it to the lenpara }
  599. lenpara.right := fracpara;
  600. { and add the realtype para (this also removes the link }
  601. { to any parameters coming after it) }
  602. fracpara.right := ccallparanode.create(
  603. cordconstnode.create(ord(tfloatdef(para.left.resulttype.def).typ),
  604. s32bittype),nil);
  605. end;
  606. end;
  607. if do_read and
  608. ((is_ordinal and
  609. (torddef(para.left.resulttype.def).typ in [s8bit,s16bit,u8bit,u16bit])
  610. ) or
  611. (is_real and
  612. not is_equal(para.left.resulttype.def,pbestrealtype^.def)
  613. )
  614. ) then
  615. { special handling of reading small numbers, because the helpers }
  616. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  617. { use functions because then the call to FPC_IOCHECK destroys }
  618. { their result before we can store it }
  619. begin
  620. { get the resulttype of the var parameter of the helper }
  621. if is_real then
  622. restype := pbestrealtype
  623. else if is_signed(para.left.resulttype.def) then
  624. restype := @s32bittype
  625. else
  626. restype := @u32bittype;
  627. { create the parameter list: the temp ... }
  628. temp := ctempcreatenode.create(restype^,restype^.def.size,true);
  629. newstatement.left := cstatementnode.create(nil,temp);
  630. newstatement := tstatementnode(newstatement.left);
  631. { ... and the file }
  632. p1 := ccallparanode.create(ctemprefnode.create(temp),
  633. filepara.getcopy);
  634. { create the call to the helper }
  635. newstatement.left := cstatementnode.create(nil,
  636. ccallnode.createintern(name,tcallparanode(p1)));
  637. newstatement := tstatementnode(newstatement.left);
  638. { assign the result to the original var (this automatically }
  639. { takes care of range checking) }
  640. newstatement.left := cstatementnode.create(nil,
  641. cassignmentnode.create(para.left,
  642. ctemprefnode.create(temp)));
  643. newstatement := tstatementnode(newstatement.left);
  644. { release the temp location }
  645. newstatement.left := cstatementnode.create(nil,
  646. ctempdeletenode.create(temp));
  647. newstatement := tstatementnode(newstatement.left);
  648. { statement of para is used }
  649. para.left := nil;
  650. { free the enclosing tcallparanode, but not the }
  651. { parameters coming after it }
  652. para.right := nil;
  653. para.free;
  654. end
  655. else
  656. { read of non s/u-8/16bit, or a write }
  657. begin
  658. { add the filepara to the current parameter }
  659. para.right := filepara.getcopy;
  660. { add the lenpara (fracpara and realtype are already linked }
  661. { with it if necessary) }
  662. tcallparanode(para.right).right := lenpara;
  663. { create the call statement }
  664. newstatement.left := cstatementnode.create(nil,
  665. ccallnode.createintern(name,para));
  666. newstatement := tstatementnode(newstatement.left);
  667. end
  668. end
  669. else
  670. { error_para = true }
  671. begin
  672. { free the parameter, since it isn't referenced anywhere anymore }
  673. para.right := nil;
  674. para.free;
  675. if assigned(lenpara) then
  676. begin
  677. lenpara.right := nil;
  678. lenpara.free;
  679. end;
  680. if assigned(fracpara) then
  681. begin
  682. fracpara.right := nil;
  683. fracpara.free;
  684. end;
  685. end;
  686. { process next parameter }
  687. para := nextpara;
  688. end;
  689. { if no error, add the write(ln)/read(ln) end calls }
  690. if not found_error then
  691. begin
  692. case inlinenumber of
  693. in_read_x:
  694. newstatement.left := ccallnode.createintern('fpc_read_end',filepara);
  695. in_write_x:
  696. newstatement.left := ccallnode.createintern('fpc_write_end',filepara);
  697. in_readln_x:
  698. newstatement.left := ccallnode.createintern('fpc_readln_end',filepara);
  699. in_writeln_x:
  700. newstatement.left := ccallnode.createintern('fpc_writeln_end',filepara);
  701. end;
  702. newstatement.left := cstatementnode.create(nil,newstatement.left);
  703. newstatement := tstatementnode(newstatement.left);
  704. end;
  705. end;
  706. { if we found an error, simply delete the generated blocknode }
  707. if found_error then
  708. newblock.free
  709. else
  710. begin
  711. { deallocate the temp for the file para if we used one }
  712. if assigned(filetemp) then
  713. begin
  714. newstatement.left := cstatementnode.create(nil,
  715. ctempdeletenode.create(filetemp));
  716. newstatement := tstatementnode(newstatement.left);
  717. end;
  718. { otherwise return the newly generated block of instructions, }
  719. { but first free the errornode we generated at the beginning }
  720. result.free;
  721. result := newblock
  722. end;
  723. end;
  724. function tinlinenode.handle_val: tnode;
  725. var
  726. procname,
  727. suffix : string[31];
  728. sourcepara,
  729. destpara,
  730. codepara,
  731. sizepara,
  732. newparas : tcallparanode;
  733. orgcode : tnode;
  734. newstatement : tstatementnode;
  735. newblock : tblocknode;
  736. tempcode : ttempcreatenode;
  737. begin
  738. { for easy exiting if something goes wrong }
  739. result := cerrornode.create;
  740. { check the amount of parameters }
  741. if not(assigned(left)) or
  742. not(assigned(tcallparanode(left).right)) then
  743. begin
  744. CGMessage(parser_e_wrong_parameter_size);
  745. exit;
  746. end;
  747. { reverse parameters for easier processing }
  748. left := reverseparameters(tcallparanode(left));
  749. { get the parameters }
  750. tempcode := nil;
  751. orgcode := nil;
  752. sizepara := nil;
  753. sourcepara := tcallparanode(left);
  754. destpara := tcallparanode(sourcepara.right);
  755. codepara := tcallparanode(destpara.right);
  756. { check if codepara is valid }
  757. if assigned(codepara) and
  758. ((codepara.resulttype.def.deftype <> orddef) or
  759. is_64bitint(codepara.resulttype.def)) then
  760. begin
  761. CGMessagePos(codepara.fileinfo,type_e_mismatch);
  762. exit;
  763. end;
  764. { check if dest para is valid }
  765. if not(destpara.resulttype.def.deftype in [orddef,floatdef]) then
  766. begin
  767. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  768. exit;
  769. end;
  770. { we're going to reuse the exisiting para's, so make sure they }
  771. { won't be disposed }
  772. left := nil;
  773. { create the blocknode which will hold the generated statements + }
  774. { an initial dummy statement }
  775. newstatement := cstatementnode.create(nil,cnothingnode.create);
  776. newblock := cblocknode.create(newstatement);
  777. { do we need a temp for code? Yes, if no code specified, or if }
  778. { code is not a 32bit parameter (we already checked whether the }
  779. { the code para, if specified, was an orddef) }
  780. if not assigned(codepara) or
  781. (torddef(codepara.resulttype.def).typ in [u8bit,u16bit,s8bit,s16bit]) then
  782. begin
  783. tempcode := ctempcreatenode.create(s32bittype,4,true);
  784. newstatement.left := cstatementnode.create(nil,tempcode);
  785. newstatement := tstatementnode(newstatement.left);
  786. { set the resulttype of the temp (needed to be able to get }
  787. { the resulttype of the tempref used in the new code para) }
  788. resulttypepass(tnode(tempcode));
  789. { create a temp codepara, but save the original code para to }
  790. { assign the result to later on }
  791. if assigned(codepara) then
  792. orgcode := codepara.left
  793. else
  794. codepara := ccallparanode.create(nil,nil);
  795. codepara.left := ctemprefnode.create(tempcode);
  796. { we need its resulttype later on }
  797. codepara.get_paratype;
  798. end
  799. else if (torddef(codepara.resulttype.def).typ = u32bit) then
  800. { because code is a var parameter, it must match types exactly }
  801. { however, since it will return values in [0..255], both longints }
  802. { and cardinals are fine. Since the formal code para type is }
  803. { longint, insert a typecoversion to longint for cardinal para's }
  804. begin
  805. codepara.left := ctypeconvnode.create(codepara.left,s32bittype);
  806. { make it explicit, oterwise you may get a nonsense range }
  807. { check error if the cardinal already contained a value }
  808. { > $7fffffff }
  809. codepara.left.toggleflag(nf_explizit);
  810. codepara.get_paratype;
  811. end;
  812. { create the procedure name }
  813. procname := 'fpc_val_';
  814. case destpara.resulttype.def.deftype of
  815. orddef:
  816. begin
  817. case torddef(destpara.resulttype.def).typ of
  818. s8bit,s16bit,s32bit:
  819. begin
  820. suffix := 'sint_';
  821. { we also need a destsize para in this case }
  822. sizepara := ccallparanode.create(cordconstnode.create
  823. (destpara.resulttype.def.size,s32bittype),nil);
  824. end;
  825. u8bit,u16bit,u32bit:
  826. suffix := 'uint_';
  827. s64bit: suffix := 'int64_';
  828. u64bit: suffix := 'qword_';
  829. end;
  830. end;
  831. floatdef:
  832. begin
  833. suffix := 'real_';
  834. end;
  835. end;
  836. procname := procname + suffix;
  837. { play a trick to have tcallnode handle invalid source parameters: }
  838. { the shortstring-longint val routine by default }
  839. if (sourcepara.resulttype.def.deftype = stringdef) then
  840. procname := procname + tstringdef(sourcepara.resulttype.def).stringtypname
  841. else procname := procname + 'shortstr';
  842. { set up the correct parameters for the call: the code para... }
  843. newparas := codepara;
  844. { and the source para }
  845. codepara.right := sourcepara;
  846. { sizepara either contains nil if none is needed (which is ok, since }
  847. { then the next statement severes any possible links with other paras }
  848. { that sourcepara may have) or it contains the necessary size para and }
  849. { its right field is nil }
  850. sourcepara.right := sizepara;
  851. { create the call and assign the result to dest }
  852. { (val helpers are functions) }
  853. { the assignment will take care of rangechecking }
  854. newstatement.left := cstatementnode.create(nil,cassignmentnode.create(
  855. destpara.left,ccallnode.createintern(procname,newparas)));
  856. newstatement := tstatementnode(newstatement.left);
  857. { dispose of the enclosing paranode of the destination }
  858. destpara.left := nil;
  859. destpara.right := nil;
  860. destpara.free;
  861. { check if we used a temp for code and whether we have to store }
  862. { it to the real code parameter }
  863. if assigned(orgcode) then
  864. begin
  865. newstatement.left := cstatementnode.create(nil,cassignmentnode.create(
  866. orgcode,ctemprefnode.create(tempcode)));
  867. newstatement := tstatementnode(newstatement.left);
  868. end;
  869. { release the temp if we allocated one }
  870. if assigned(tempcode) then
  871. begin
  872. newstatement.left := cstatementnode.create(nil,
  873. ctempdeletenode.create(tempcode));
  874. newstatement := tstatementnode(newstatement.left);
  875. end;
  876. { free the errornode }
  877. result.free;
  878. { and return it }
  879. result := newblock;
  880. end;
  881. function tinlinenode.det_resulttype:tnode;
  882. function do_lowhigh(const t:ttype) : tnode;
  883. var
  884. v : tconstexprint;
  885. enum : tenumsym;
  886. hp : tnode;
  887. begin
  888. case t.def.deftype of
  889. orddef:
  890. begin
  891. if inlinenumber=in_low_x then
  892. v:=torddef(t.def).low
  893. else
  894. v:=torddef(t.def).high;
  895. { low/high of torddef are longints, so we need special }
  896. { handling for cardinal and 64bit types (JM) }
  897. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  898. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  899. shl twice (PFV) }
  900. if is_signed(t.def) and
  901. is_64bitint(t.def) then
  902. if (inlinenumber=in_low_x) then
  903. v := int64($80000000) shl 32
  904. else
  905. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  906. else
  907. if is_64bitint(t.def) then
  908. { we have to use a dirty trick for high(qword), }
  909. { because it's bigger than high(tconstexprint) (JM) }
  910. v := 0
  911. else
  912. if not is_signed(t.def) then
  913. v := cardinal(v);
  914. hp:=cordconstnode.create(v,t);
  915. resulttypepass(hp);
  916. { fix high(qword) }
  917. if not is_signed(t.def) and
  918. is_64bitint(t.def) and
  919. (inlinenumber = in_high_x) then
  920. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  921. do_lowhigh:=hp;
  922. end;
  923. enumdef:
  924. begin
  925. enum:=tenumsym(tenumdef(t.def).firstenum);
  926. v:=tenumdef(t.def).maxval;
  927. if inlinenumber=in_high_x then
  928. while assigned(enum) and (enum.value <> v) do
  929. enum:=enum.nextenum;
  930. if not assigned(enum) then
  931. internalerror(309993)
  932. else
  933. hp:=genenumnode(enum);
  934. do_lowhigh:=hp;
  935. end;
  936. else
  937. internalerror(87);
  938. end;
  939. end;
  940. function getconstrealvalue : bestreal;
  941. begin
  942. case left.nodetype of
  943. ordconstn:
  944. getconstrealvalue:=tordconstnode(left).value;
  945. realconstn:
  946. getconstrealvalue:=trealconstnode(left).value_real;
  947. else
  948. internalerror(309992);
  949. end;
  950. end;
  951. procedure setconstrealvalue(r : bestreal);
  952. begin
  953. result:=crealconstnode.create(r,pbestrealtype^);
  954. end;
  955. var
  956. vl,vl2 : longint;
  957. vr : bestreal;
  958. hp : tnode;
  959. srsym : tsym;
  960. isreal : boolean;
  961. label
  962. myexit;
  963. begin
  964. result:=nil;
  965. { if we handle writeln; left contains no valid address }
  966. if assigned(left) then
  967. begin
  968. if left.nodetype=callparan then
  969. tcallparanode(left).get_paratype
  970. else
  971. resulttypepass(left);
  972. end;
  973. inc(parsing_para_level);
  974. { handle intern constant functions in separate case }
  975. if nf_inlineconst in flags then
  976. begin
  977. { no parameters? }
  978. if not assigned(left) then
  979. begin
  980. case inlinenumber of
  981. in_const_pi :
  982. hp:=crealconstnode.create(pi,pbestrealtype^);
  983. else
  984. internalerror(89);
  985. end;
  986. end
  987. else
  988. begin
  989. vl:=0;
  990. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  991. vr:=0;
  992. isreal:=false;
  993. case left.nodetype of
  994. realconstn :
  995. begin
  996. isreal:=true;
  997. vr:=trealconstnode(left).value_real;
  998. end;
  999. ordconstn :
  1000. vl:=tordconstnode(left).value;
  1001. callparan :
  1002. begin
  1003. { both exists, else it was not generated }
  1004. vl:=tordconstnode(tcallparanode(left).left).value;
  1005. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1006. end;
  1007. else
  1008. CGMessage(cg_e_illegal_expression);
  1009. end;
  1010. case inlinenumber of
  1011. in_const_trunc :
  1012. begin
  1013. if isreal then
  1014. begin
  1015. if (vr>=2147483648.0) or (vr<=-2147483649.0) then
  1016. begin
  1017. CGMessage(parser_e_range_check_error);
  1018. hp:=cordconstnode.create(1,s32bittype)
  1019. end
  1020. else
  1021. hp:=cordconstnode.create(trunc(vr),s32bittype)
  1022. end
  1023. else
  1024. hp:=cordconstnode.create(trunc(vl),s32bittype);
  1025. end;
  1026. in_const_round :
  1027. begin
  1028. if isreal then
  1029. begin
  1030. if (vr>=2147483647.5) or (vr<=-2147483648.5) then
  1031. begin
  1032. CGMessage(parser_e_range_check_error);
  1033. hp:=cordconstnode.create(1,s32bittype)
  1034. end
  1035. else
  1036. hp:=cordconstnode.create(round(vr),s32bittype)
  1037. end
  1038. else
  1039. hp:=cordconstnode.create(round(vl),s32bittype);
  1040. end;
  1041. in_const_frac :
  1042. begin
  1043. if isreal then
  1044. hp:=crealconstnode.create(frac(vr),pbestrealtype^)
  1045. else
  1046. hp:=crealconstnode.create(frac(vl),pbestrealtype^);
  1047. end;
  1048. in_const_int :
  1049. begin
  1050. if isreal then
  1051. hp:=crealconstnode.create(int(vr),pbestrealtype^)
  1052. else
  1053. hp:=crealconstnode.create(int(vl),pbestrealtype^);
  1054. end;
  1055. in_const_abs :
  1056. begin
  1057. if isreal then
  1058. hp:=crealconstnode.create(abs(vr),pbestrealtype^)
  1059. else
  1060. hp:=cordconstnode.create(abs(vl),left.resulttype);
  1061. end;
  1062. in_const_sqr :
  1063. begin
  1064. if isreal then
  1065. hp:=crealconstnode.create(sqr(vr),pbestrealtype^)
  1066. else
  1067. hp:=cordconstnode.create(sqr(vl),left.resulttype);
  1068. end;
  1069. in_const_odd :
  1070. begin
  1071. if isreal then
  1072. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1073. else
  1074. hp:=cordconstnode.create(byte(odd(vl)),booltype);
  1075. end;
  1076. in_const_swap_word :
  1077. begin
  1078. if isreal then
  1079. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1080. else
  1081. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resulttype);
  1082. end;
  1083. in_const_swap_long :
  1084. begin
  1085. if isreal then
  1086. CGMessage(type_e_mismatch)
  1087. else
  1088. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resulttype);
  1089. end;
  1090. in_const_ptr :
  1091. begin
  1092. if isreal then
  1093. CGMessage(type_e_mismatch)
  1094. else
  1095. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1096. end;
  1097. in_const_sqrt :
  1098. begin
  1099. if isreal then
  1100. begin
  1101. if vr<0.0 then
  1102. CGMessage(type_e_wrong_math_argument)
  1103. else
  1104. hp:=crealconstnode.create(sqrt(vr),pbestrealtype^)
  1105. end
  1106. else
  1107. begin
  1108. if vl<0 then
  1109. CGMessage(type_e_wrong_math_argument)
  1110. else
  1111. hp:=crealconstnode.create(sqrt(vl),pbestrealtype^);
  1112. end;
  1113. end;
  1114. in_const_arctan :
  1115. begin
  1116. if isreal then
  1117. hp:=crealconstnode.create(arctan(vr),pbestrealtype^)
  1118. else
  1119. hp:=crealconstnode.create(arctan(vl),pbestrealtype^);
  1120. end;
  1121. in_const_cos :
  1122. begin
  1123. if isreal then
  1124. hp:=crealconstnode.create(cos(vr),pbestrealtype^)
  1125. else
  1126. hp:=crealconstnode.create(cos(vl),pbestrealtype^);
  1127. end;
  1128. in_const_sin :
  1129. begin
  1130. if isreal then
  1131. hp:=crealconstnode.create(sin(vr),pbestrealtype^)
  1132. else
  1133. hp:=crealconstnode.create(sin(vl),pbestrealtype^);
  1134. end;
  1135. in_const_exp :
  1136. begin
  1137. if isreal then
  1138. hp:=crealconstnode.create(exp(vr),pbestrealtype^)
  1139. else
  1140. hp:=crealconstnode.create(exp(vl),pbestrealtype^);
  1141. end;
  1142. in_const_ln :
  1143. begin
  1144. if isreal then
  1145. begin
  1146. if vr<=0.0 then
  1147. CGMessage(type_e_wrong_math_argument)
  1148. else
  1149. hp:=crealconstnode.create(ln(vr),pbestrealtype^)
  1150. end
  1151. else
  1152. begin
  1153. if vl<=0 then
  1154. CGMessage(type_e_wrong_math_argument)
  1155. else
  1156. hp:=crealconstnode.create(ln(vl),pbestrealtype^);
  1157. end;
  1158. end;
  1159. else
  1160. internalerror(88);
  1161. end;
  1162. end;
  1163. if hp=nil then
  1164. hp:=tnode.create(errorn);
  1165. result:=hp;
  1166. goto myexit;
  1167. end
  1168. else
  1169. begin
  1170. case inlinenumber of
  1171. in_lo_long,
  1172. in_hi_long,
  1173. in_lo_qword,
  1174. in_hi_qword,
  1175. in_lo_word,
  1176. in_hi_word :
  1177. begin
  1178. { give warning for incompatibility with tp and delphi }
  1179. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1180. ((m_tp7 in aktmodeswitches) or
  1181. (m_delphi in aktmodeswitches)) then
  1182. CGMessage(type_w_maybe_wrong_hi_lo);
  1183. { constant folding }
  1184. if left.nodetype=ordconstn then
  1185. begin
  1186. case inlinenumber of
  1187. in_lo_word :
  1188. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype);
  1189. in_hi_word :
  1190. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype);
  1191. in_lo_long :
  1192. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype);
  1193. in_hi_long :
  1194. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype);
  1195. in_lo_qword :
  1196. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype);
  1197. in_hi_qword :
  1198. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype);
  1199. end;
  1200. result:=hp;
  1201. goto myexit;
  1202. end;
  1203. set_varstate(left,true);
  1204. if not is_integer(left.resulttype.def) then
  1205. CGMessage(type_e_mismatch);
  1206. case inlinenumber of
  1207. in_lo_word,
  1208. in_hi_word :
  1209. resulttype:=u8bittype;
  1210. in_lo_long,
  1211. in_hi_long :
  1212. resulttype:=u16bittype;
  1213. in_lo_qword,
  1214. in_hi_qword :
  1215. resulttype:=u32bittype;
  1216. end;
  1217. end;
  1218. in_sizeof_x:
  1219. begin
  1220. set_varstate(left,false);
  1221. resulttype:=s32bittype;
  1222. end;
  1223. in_typeof_x:
  1224. begin
  1225. set_varstate(left,false);
  1226. resulttype:=voidpointertype;
  1227. end;
  1228. in_ord_x:
  1229. begin
  1230. if (left.nodetype=ordconstn) then
  1231. begin
  1232. hp:=cordconstnode.create(tordconstnode(left).value,s32bittype);
  1233. result:=hp;
  1234. goto myexit;
  1235. end;
  1236. set_varstate(left,true);
  1237. case left.resulttype.def.deftype of
  1238. orddef :
  1239. begin
  1240. case torddef(left.resulttype.def).typ of
  1241. bool8bit,
  1242. uchar:
  1243. begin
  1244. { change to byte() }
  1245. hp:=ctypeconvnode.create(left,u8bittype);
  1246. left:=nil;
  1247. include(hp.flags,nf_explizit);
  1248. result:=hp;
  1249. end;
  1250. bool16bit,
  1251. uwidechar :
  1252. begin
  1253. { change to word() }
  1254. hp:=ctypeconvnode.create(left,u16bittype);
  1255. left:=nil;
  1256. include(hp.flags,nf_explizit);
  1257. result:=hp;
  1258. end;
  1259. bool32bit :
  1260. begin
  1261. { change to dword() }
  1262. hp:=ctypeconvnode.create(left,u32bittype);
  1263. left:=nil;
  1264. include(hp.flags,nf_explizit);
  1265. result:=hp;
  1266. end;
  1267. uvoid :
  1268. CGMessage(type_e_mismatch)
  1269. else
  1270. begin
  1271. { all other orddef need no transformation }
  1272. hp:=left;
  1273. left:=nil;
  1274. result:=hp;
  1275. end;
  1276. end;
  1277. end;
  1278. enumdef :
  1279. begin
  1280. hp:=ctypeconvnode.create(left,s32bittype);
  1281. left:=nil;
  1282. include(hp.flags,nf_explizit);
  1283. result:=hp;
  1284. end;
  1285. else
  1286. CGMessage(type_e_mismatch);
  1287. end;
  1288. end;
  1289. in_chr_byte:
  1290. begin
  1291. { convert to explicit char() }
  1292. set_varstate(left,true);
  1293. hp:=ctypeconvnode.create(left,cchartype);
  1294. include(hp.flags,nf_explizit);
  1295. left:=nil;
  1296. result:=hp;
  1297. end;
  1298. in_length_x:
  1299. begin
  1300. set_varstate(left,true);
  1301. case left.resulttype.def.deftype of
  1302. stringdef :
  1303. begin
  1304. { we don't need string convertions here }
  1305. if (left.nodetype=typeconvn) and
  1306. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  1307. begin
  1308. hp:=ttypeconvnode(left).left;
  1309. ttypeconvnode(left).left:=nil;
  1310. left.free;
  1311. left:=hp;
  1312. end;
  1313. { evaluates length of constant strings direct }
  1314. if (left.nodetype=stringconstn) then
  1315. begin
  1316. hp:=cordconstnode.create(tstringconstnode(left).len,s32bittype);
  1317. result:=hp;
  1318. goto myexit;
  1319. end;
  1320. end;
  1321. orddef :
  1322. begin
  1323. { length of char is one allways }
  1324. if is_char(left.resulttype.def) or
  1325. is_widechar(left.resulttype.def) then
  1326. begin
  1327. hp:=cordconstnode.create(1,s32bittype);
  1328. result:=hp;
  1329. goto myexit;
  1330. end
  1331. else
  1332. CGMessage(type_e_mismatch);
  1333. end;
  1334. arraydef :
  1335. begin
  1336. if is_open_array(left.resulttype.def) or
  1337. is_array_of_const(left.resulttype.def) then
  1338. begin
  1339. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1340. hp:=caddnode.create(addn,cloadnode.create(srsym,tloadnode(left).symtable),
  1341. cordconstnode.create(1,s32bittype));
  1342. result:=hp;
  1343. goto myexit;
  1344. end
  1345. else
  1346. if not is_dynamic_array(left.resulttype.def) then
  1347. begin
  1348. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange-
  1349. tarraydef(left.resulttype.def).lowrange+1,
  1350. s32bittype);
  1351. result:=hp;
  1352. goto myexit;
  1353. end
  1354. else
  1355. begin
  1356. { can't use inserttypeconv because we need }
  1357. { an explicit type conversion (JM) }
  1358. hp := ctypeconvnode.create(left,voidpointertype);
  1359. hp.toggleflag(nf_explizit);
  1360. hp := ccallparanode.create(hp,nil);
  1361. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1362. { make sure the left node doesn't get disposed, since it's }
  1363. { reused in the new node (JM) }
  1364. left:=nil;
  1365. resulttypepass(result);
  1366. exit;
  1367. end;
  1368. end;
  1369. else
  1370. CGMessage(type_e_mismatch);
  1371. end;
  1372. { shortstring return an 8 bit value as the length
  1373. is the first byte of the string }
  1374. if is_shortstring(left.resulttype.def) then
  1375. resulttype:=u8bittype
  1376. else
  1377. resulttype:=s32bittype;
  1378. end;
  1379. in_typeinfo_x:
  1380. begin
  1381. set_varstate(left,true);
  1382. resulttype:=voidpointertype;
  1383. end;
  1384. in_assigned_x:
  1385. begin
  1386. { assigned(nil) is always false }
  1387. if (tcallparanode(left).left.nodetype=niln) then
  1388. begin
  1389. hp:=cordconstnode.create(0,booltype);
  1390. result:=hp;
  1391. goto myexit;
  1392. end;
  1393. { assigned(pointer(n)) is only false when n=0 }
  1394. if (tcallparanode(left).left.nodetype=pointerconstn) then
  1395. begin
  1396. if tpointerconstnode(tcallparanode(left).left).value=0 then
  1397. hp:=cordconstnode.create(0,booltype)
  1398. else
  1399. hp:=cordconstnode.create(1,booltype);
  1400. result:=hp;
  1401. goto myexit;
  1402. end;
  1403. set_varstate(left,true);
  1404. resulttype:=booltype;
  1405. end;
  1406. in_ofs_x :
  1407. internalerror(2000101001);
  1408. in_seg_x :
  1409. begin
  1410. set_varstate(left,false);
  1411. hp:=cordconstnode.create(0,s32bittype);
  1412. result:=hp;
  1413. goto myexit;
  1414. end;
  1415. in_pred_x,
  1416. in_succ_x:
  1417. begin
  1418. set_varstate(left,true);
  1419. resulttype:=left.resulttype;
  1420. if not is_ordinal(resulttype.def) then
  1421. CGMessage(type_e_ordinal_expr_expected)
  1422. else
  1423. begin
  1424. if (resulttype.def.deftype=enumdef) and
  1425. (tenumdef(resulttype.def).has_jumps) then
  1426. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1427. end;
  1428. { do constant folding after check for jumps }
  1429. if left.nodetype=ordconstn then
  1430. begin
  1431. if inlinenumber=in_succ_x then
  1432. hp:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype)
  1433. else
  1434. hp:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype);
  1435. result:=hp;
  1436. end;
  1437. end;
  1438. in_finalize_x,
  1439. in_setlength_x:
  1440. begin
  1441. { inlined from pinline }
  1442. internalerror(200204231);
  1443. end;
  1444. in_inc_x,
  1445. in_dec_x:
  1446. begin
  1447. resulttype:=voidtype;
  1448. if assigned(left) then
  1449. begin
  1450. set_varstate(left,true);
  1451. if codegenerror then
  1452. exit;
  1453. { first param must be var }
  1454. valid_for_var(tcallparanode(left).left);
  1455. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1456. is_ordinal(left.resulttype.def) then
  1457. begin
  1458. { value of left gets changed -> must be unique }
  1459. { (bug 1735) (JM) }
  1460. set_unique(tcallparanode(left).left);
  1461. { two paras ? }
  1462. if assigned(tcallparanode(left).right) then
  1463. begin
  1464. { insert a type conversion }
  1465. { the second param is always longint }
  1466. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  1467. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1468. CGMessage(cg_e_illegal_expression);
  1469. end;
  1470. end
  1471. else
  1472. CGMessage(type_e_ordinal_expr_expected);
  1473. end
  1474. else
  1475. CGMessage(type_e_mismatch);
  1476. end;
  1477. in_read_x,
  1478. in_readln_x,
  1479. in_write_x,
  1480. in_writeln_x :
  1481. begin
  1482. result := handle_read_write;
  1483. end;
  1484. in_settextbuf_file_x :
  1485. begin
  1486. resulttype:=voidtype;
  1487. { now we know the type of buffer }
  1488. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1489. hp:=ccallparanode.create(cordconstnode.create(tcallparanode(left).left.resulttype.def.size,s32bittype),left);
  1490. hp:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil);
  1491. left:=nil;
  1492. result:=hp;
  1493. end;
  1494. { the firstpass of the arg has been done in firstcalln ? }
  1495. in_reset_typedfile,
  1496. in_rewrite_typedfile :
  1497. begin
  1498. result := handle_reset_rewrite_typed;
  1499. end;
  1500. in_str_x_string :
  1501. begin
  1502. result := handle_str;
  1503. end;
  1504. in_val_x :
  1505. begin
  1506. result := handle_val;
  1507. end;
  1508. in_include_x_y,
  1509. in_exclude_x_y:
  1510. begin
  1511. resulttype:=voidtype;
  1512. { the parser already checks whether we have two (and exectly two) }
  1513. { parameters (JM) }
  1514. set_varstate(left,true);
  1515. { first param must be var }
  1516. valid_for_var(tcallparanode(left).left);
  1517. { check type }
  1518. if (left.resulttype.def.deftype=setdef) then
  1519. begin
  1520. { insert a type conversion }
  1521. { to the type of the set elements }
  1522. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1523. tsetdef(left.resulttype.def).elementtype);
  1524. end
  1525. else
  1526. CGMessage(type_e_mismatch);
  1527. end;
  1528. in_low_x,
  1529. in_high_x:
  1530. begin
  1531. set_varstate(left,false);
  1532. case left.resulttype.def.deftype of
  1533. orddef,
  1534. enumdef:
  1535. begin
  1536. result:=do_lowhigh(left.resulttype);
  1537. end;
  1538. setdef:
  1539. begin
  1540. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1541. end;
  1542. arraydef:
  1543. begin
  1544. if inlinenumber=in_low_x then
  1545. begin
  1546. result:=cordconstnode.create(tarraydef(left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype);
  1547. end
  1548. else
  1549. begin
  1550. if is_open_array(left.resulttype.def) or
  1551. is_array_of_const(left.resulttype.def) then
  1552. begin
  1553. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1554. result:=cloadnode.create(srsym,tloadnode(left).symtable);
  1555. end
  1556. else
  1557. if is_dynamic_array(left.resulttype.def) then
  1558. begin
  1559. { can't use inserttypeconv because we need }
  1560. { an explicit type conversion (JM) }
  1561. hp := ctypeconvnode.create(left,voidpointertype);
  1562. hp.toggleflag(nf_explizit);
  1563. hp := ccallparanode.create(hp,nil);
  1564. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1565. { make sure the left node doesn't get disposed, since it's }
  1566. { reused in the new node (JM) }
  1567. left:=nil;
  1568. end
  1569. else
  1570. begin
  1571. result:=cordconstnode.create(tarraydef(left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype);
  1572. end;
  1573. end;
  1574. resulttypepass(result);
  1575. end;
  1576. stringdef:
  1577. begin
  1578. if inlinenumber=in_low_x then
  1579. begin
  1580. hp:=cordconstnode.create(0,u8bittype);
  1581. resulttypepass(hp);
  1582. result:=hp;
  1583. end
  1584. else
  1585. begin
  1586. if is_open_string(left.resulttype.def) then
  1587. begin
  1588. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1589. hp:=cloadnode.create(srsym,tloadnode(left).symtable);
  1590. resulttypepass(hp);
  1591. result:=hp;
  1592. end
  1593. else
  1594. begin
  1595. hp:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8bittype);
  1596. resulttypepass(hp);
  1597. result:=hp;
  1598. end;
  1599. end;
  1600. end;
  1601. else
  1602. CGMessage(type_e_mismatch);
  1603. end;
  1604. end;
  1605. in_pi:
  1606. begin
  1607. if block_type=bt_const then
  1608. setconstrealvalue(pi)
  1609. else
  1610. resulttype:=s80floattype;
  1611. end;
  1612. in_cos_extended :
  1613. begin
  1614. if left.nodetype in [ordconstn,realconstn] then
  1615. setconstrealvalue(cos(getconstrealvalue))
  1616. else
  1617. begin
  1618. set_varstate(left,true);
  1619. inserttypeconv(left,s80floattype);
  1620. resulttype:=s80floattype;
  1621. end;
  1622. end;
  1623. in_sin_extended :
  1624. begin
  1625. if left.nodetype in [ordconstn,realconstn] then
  1626. setconstrealvalue(sin(getconstrealvalue))
  1627. else
  1628. begin
  1629. set_varstate(left,true);
  1630. inserttypeconv(left,s80floattype);
  1631. resulttype:=s80floattype;
  1632. end;
  1633. end;
  1634. in_arctan_extended :
  1635. begin
  1636. if left.nodetype in [ordconstn,realconstn] then
  1637. setconstrealvalue(arctan(getconstrealvalue))
  1638. else
  1639. begin
  1640. set_varstate(left,true);
  1641. inserttypeconv(left,s80floattype);
  1642. resulttype:=s80floattype;
  1643. end;
  1644. end;
  1645. in_abs_extended :
  1646. begin
  1647. if left.nodetype in [ordconstn,realconstn] then
  1648. setconstrealvalue(abs(getconstrealvalue))
  1649. else
  1650. begin
  1651. set_varstate(left,true);
  1652. inserttypeconv(left,s80floattype);
  1653. resulttype:=s80floattype;
  1654. end;
  1655. end;
  1656. in_sqr_extended :
  1657. begin
  1658. if left.nodetype in [ordconstn,realconstn] then
  1659. setconstrealvalue(sqr(getconstrealvalue))
  1660. else
  1661. begin
  1662. set_varstate(left,true);
  1663. inserttypeconv(left,s80floattype);
  1664. resulttype:=s80floattype;
  1665. end;
  1666. end;
  1667. in_sqrt_extended :
  1668. begin
  1669. if left.nodetype in [ordconstn,realconstn] then
  1670. begin
  1671. vr:=getconstrealvalue;
  1672. if vr<0.0 then
  1673. begin
  1674. CGMessage(type_e_wrong_math_argument);
  1675. setconstrealvalue(0);
  1676. end
  1677. else
  1678. setconstrealvalue(sqrt(vr));
  1679. end
  1680. else
  1681. begin
  1682. set_varstate(left,true);
  1683. inserttypeconv(left,s80floattype);
  1684. resulttype:=s80floattype;
  1685. end;
  1686. end;
  1687. in_ln_extended :
  1688. begin
  1689. if left.nodetype in [ordconstn,realconstn] then
  1690. begin
  1691. vr:=getconstrealvalue;
  1692. if vr<=0.0 then
  1693. begin
  1694. CGMessage(type_e_wrong_math_argument);
  1695. setconstrealvalue(0);
  1696. end
  1697. else
  1698. setconstrealvalue(ln(vr));
  1699. end
  1700. else
  1701. begin
  1702. set_varstate(left,true);
  1703. inserttypeconv(left,s80floattype);
  1704. resulttype:=s80floattype;
  1705. end;
  1706. end;
  1707. {$ifdef SUPPORT_MMX}
  1708. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1709. begin
  1710. end;
  1711. {$endif SUPPORT_MMX}
  1712. in_assert_x_y :
  1713. begin
  1714. resulttype:=voidtype;
  1715. if assigned(left) then
  1716. begin
  1717. set_varstate(left,true);
  1718. { check type }
  1719. if is_boolean(left.resulttype.def) then
  1720. begin
  1721. { must always be a string }
  1722. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1723. end
  1724. else
  1725. CGMessage(type_e_mismatch);
  1726. end
  1727. else
  1728. CGMessage(type_e_mismatch);
  1729. end;
  1730. else
  1731. internalerror(8);
  1732. end;
  1733. end;
  1734. myexit:
  1735. { Run get_paratype again to update maybe inserted typeconvs }
  1736. if not codegenerror then
  1737. begin
  1738. if assigned(left) and
  1739. (left.nodetype=callparan) then
  1740. tcallparanode(left).get_paratype;
  1741. end;
  1742. dec(parsing_para_level);
  1743. end;
  1744. {$ifdef fpc}
  1745. {$maxfpuregisters 0}
  1746. {$endif fpc}
  1747. function tinlinenode.pass_1 : tnode;
  1748. var
  1749. srsym : tsym;
  1750. hp,hpp : tnode;
  1751. shiftconst: longint;
  1752. begin
  1753. result:=nil;
  1754. { if we handle writeln; left contains no valid address }
  1755. if assigned(left) then
  1756. begin
  1757. if left.nodetype=callparan then
  1758. tcallparanode(left).firstcallparan(nil,false)
  1759. else
  1760. firstpass(left);
  1761. left_max;
  1762. location.loc:=left.location.loc;
  1763. end;
  1764. inc(parsing_para_level);
  1765. { intern const should already be handled }
  1766. if nf_inlineconst in flags then
  1767. internalerror(200104044);
  1768. case inlinenumber of
  1769. in_lo_qword,
  1770. in_hi_qword,
  1771. in_lo_long,
  1772. in_hi_long,
  1773. in_lo_word,
  1774. in_hi_word:
  1775. begin
  1776. shiftconst := 0;
  1777. case inlinenumber of
  1778. in_hi_qword:
  1779. shiftconst := 32;
  1780. in_hi_long:
  1781. shiftconst := 16;
  1782. in_hi_word:
  1783. shiftconst := 8;
  1784. end;
  1785. if shiftconst <> 0 then
  1786. result := ctypeconvnode.create(cshlshrnode.create(shrn,left,
  1787. cordconstnode.create(shiftconst,u32bittype)),resulttype)
  1788. else
  1789. result := ctypeconvnode.create(left,resulttype);
  1790. left := nil;
  1791. include(result.flags,nf_explizit);
  1792. firstpass(result);
  1793. end;
  1794. in_sizeof_x:
  1795. begin
  1796. if push_high_param(left.resulttype.def) then
  1797. begin
  1798. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1799. hp:=caddnode.create(addn,cloadnode.create(srsym,tloadnode(left).symtable),
  1800. cordconstnode.create(1,s32bittype));
  1801. if (left.resulttype.def.deftype=arraydef) and
  1802. (tarraydef(left.resulttype.def).elesize<>1) then
  1803. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(left.resulttype.def).elesize,s32bittype));
  1804. firstpass(hp);
  1805. result:=hp;
  1806. end
  1807. else
  1808. begin
  1809. if registers32<1 then
  1810. registers32:=1;
  1811. location.loc:=LOC_REGISTER;
  1812. end;
  1813. end;
  1814. in_typeof_x:
  1815. begin
  1816. if registers32<1 then
  1817. registers32:=1;
  1818. location.loc:=LOC_REGISTER;
  1819. end;
  1820. in_ord_x,
  1821. in_chr_byte:
  1822. begin
  1823. { should not happend as it's converted to typeconv }
  1824. internalerror(200104045);
  1825. end;
  1826. in_length_x:
  1827. begin
  1828. if is_shortstring(left.resulttype.def) then
  1829. location.loc:=LOC_REFERENCE
  1830. else
  1831. begin
  1832. { ansi/wide string }
  1833. if registers32<1 then
  1834. registers32:=1;
  1835. location.loc:=LOC_REGISTER;
  1836. end;
  1837. end;
  1838. in_typeinfo_x:
  1839. begin
  1840. location.loc:=LOC_REGISTER;
  1841. registers32:=1;
  1842. end;
  1843. in_assigned_x:
  1844. begin
  1845. location.loc:=LOC_FLAGS;
  1846. end;
  1847. in_ofs_x :
  1848. internalerror(2000101001);
  1849. in_seg_x :
  1850. internalerror(200104046);
  1851. in_pred_x,
  1852. in_succ_x:
  1853. begin
  1854. if is_64bitint(resulttype.def) then
  1855. begin
  1856. if (registers32<2) then
  1857. registers32:=2
  1858. end
  1859. else
  1860. begin
  1861. if (registers32<1) then
  1862. registers32:=1;
  1863. end;
  1864. location.loc:=LOC_REGISTER;
  1865. end;
  1866. in_setlength_x:
  1867. begin
  1868. end;
  1869. in_finalize_x:
  1870. begin
  1871. end;
  1872. in_inc_x,
  1873. in_dec_x:
  1874. begin
  1875. { check type }
  1876. if is_64bitint(left.resulttype.def) or
  1877. { range/overflow checking doesn't work properly }
  1878. { with the inc/dec code that's generated (JM) }
  1879. ((left.resulttype.def.deftype = orddef) and
  1880. not(is_char(left.resulttype.def)) and
  1881. not(is_boolean(left.resulttype.def)) and
  1882. (aktlocalswitches *
  1883. [cs_check_overflow,cs_check_range] <> [])) then
  1884. { convert to simple add (JM) }
  1885. begin
  1886. { extra parameter? }
  1887. if assigned(tcallparanode(left).right) then
  1888. begin
  1889. { Yes, use for add node }
  1890. hpp := tcallparanode(tcallparanode(left).right).left;
  1891. tcallparanode(tcallparanode(left).right).left := nil;
  1892. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1893. CGMessage(cg_e_illegal_expression);
  1894. end
  1895. else
  1896. { no, create constant 1 }
  1897. hpp := cordconstnode.create(1,s32bittype);
  1898. { addition/substraction depending on inc/dec }
  1899. if inlinenumber = in_inc_x then
  1900. hp := caddnode.create(addn,tcallparanode(left).left.getcopy,hpp)
  1901. else
  1902. hp := caddnode.create(subn,tcallparanode(left).left.getcopy,hpp);
  1903. { assign result of addition }
  1904. hpp := cassignmentnode.create(tcallparanode(left).left,hp);
  1905. tcallparanode(left).left := nil;
  1906. { firstpass it }
  1907. firstpass(hpp);
  1908. { return new node }
  1909. result := hpp;
  1910. end
  1911. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1912. is_ordinal(left.resulttype.def) then
  1913. begin
  1914. { two paras ? }
  1915. if assigned(tcallparanode(left).right) then
  1916. begin
  1917. { need we an additional register ? }
  1918. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  1919. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  1920. (tcallparanode(tcallparanode(left).right).left.registers32<=1) then
  1921. inc(registers32);
  1922. { do we need an additional register to restore the first parameter? }
  1923. if tcallparanode(tcallparanode(left).right).left.registers32>=registers32 then
  1924. inc(registers32);
  1925. end;
  1926. end;
  1927. end;
  1928. in_read_x,
  1929. in_readln_x,
  1930. in_write_x,
  1931. in_writeln_x :
  1932. begin
  1933. { should be handled by det_resulttype }
  1934. internalerror(200108234);
  1935. end;
  1936. in_settextbuf_file_x :
  1937. internalerror(200104262);
  1938. in_reset_typedfile,
  1939. in_rewrite_typedfile :
  1940. begin
  1941. { should already be removed in det_resulttype (JM) }
  1942. internalerror(200108236);
  1943. end;
  1944. in_str_x_string :
  1945. begin
  1946. { should already be removed in det_resulttype (JM) }
  1947. internalerror(200108235);
  1948. end;
  1949. in_val_x :
  1950. begin
  1951. { should already be removed in det_resulttype (JM) }
  1952. internalerror(200108242);
  1953. end;
  1954. in_include_x_y,
  1955. in_exclude_x_y:
  1956. begin
  1957. registers32:=left.registers32;
  1958. registersfpu:=left.registersfpu;
  1959. {$ifdef SUPPORT_MMX}
  1960. registersmmx:=left.registersmmx;
  1961. {$endif SUPPORT_MMX}
  1962. end;
  1963. in_low_x,
  1964. in_high_x:
  1965. internalerror(200104047);
  1966. in_cos_extended:
  1967. begin
  1968. location.loc:=LOC_FPUREGISTER;
  1969. registers32:=left.registers32;
  1970. registersfpu:=max(left.registersfpu,1);
  1971. {$ifdef SUPPORT_MMX}
  1972. registersmmx:=left.registersmmx;
  1973. {$endif SUPPORT_MMX}
  1974. end;
  1975. in_sin_extended:
  1976. begin
  1977. location.loc:=LOC_FPUREGISTER;
  1978. registers32:=left.registers32;
  1979. registersfpu:=max(left.registersfpu,1);
  1980. {$ifdef SUPPORT_MMX}
  1981. registersmmx:=left.registersmmx;
  1982. {$endif SUPPORT_MMX}
  1983. end;
  1984. in_arctan_extended:
  1985. begin
  1986. location.loc:=LOC_FPUREGISTER;
  1987. registers32:=left.registers32;
  1988. registersfpu:=max(left.registersfpu,2);
  1989. {$ifdef SUPPORT_MMX}
  1990. registersmmx:=left.registersmmx;
  1991. {$endif SUPPORT_MMX}
  1992. end;
  1993. in_pi:
  1994. begin
  1995. location.loc:=LOC_FPUREGISTER;
  1996. registersfpu:=1;
  1997. end;
  1998. in_abs_extended:
  1999. begin
  2000. location.loc:=LOC_FPUREGISTER;
  2001. registers32:=left.registers32;
  2002. registersfpu:=max(left.registersfpu,1);
  2003. {$ifdef SUPPORT_MMX}
  2004. registersmmx:=left.registersmmx;
  2005. {$endif SUPPORT_MMX}
  2006. end;
  2007. in_sqr_extended:
  2008. begin
  2009. location.loc:=LOC_FPUREGISTER;
  2010. registers32:=left.registers32;
  2011. registersfpu:=max(left.registersfpu,1);
  2012. {$ifdef SUPPORT_MMX}
  2013. registersmmx:=left.registersmmx;
  2014. {$endif SUPPORT_MMX}
  2015. end;
  2016. in_sqrt_extended:
  2017. begin
  2018. location.loc:=LOC_FPUREGISTER;
  2019. registers32:=left.registers32;
  2020. registersfpu:=max(left.registersfpu,1);
  2021. {$ifdef SUPPORT_MMX}
  2022. registersmmx:=left.registersmmx;
  2023. {$endif SUPPORT_MMX}
  2024. end;
  2025. in_ln_extended:
  2026. begin
  2027. location.loc:=LOC_FPUREGISTER;
  2028. registers32:=left.registers32;
  2029. registersfpu:=max(left.registersfpu,2);
  2030. {$ifdef SUPPORT_MMX}
  2031. registersmmx:=left.registersmmx;
  2032. {$endif SUPPORT_MMX}
  2033. end;
  2034. {$ifdef SUPPORT_MMX}
  2035. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2036. begin
  2037. end;
  2038. {$endif SUPPORT_MMX}
  2039. in_assert_x_y :
  2040. begin
  2041. { We've checked the whole statement for correctness, now we
  2042. can remove it if assertions are off }
  2043. if not(cs_do_assertion in aktlocalswitches) then
  2044. begin
  2045. { we need a valid node, so insert a nothingn }
  2046. result:=cnothingnode.create;
  2047. end
  2048. else
  2049. begin
  2050. registers32:=left.registers32;
  2051. registersfpu:=left.registersfpu;
  2052. {$ifdef SUPPORT_MMX}
  2053. registersmmx:=left.registersmmx;
  2054. {$endif SUPPORT_MMX}
  2055. end;
  2056. end;
  2057. else
  2058. internalerror(8);
  2059. end;
  2060. dec(parsing_para_level);
  2061. end;
  2062. {$ifdef fpc}
  2063. {$maxfpuregisters default}
  2064. {$endif fpc}
  2065. function tinlinenode.docompare(p: tnode): boolean;
  2066. begin
  2067. docompare :=
  2068. inherited docompare(p) and
  2069. (inlinenumber = tinlinenode(p).inlinenumber);
  2070. end;
  2071. begin
  2072. cinlinenode:=tinlinenode;
  2073. end.
  2074. {
  2075. $Log$
  2076. Revision 1.75 2002-05-16 19:46:38 carl
  2077. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2078. + try to fix temp allocation (still in ifdef)
  2079. + generic constructor calls
  2080. + start of tassembler / tmodulebase class cleanup
  2081. Revision 1.73 2002/05/12 16:53:07 peter
  2082. * moved entry and exitcode to ncgutil and cgobj
  2083. * foreach gets extra argument for passing local data to the
  2084. iterator function
  2085. * -CR checks also class typecasts at runtime by changing them
  2086. into as
  2087. * fixed compiler to cycle with the -CR option
  2088. * fixed stabs with elf writer, finally the global variables can
  2089. be watched
  2090. * removed a lot of routines from cga unit and replaced them by
  2091. calls to cgobj
  2092. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2093. u32bit then the other is typecasted also to u32bit without giving
  2094. a rangecheck warning/error.
  2095. * fixed pascal calling method with reversing also the high tree in
  2096. the parast, detected by tcalcst3 test
  2097. Revision 1.72 2002/04/23 19:16:34 peter
  2098. * add pinline unit that inserts compiler supported functions using
  2099. one or more statements
  2100. * moved finalize and setlength from ninl to pinline
  2101. Revision 1.71 2002/04/02 17:11:29 peter
  2102. * tlocation,treference update
  2103. * LOC_CONSTANT added for better constant handling
  2104. * secondadd splitted in multiple routines
  2105. * location_force_reg added for loading a location to a register
  2106. of a specified size
  2107. * secondassignment parses now first the right and then the left node
  2108. (this is compatible with Kylix). This saves a lot of push/pop especially
  2109. with string operations
  2110. * adapted some routines to use the new cg methods
  2111. Revision 1.70 2002/03/31 20:26:34 jonas
  2112. + a_loadfpu_* and a_loadmm_* methods in tcg
  2113. * register allocation is now handled by a class and is mostly processor
  2114. independent (+rgobj.pas and i386/rgcpu.pas)
  2115. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2116. * some small improvements and fixes to the optimizer
  2117. * some register allocation fixes
  2118. * some fpuvaroffset fixes in the unary minus node
  2119. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2120. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2121. also better optimizable)
  2122. * fixed and optimized register saving/restoring for new/dispose nodes
  2123. * LOC_FPU locations now also require their "register" field to be set to
  2124. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2125. - list field removed of the tnode class because it's not used currently
  2126. and can cause hard-to-find bugs
  2127. Revision 1.69 2002/01/24 18:25:48 peter
  2128. * implicit result variable generation for assembler routines
  2129. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2130. Revision 1.68 2002/01/19 11:53:56 peter
  2131. * constant evaluation for assinged added
  2132. Revision 1.67 2001/12/28 14:09:21 jonas
  2133. * fixed web bug 1735 (argument of inc/dec must be made unique) ("merged")
  2134. Revision 1.66 2001/12/10 14:26:22 jonas
  2135. - removed unnecessary resulttypepass call
  2136. Revision 1.65 2001/12/04 15:59:03 jonas
  2137. * converted lo/hi to processor independent code, generated code is the
  2138. same as before (when turning on the optimizer)
  2139. Revision 1.64 2001/12/03 14:21:34 jonas
  2140. * fixed web bug 1693 (dynarray support for length)
  2141. Revision 1.63 2001/10/24 16:17:36 jonas
  2142. * fixed web bug 1621 (write(typed_file,function_call) works again)
  2143. * allow write(typed_file,procvar_call) too (it was already allowed for
  2144. text file writes)
  2145. Revision 1.62 2001/09/30 16:16:28 jonas
  2146. - removed unused units form uses-clause and unused local vars
  2147. Revision 1.61 2001/09/24 16:09:55 jonas
  2148. * check if amount of dimensions passed to setlength for dynamic arrays
  2149. is correct
  2150. Revision 1.60 2001/09/24 11:35:55 jonas
  2151. * fix from Pavel V. Ozersk to accept multiple dimensions for setlength
  2152. and dynamical arrays
  2153. Revision 1.59 2001/09/17 21:29:12 peter
  2154. * merged netbsd, fpu-overflow from fixes branch
  2155. Revision 1.58 2001/09/05 15:19:43 jonas
  2156. * the result of high/low nodes wasn't always resulttypepassed
  2157. Revision 1.57 2001/09/04 14:32:45 jonas
  2158. * simplified det_resulttype code for include/exclude
  2159. * include/exclude doesn't use any helpers anymore in the i386 secondpass
  2160. Revision 1.56 2001/09/04 11:38:55 jonas
  2161. + searchsystype() and searchsystype() functions in symtable
  2162. * changed ninl and nadd to use these functions
  2163. * i386 set comparison functions now return their results in al instead
  2164. of in the flags so that they can be sued as compilerprocs
  2165. - removed all processor specific code from n386add.pas that has to do
  2166. with set handling, it's now all done in nadd.pas
  2167. * fixed fpc_set_contains_sets in genset.inc
  2168. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  2169. helper anymore
  2170. * some small fixes in compproc.inc/set.inc regarding the declaration of
  2171. internal helper types (fpc_small_set and fpc_normal_set)
  2172. Revision 1.55 2001/09/02 21:12:07 peter
  2173. * move class of definitions into type section for delphi
  2174. Revision 1.54 2001/08/28 13:24:46 jonas
  2175. + compilerproc implementation of most string-related type conversions
  2176. - removed all code from the compiler which has been replaced by
  2177. compilerproc implementations (using $ifdef hascompilerproc is not
  2178. necessary in the compiler)
  2179. Revision 1.53 2001/08/27 11:04:41 jonas
  2180. * avoid nonsense range error when using cardinal with value
  2181. > high(longint) as code para with val()
  2182. Revision 1.52 2001/08/26 13:36:40 florian
  2183. * some cg reorganisation
  2184. * some PPC updates
  2185. Revision 1.51 2001/08/24 13:47:27 jonas
  2186. * moved "reverseparameters" from ninl.pas to ncal.pas
  2187. + support for non-persistent temps in ttempcreatenode.create, for use
  2188. with typeconversion nodes
  2189. Revision 1.50 2001/08/24 12:33:54 jonas
  2190. * fixed big bug in handle_str that caused it to (almost) always call
  2191. fpc_<stringtype>_longint
  2192. * fixed small bug in handle_read_write that caused wrong warnigns about
  2193. uninitialized vars with read(ln)
  2194. + handle_val (processor independent val() handling)
  2195. Revision 1.49 2001/08/23 14:28:35 jonas
  2196. + tempcreate/ref/delete nodes (allows the use of temps in the
  2197. resulttype and first pass)
  2198. * made handling of read(ln)/write(ln) processor independent
  2199. * moved processor independent handling for str and reset/rewrite-typed
  2200. from firstpass to resulttype pass
  2201. * changed names of helpers in text.inc to be generic for use as
  2202. compilerprocs + added "iocheck" directive for most of them
  2203. * reading of ordinals is done by procedures instead of functions
  2204. because otherwise FPC_IOCHECK overwrote the result before it could
  2205. be stored elsewhere (range checking still works)
  2206. * compilerprocs can now be used in the system unit before they are
  2207. implemented
  2208. * added note to errore.msg that booleans can't be read using read/readln
  2209. Revision 1.48 2001/08/13 15:39:52 jonas
  2210. * made in_reset_typedfile/in_rewrite_typedfile handling processor
  2211. independent
  2212. Revision 1.47 2001/08/13 12:41:57 jonas
  2213. * made code for str(x,y) completely processor independent
  2214. Revision 1.46 2001/08/06 12:47:31 jonas
  2215. * parameters to FPC_TYPED_WRITE can't be regvars (merged)
  2216. Revision 1.45 2001/08/06 09:44:10 jonas
  2217. + support for high(dynarray) using compilerproc (forgot to commit
  2218. previously)
  2219. Revision 1.44 2001/07/09 21:15:40 peter
  2220. * Length made internal
  2221. * Add array support for Length
  2222. Revision 1.43 2001/07/08 21:00:15 peter
  2223. * various widestring updates, it works now mostly without charset
  2224. mapping supported
  2225. Revision 1.42 2001/06/04 11:48:01 peter
  2226. * better const to var checking
  2227. Revision 1.41 2001/06/03 20:12:53 peter
  2228. * changed int64($ffffffff) that is buggy under 1.0.x to expression
  2229. with a shl
  2230. Revision 1.40 2001/05/06 17:16:43 jonas
  2231. + added warning about missing implementation for high(dynamic_array)
  2232. Revision 1.39 2001/04/26 21:57:05 peter
  2233. * moved code from firstpass to det_resulttype and remove extraneous
  2234. calls to firstcallparan for in_str,in_write,in_val
  2235. Revision 1.38 2001/04/21 12:03:11 peter
  2236. * m68k updates merged from fixes branch
  2237. Revision 1.37 2001/04/13 22:22:30 peter
  2238. * call set_varstate for setlength
  2239. * ptr returns pointerconstnode instead of ordconstnode
  2240. Revision 1.36 2001/04/13 01:22:09 peter
  2241. * symtable change to classes
  2242. * range check generation and errors fixed, make cycle DEBUG=1 works
  2243. * memory leaks fixed
  2244. Revision 1.35 2001/04/05 21:02:13 peter
  2245. * fixed fpu inline functions typeconvs
  2246. Revision 1.34 2001/04/04 22:42:40 peter
  2247. * move constant folding into det_resulttype
  2248. Revision 1.33 2001/04/04 21:30:43 florian
  2249. * applied several fixes to get the DD8 Delphi Unit compiled
  2250. e.g. "forward"-interfaces are working now
  2251. Revision 1.32 2001/04/02 21:20:31 peter
  2252. * resulttype rewrite
  2253. Revision 1.31 2001/03/23 00:16:07 florian
  2254. + some stuff to compile FreeCLX added
  2255. Revision 1.30 2001/03/12 12:47:46 michael
  2256. + Patches from peter
  2257. Revision 1.29 2001/03/03 12:38:08 jonas
  2258. * fixed low() for signed types < 64bit
  2259. Revision 1.28 2001/02/26 19:44:53 peter
  2260. * merged generic m68k updates from fixes branch
  2261. Revision 1.27 2001/02/22 11:24:40 jonas
  2262. * fixed bug in previous fix (hopped over revision 1.26 because that one
  2263. also removed the fix for high(cardinal))
  2264. Revision 1.26 2001/02/21 20:50:59 peter
  2265. * fix to compile again, but high(cardinal) with $R+ still fails!
  2266. Revision 1.25 2001/02/21 12:57:46 jonas
  2267. * fixed high/low for cardinal, int64 and qword
  2268. Revision 1.24 2001/01/06 19:54:11 peter
  2269. * merged fix for 1310
  2270. Revision 1.23 2001/01/06 18:28:39 peter
  2271. * fixed wrong notes about locals
  2272. Revision 1.22 2000/12/31 11:14:10 jonas
  2273. + implemented/fixed docompare() mathods for all nodes (not tested)
  2274. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  2275. and constant strings/chars together
  2276. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  2277. when adding
  2278. Revision 1.21 2000/12/25 00:07:26 peter
  2279. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2280. tlinkedlist objects)
  2281. Revision 1.20 2000/12/17 14:35:41 peter
  2282. * fixed crash with val()
  2283. Revision 1.19 2000/11/29 00:30:33 florian
  2284. * unused units removed from uses clause
  2285. * some changes for widestrings
  2286. Revision 1.18 2000/11/12 15:27:22 jonas
  2287. * also don't do conversion for chars/booleans (hopefully final change :/)
  2288. Revision 1.17 2000/11/11 21:08:13 jonas
  2289. * don't do inc/dec to add/sub conversion for enums
  2290. Revision 1.16 2000/11/11 16:18:35 peter
  2291. * ptr returns farpointer
  2292. Revision 1.15 2000/11/11 15:59:07 jonas
  2293. * convert inc/dec to add/sub when range/overflow checking is on
  2294. Revision 1.14 2000/11/09 17:46:54 florian
  2295. * System.TypeInfo fixed
  2296. + System.Finalize implemented
  2297. + some new keywords for interface support added
  2298. Revision 1.13 2000/11/04 16:48:32 florian
  2299. * innr.inc renamed to make compiler compilation easier because the rtl contains
  2300. a file of the same name
  2301. Revision 1.12 2000/10/31 22:02:48 peter
  2302. * symtable splitted, no real code changes
  2303. Revision 1.11 2000/10/26 14:15:06 jonas
  2304. * fixed setlength for shortstrings
  2305. Revision 1.10 2000/10/21 18:16:11 florian
  2306. * a lot of changes:
  2307. - basic dyn. array support
  2308. - basic C++ support
  2309. - some work for interfaces done
  2310. ....
  2311. Revision 1.9 2000/10/15 08:38:46 jonas
  2312. * added missing getcopy for previous addition
  2313. Revision 1.8 2000/10/14 18:27:53 jonas
  2314. * merged fix for inc/dec on 64bit types from tcinl
  2315. Revision 1.7 2000/10/14 10:14:50 peter
  2316. * moehrendorf oct 2000 rewrite
  2317. Revision 1.6 2000/10/01 19:48:24 peter
  2318. * lot of compile updates for cg11
  2319. Revision 1.5 2000/09/28 19:49:52 florian
  2320. *** empty log message ***
  2321. Revision 1.4 2000/09/28 16:34:47 florian
  2322. *** empty log message ***
  2323. Revision 1.3 2000/09/27 21:33:22 florian
  2324. * finally nadd.pas compiles
  2325. Revision 1.2 2000/09/27 20:25:44 florian
  2326. * more stuff fixed
  2327. Revision 1.1 2000/09/26 14:59:34 florian
  2328. * more conversion work done
  2329. }