ninl.pas 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. {
  2. Copyright (c) 1998-2007 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,cpuinfo,symtype;
  22. {$i compinnr.inc}
  23. type
  24. tinlinenode = class(tunarynode)
  25. inlinenumber : byte;
  26. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  27. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  28. procedure ppuwrite(ppufile:tcompilerppufile);override;
  29. function dogetcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function pass_typecheck:tnode;override;
  32. function docompare(p: tnode): boolean; override;
  33. { pack and unpack are changed into for-loops by the compiler }
  34. function first_pack_unpack: tnode; virtual;
  35. { All the following routines currently
  36. call compilerprocs, unless they are
  37. overriden in which case, the code
  38. generator handles them.
  39. }
  40. function first_pi: tnode ; virtual;
  41. function first_arctan_real: tnode; virtual;
  42. function first_abs_real: tnode; virtual;
  43. function first_sqr_real: tnode; virtual;
  44. function first_sqrt_real: tnode; virtual;
  45. function first_ln_real: tnode; virtual;
  46. function first_cos_real: tnode; virtual;
  47. function first_sin_real: tnode; virtual;
  48. function first_exp_real: tnode; virtual;
  49. function first_frac_real: tnode; virtual;
  50. function first_round_real: tnode; virtual;
  51. function first_trunc_real: tnode; virtual;
  52. function first_int_real: tnode; virtual;
  53. private
  54. function handle_str: tnode;
  55. function handle_reset_rewrite_typed: tnode;
  56. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  57. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  58. function handle_read_write: tnode;
  59. function handle_val: tnode;
  60. end;
  61. tinlinenodeclass = class of tinlinenode;
  62. var
  63. cinlinenode : tinlinenodeclass;
  64. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  65. implementation
  66. uses
  67. verbose,globals,systems,
  68. globtype, cutils,
  69. symconst,symdef,symsym,symtable,paramgr,defutil,
  70. pass_1,
  71. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  72. cgbase,procinfo
  73. ;
  74. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  75. begin
  76. geninlinenode:=cinlinenode.create(number,is_const,l);
  77. end;
  78. {*****************************************************************************
  79. TINLINENODE
  80. *****************************************************************************}
  81. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  82. begin
  83. inherited create(inlinen,l);
  84. if is_const then
  85. include(flags,nf_inlineconst);
  86. inlinenumber:=number;
  87. end;
  88. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  89. begin
  90. inherited ppuload(t,ppufile);
  91. inlinenumber:=ppufile.getbyte;
  92. end;
  93. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  94. begin
  95. inherited ppuwrite(ppufile);
  96. ppufile.putbyte(inlinenumber);
  97. end;
  98. function tinlinenode.dogetcopy : tnode;
  99. var
  100. n : tinlinenode;
  101. begin
  102. n:=tinlinenode(inherited dogetcopy);
  103. n.inlinenumber:=inlinenumber;
  104. result:=n;
  105. end;
  106. function tinlinenode.handle_str : tnode;
  107. var
  108. lenpara,
  109. fracpara,
  110. newparas,
  111. tmppara,
  112. dest,
  113. source : tcallparanode;
  114. procname: string;
  115. is_real,is_enum : boolean;
  116. rt : aint;
  117. begin
  118. result := cerrornode.create;
  119. { make sure we got at least two parameters (if we got only one, }
  120. { this parameter may not be encapsulated in a callparan) }
  121. if not assigned(left) or
  122. (left.nodetype <> callparan) then
  123. begin
  124. CGMessage1(parser_e_wrong_parameter_size,'Str');
  125. exit;
  126. end;
  127. { get destination string }
  128. dest := tcallparanode(left);
  129. { get source para (number) }
  130. source := dest;
  131. while assigned(source.right) do
  132. source := tcallparanode(source.right);
  133. { destination parameter must be a normal (not a colon) parameter, this
  134. check is needed because str(v:len) also has 2 parameters }
  135. if (source=dest) or
  136. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  137. begin
  138. CGMessage1(parser_e_wrong_parameter_size,'Str');
  139. exit;
  140. end;
  141. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  142. is_enum:=source.left.resultdef.typ=enumdef;
  143. if ((dest.left.resultdef.typ<>stringdef) and
  144. not(is_chararray(dest.left.resultdef))) or
  145. not(is_real or is_enum or
  146. (source.left.resultdef.typ=orddef)) then
  147. begin
  148. CGMessagePos(fileinfo,parser_e_illegal_expression);
  149. exit;
  150. end;
  151. { get len/frac parameters }
  152. lenpara := nil;
  153. fracpara := nil;
  154. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  155. begin
  156. lenpara := tcallparanode(dest.right);
  157. { we can let the callnode do the type checking of these parameters too, }
  158. { but then the error messages aren't as nice }
  159. if not is_integer(lenpara.resultdef) then
  160. begin
  161. CGMessagePos1(lenpara.fileinfo,
  162. type_e_integer_expr_expected,lenpara.resultdef.typename);
  163. exit;
  164. end;
  165. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  166. begin
  167. { parameters are in reverse order! }
  168. fracpara := lenpara;
  169. lenpara := tcallparanode(lenpara.right);
  170. if not is_real then
  171. begin
  172. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  173. exit
  174. end;
  175. if not is_integer(lenpara.resultdef) then
  176. begin
  177. CGMessagePos1(lenpara.fileinfo,
  178. type_e_integer_expr_expected,lenpara.resultdef.typename);
  179. exit;
  180. end;
  181. end;
  182. end;
  183. { generate the parameter list for the compilerproc }
  184. newparas := dest;
  185. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  186. if is_real then
  187. begin
  188. { insert realtype parameter }
  189. if not is_currency(source.resultdef) then
  190. begin
  191. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  192. newparas.right := ccallparanode.create(cordconstnode.create(
  193. rt,s32inttype,true),newparas.right);
  194. tmppara:=tcallparanode(newparas.right);
  195. end
  196. else
  197. tmppara:=newparas;
  198. { if necessary, insert a fraction parameter }
  199. if not assigned(fracpara) then
  200. begin
  201. tmppara.right := ccallparanode.create(
  202. cordconstnode.create(-1,s32inttype,false),
  203. tmppara.right);
  204. fracpara := tcallparanode(tmppara.right);
  205. end;
  206. { if necessary, insert a length para }
  207. if not assigned(lenpara) then
  208. fracpara.right := ccallparanode.create(
  209. cordconstnode.create(-32767,s32inttype,false),
  210. fracpara.right);
  211. end
  212. else if is_enum then
  213. begin
  214. {Insert a reference to the ord2string index.}
  215. newparas.right:=Ccallparanode.create(
  216. Caddrnode.create_internal(
  217. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  218. ),
  219. newparas.right);
  220. {Insert a reference to the typinfo.}
  221. newparas.right:=Ccallparanode.create(
  222. Caddrnode.create_internal(
  223. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  224. ),
  225. newparas.right);
  226. {Insert a type conversion from the enumeration to longint.}
  227. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  228. typecheckpass(source.left);
  229. { if necessary, insert a length para }
  230. if not assigned(lenpara) then
  231. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  232. Ccallparanode.create(
  233. cordconstnode.create(-1,s32inttype,false),
  234. Tcallparanode(Tcallparanode(newparas.right).right).right
  235. );
  236. end
  237. else
  238. { for a normal parameter, insert a only length parameter if one is missing }
  239. if not assigned(lenpara) then
  240. newparas.right := ccallparanode.create(cordconstnode.create(-1,s32inttype,false),
  241. newparas.right);
  242. { remove the parameters from the original node so they won't get disposed, }
  243. { since they're reused }
  244. left := nil;
  245. { create procedure name }
  246. if is_chararray(dest.resultdef) then
  247. procname:='fpc_chararray_'
  248. else
  249. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  250. if is_real then
  251. if is_currency(source.resultdef) then
  252. procname := procname + 'currency'
  253. else
  254. procname := procname + 'float'
  255. else if is_enum then
  256. procname:=procname+'enum'
  257. else
  258. case torddef(source.resultdef).ordtype of
  259. {$ifdef cpu64bit}
  260. u64bit:
  261. procname := procname + 'uint';
  262. {$else}
  263. u32bit:
  264. procname := procname + 'uint';
  265. u64bit:
  266. procname := procname + 'qword';
  267. scurrency,
  268. s64bit:
  269. procname := procname + 'int64';
  270. {$endif}
  271. else
  272. procname := procname + 'sint';
  273. end;
  274. { free the errornode we generated in the beginning }
  275. result.free;
  276. { create the call node, }
  277. result := ccallnode.createintern(procname,newparas);
  278. end;
  279. function tinlinenode.handle_reset_rewrite_typed: tnode;
  280. begin
  281. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  282. { a typed file as argument and we don't have to check it again (JM) }
  283. { add the recsize parameter }
  284. { note: for some reason, the parameter of intern procedures with only one }
  285. { parameter is gets lifted out of its original tcallparanode (see round }
  286. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  287. left := ccallparanode.create(cordconstnode.create(
  288. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  289. ccallparanode.create(left,nil));
  290. { create the correct call }
  291. if inlinenumber=in_reset_typedfile then
  292. result := ccallnode.createintern('fpc_reset_typed',left)
  293. else
  294. result := ccallnode.createintern('fpc_rewrite_typed',left);
  295. { make sure left doesn't get disposed, since we use it in the new call }
  296. left := nil;
  297. end;
  298. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  299. {Read(ln)/write(ln) for text files.}
  300. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  301. var error_para,is_real,special_handling,found_error,do_read:boolean;
  302. p1:Tnode;
  303. nextpara,
  304. indexpara,
  305. lenpara,
  306. para,
  307. fracpara:Tcallparanode;
  308. temp:Ttempcreatenode;
  309. readfunctype:Tdef;
  310. name:string[31];
  311. begin
  312. para:=Tcallparanode(params);
  313. found_error:=false;
  314. do_read:=inlinenumber in [in_read_x,in_readln_x];
  315. while assigned(para) do
  316. begin
  317. { is this parameter faulty? }
  318. error_para:=false;
  319. { is this parameter a real? }
  320. is_real:=false;
  321. { type used for the read(), this is used to check
  322. whether a temp is needed for range checking }
  323. readfunctype:=nil;
  324. { can't read/write types }
  325. if para.left.nodetype=typen then
  326. begin
  327. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  328. error_para := true;
  329. end;
  330. { support writeln(procvar) }
  331. if para.left.resultdef.typ=procvardef then
  332. begin
  333. p1:=ccallnode.create_procvar(nil,para.left);
  334. typecheckpass(p1);
  335. para.left:=p1;
  336. end;
  337. case para.left.resultdef.typ of
  338. stringdef :
  339. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  340. pointerdef :
  341. begin
  342. if (not is_pchar(para.left.resultdef)) or do_read then
  343. begin
  344. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  345. error_para := true;
  346. end
  347. else
  348. name:=procprefixes[do_read]+'pchar_as_pointer';
  349. end;
  350. floatdef :
  351. begin
  352. is_real:=true;
  353. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  354. name := procprefixes[do_read]+'currency'
  355. else
  356. begin
  357. name := procprefixes[do_read]+'float';
  358. readfunctype:=pbestrealtype^;
  359. end;
  360. end;
  361. enumdef:
  362. begin
  363. name:=procprefixes[do_read]+'enum';
  364. readfunctype:=s32inttype;
  365. end;
  366. orddef :
  367. begin
  368. case Torddef(para.left.resultdef).ordtype of
  369. {$ifdef cpu64bit}
  370. s64bit,
  371. {$endif cpu64bit}
  372. s8bit,
  373. s16bit,
  374. s32bit :
  375. begin
  376. name := procprefixes[do_read]+'sint';
  377. readfunctype:=sinttype;
  378. end;
  379. {$ifdef cpu64bit}
  380. u64bit,
  381. {$endif cpu64bit}
  382. u8bit,
  383. u16bit,
  384. u32bit :
  385. begin
  386. name := procprefixes[do_read]+'uint';
  387. readfunctype:=uinttype;
  388. end;
  389. uchar :
  390. begin
  391. name := procprefixes[do_read]+'char';
  392. readfunctype:=cchartype;
  393. end;
  394. uwidechar :
  395. begin
  396. name := procprefixes[do_read]+'widechar';
  397. readfunctype:=cwidechartype;
  398. end;
  399. {$ifndef cpu64bit}
  400. s64bit :
  401. begin
  402. name := procprefixes[do_read]+'int64';
  403. readfunctype:=s64inttype;
  404. end;
  405. u64bit :
  406. begin
  407. name := procprefixes[do_read]+'qword';
  408. readfunctype:=u64inttype;
  409. end;
  410. {$endif cpu64bit}
  411. scurrency:
  412. begin
  413. name := procprefixes[do_read]+'currency'
  414. readfunctype:=s64currencytype;
  415. end;
  416. bool8bit,
  417. bool16bit,
  418. bool32bit,
  419. bool64bit:
  420. if do_read then
  421. begin
  422. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  423. error_para := true;
  424. end
  425. else
  426. begin
  427. name := procprefixes[do_read]+'boolean';
  428. readfunctype:=booltype;
  429. end
  430. else
  431. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  432. error_para := true;
  433. end;
  434. end;
  435. variantdef :
  436. name:=procprefixes[do_read]+'variant';
  437. arraydef :
  438. begin
  439. if is_chararray(para.left.resultdef) then
  440. name := procprefixes[do_read]+'pchar_as_array'
  441. else
  442. begin
  443. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  444. error_para := true;
  445. end
  446. end
  447. else
  448. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  449. error_para := true;
  450. end;
  451. { check for length/fractional colon para's }
  452. fracpara:=nil;
  453. lenpara:=nil;
  454. indexpara:=nil;
  455. if assigned(para.right) and
  456. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  457. begin
  458. lenpara := tcallparanode(para.right);
  459. if assigned(lenpara.right) and
  460. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  461. fracpara:=tcallparanode(lenpara.right);
  462. end;
  463. { get the next parameter now already, because we're going }
  464. { to muck around with the pointers }
  465. if assigned(fracpara) then
  466. nextpara := tcallparanode(fracpara.right)
  467. else if assigned(lenpara) then
  468. nextpara := tcallparanode(lenpara.right)
  469. else
  470. nextpara := tcallparanode(para.right);
  471. { check if a fracpara is allowed }
  472. if assigned(fracpara) and not is_real then
  473. begin
  474. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  475. error_para := true;
  476. end
  477. else if assigned(lenpara) and do_read then
  478. begin
  479. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  480. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  481. error_para := true;
  482. end;
  483. { adjust found_error }
  484. found_error := found_error or error_para;
  485. if not error_para then
  486. begin
  487. special_handling:=false;
  488. { create dummy frac/len para's if necessary }
  489. if not do_read then
  490. begin
  491. { difference in default value for floats and the rest :( }
  492. if not is_real then
  493. begin
  494. if not assigned(lenpara) then
  495. lenpara := ccallparanode.create(
  496. cordconstnode.create(0,s32inttype,false),nil)
  497. else
  498. { make sure we don't pass the successive }
  499. { parameters too. We also already have a }
  500. { reference to the next parameter in }
  501. { nextpara }
  502. lenpara.right := nil;
  503. end
  504. else
  505. begin
  506. if not assigned(lenpara) then
  507. lenpara := ccallparanode.create(
  508. cordconstnode.create(-32767,s32inttype,false),nil);
  509. { also create a default fracpara if necessary }
  510. if not assigned(fracpara) then
  511. fracpara := ccallparanode.create(
  512. cordconstnode.create(-1,s32inttype,false),nil);
  513. { add it to the lenpara }
  514. lenpara.right := fracpara;
  515. if not is_currency(para.left.resultdef) then
  516. begin
  517. { and add the realtype para (this also removes the link }
  518. { to any parameters coming after it) }
  519. fracpara.right := ccallparanode.create(
  520. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  521. s32inttype,true),nil);
  522. end;
  523. end;
  524. if para.left.resultdef.typ=enumdef then
  525. begin
  526. {To write(ln) an enum we need a some extra parameters.}
  527. {Insert a reference to the ord2string index.}
  528. indexpara:=Ccallparanode.create(
  529. Caddrnode.create_internal(
  530. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  531. ),
  532. nil);
  533. {Insert a reference to the typinfo.}
  534. indexpara:=Ccallparanode.create(
  535. Caddrnode.create_internal(
  536. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  537. ),
  538. indexpara);
  539. {Insert a type conversion to to convert the enum to longint.}
  540. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  541. typecheckpass(para.left);
  542. end;
  543. end
  544. else
  545. begin
  546. {To read(ln) an enum we need a an extra parameter.}
  547. if para.left.resultdef.typ=enumdef then
  548. begin
  549. {Insert a reference to the string2ord index.}
  550. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  551. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  552. ),nil);
  553. {Insert a type conversion to to convert the enum to longint.}
  554. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  555. typecheckpass(para.left);
  556. end;
  557. { special handling of reading small numbers, because the helpers }
  558. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  559. { use functions because then the call to FPC_IOCHECK destroys }
  560. { their result before we can store it }
  561. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  562. special_handling:=true;
  563. end;
  564. if special_handling then
  565. begin
  566. { create the parameter list: the temp ... }
  567. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  568. addstatement(Tstatementnode(newstatement),temp);
  569. { ... and the file }
  570. p1 := ccallparanode.create(ctemprefnode.create(temp),
  571. filepara.getcopy);
  572. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  573. { create the call to the helper }
  574. addstatement(Tstatementnode(newstatement),
  575. ccallnode.createintern(name,tcallparanode(p1)));
  576. { assign the result to the original var (this automatically }
  577. { takes care of range checking) }
  578. addstatement(Tstatementnode(newstatement),
  579. cassignmentnode.create(para.left,
  580. ctemprefnode.create(temp)));
  581. { release the temp location }
  582. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  583. { statement of para is used }
  584. para.left := nil;
  585. { free the enclosing tcallparanode, but not the }
  586. { parameters coming after it }
  587. para.right := nil;
  588. para.free;
  589. end
  590. else
  591. { read of non s/u-8/16bit, or a write }
  592. begin
  593. { add the filepara to the current parameter }
  594. para.right := filepara.getcopy;
  595. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  596. already linked with the lenpara if necessary).}
  597. if indexpara=nil then
  598. Tcallparanode(para.right).right:=lenpara
  599. else
  600. begin
  601. if lenpara=nil then
  602. Tcallparanode(para.right).right:=indexpara
  603. else
  604. begin
  605. Tcallparanode(para.right).right:=lenpara;
  606. lenpara.right:=indexpara;
  607. end;
  608. { indexpara.right:=lenpara;}
  609. end;
  610. { in case of writing a chararray, add whether it's }
  611. { zero-based }
  612. if para.left.resultdef.typ=arraydef then
  613. para := ccallparanode.create(cordconstnode.create(
  614. ord(tarraydef(para.left.resultdef).lowrange=0),booltype,false),para);
  615. { create the call statement }
  616. addstatement(Tstatementnode(newstatement),
  617. ccallnode.createintern(name,para));
  618. end
  619. end
  620. else
  621. { error_para = true }
  622. begin
  623. { free the parameter, since it isn't referenced anywhere anymore }
  624. para.right := nil;
  625. para.free;
  626. if assigned(lenpara) then
  627. begin
  628. lenpara.right := nil;
  629. lenpara.free;
  630. end;
  631. if assigned(fracpara) then
  632. begin
  633. fracpara.right := nil;
  634. fracpara.free;
  635. end;
  636. end;
  637. { process next parameter }
  638. para := nextpara;
  639. end;
  640. { if no error, add the write(ln)/read(ln) end calls }
  641. if not found_error then
  642. begin
  643. case inlinenumber of
  644. in_read_x:
  645. name:='fpc_read_end';
  646. in_write_x:
  647. name:='fpc_write_end';
  648. in_readln_x:
  649. name:='fpc_readln_end';
  650. in_writeln_x:
  651. name:='fpc_writeln_end';
  652. end;
  653. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara));
  654. end;
  655. handle_text_read_write:=found_error;
  656. end;
  657. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  658. {Read/write for typed files.}
  659. const procprefixes:array[boolean] of string[15]=('fpc_typed_write','fpc_typed_read');
  660. procnamesdisplay:array[boolean] of string[5] = ('Write','Read');
  661. var found_error,do_read:boolean;
  662. para,nextpara:Tcallparanode;
  663. p1:Tnode;
  664. temp:Ttempcreatenode;
  665. begin
  666. para:=Tcallparanode(params);
  667. do_read:=inlinenumber in [in_read_x,in_readln_x];
  668. { add the typesize to the filepara }
  669. if filepara.resultdef.typ=filedef then
  670. filepara.right := ccallparanode.create(cordconstnode.create(
  671. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  672. { check for "no parameters" (you need at least one extra para for typed files) }
  673. if not assigned(para) then
  674. begin
  675. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[do_read]);
  676. found_error := true;
  677. end;
  678. { process all parameters }
  679. while assigned(para) do
  680. begin
  681. { check if valid parameter }
  682. if para.left.nodetype=typen then
  683. begin
  684. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  685. found_error := true;
  686. end;
  687. { support writeln(procvar) }
  688. if (para.left.resultdef.typ=procvardef) then
  689. begin
  690. p1:=ccallnode.create_procvar(nil,para.left);
  691. typecheckpass(p1);
  692. para.left:=p1;
  693. end;
  694. if filepara.resultdef.typ=filedef then
  695. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  696. if assigned(para.right) and
  697. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  698. begin
  699. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  700. { skip all colon para's }
  701. nextpara := tcallparanode(tcallparanode(para.right).right);
  702. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  703. nextpara := tcallparanode(nextpara.right);
  704. found_error := true;
  705. end
  706. else
  707. { get next parameter }
  708. nextpara := tcallparanode(para.right);
  709. { When we have a call, we have a problem: you can't pass the }
  710. { result of a call as a formal const parameter. Solution: }
  711. { assign the result to a temp and pass this temp as parameter }
  712. { This is not very efficient, but write(typedfile,x) is }
  713. { already slow by itself anyway (no buffering) (JM) }
  714. { Actually, thge same goes for every non-simple expression }
  715. { (such as an addition, ...) -> put everything but load nodes }
  716. { into temps (JM) }
  717. { of course, this must only be allowed for writes!!! (JM) }
  718. if not(do_read) and (para.left.nodetype <> loadn) then
  719. begin
  720. { create temp for result }
  721. temp := ctempcreatenode.create(para.left.resultdef,
  722. para.left.resultdef.size,tt_persistent,false);
  723. addstatement(Tstatementnode(newstatement),temp);
  724. { assign result to temp }
  725. addstatement(Tstatementnode(newstatement),
  726. cassignmentnode.create(ctemprefnode.create(temp),
  727. para.left));
  728. { replace (reused) paranode with temp }
  729. para.left := ctemprefnode.create(temp);
  730. end;
  731. { add fileparameter }
  732. para.right := filepara.getcopy;
  733. { create call statment }
  734. { since the parameters are in the correct order, we have to insert }
  735. { the statements always at the end of the current block }
  736. addstatement(Tstatementnode(newstatement),
  737. Ccallnode.createintern(procprefixes[do_read],para
  738. ));
  739. { if we used a temp, free it }
  740. if para.left.nodetype = temprefn then
  741. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  742. { process next parameter }
  743. para := nextpara;
  744. end;
  745. { free the file parameter }
  746. filepara.free;
  747. handle_typed_read_write:=found_error;
  748. end;
  749. function tinlinenode.handle_read_write: tnode;
  750. var
  751. filepara,
  752. nextpara,
  753. params : tcallparanode;
  754. newstatement : tstatementnode;
  755. newblock : tblocknode;
  756. filetemp : Ttempcreatenode;
  757. name : string[31];
  758. textsym : ttypesym;
  759. readfunctype : tdef;
  760. is_typed,
  761. do_read,
  762. found_error : boolean;
  763. begin
  764. filepara := nil;
  765. is_typed := false;
  766. filetemp := nil;
  767. do_read := inlinenumber in [in_read_x,in_readln_x];
  768. { if we fail, we can quickly exit this way. We must generate something }
  769. { instead of the inline node, because firstpass will bomb with an }
  770. { internalerror if it encounters a read/write }
  771. result := cerrornode.create;
  772. { reverse the parameters (needed to get the colon parameters in the }
  773. { correct order when processing write(ln) }
  774. left := reverseparameters(tcallparanode(left));
  775. if assigned(left) then
  776. begin
  777. { check if we have a file parameter and if yes, what kind it is }
  778. filepara := tcallparanode(left);
  779. if (filepara.resultdef.typ=filedef) then
  780. begin
  781. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  782. begin
  783. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  784. exit;
  785. end
  786. else
  787. begin
  788. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  789. begin
  790. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  791. begin
  792. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  793. exit;
  794. end;
  795. is_typed := true;
  796. end
  797. end;
  798. end
  799. else
  800. filepara := nil;
  801. end;
  802. { create a blocknode in which the successive write/read statements will be }
  803. { put, since they belong together. Also create a dummy statement already to }
  804. { make inserting of additional statements easier }
  805. newblock:=internalstatements(newstatement);
  806. { if we don't have a filepara, create one containing the default }
  807. if not assigned(filepara) then
  808. begin
  809. { since the input/output variables are threadvars loading them into
  810. a temp once is faster. Create a temp which will hold a pointer to the file }
  811. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  812. addstatement(newstatement,filetemp);
  813. { make sure the resultdef of the temp (and as such of the }
  814. { temprefs coming after it) is set (necessary because the }
  815. { temprefs will be part of the filepara, of which we need }
  816. { the resultdef later on and temprefs can only be }
  817. { typecheckpassed if the resultdef of the temp is known) }
  818. typecheckpass(tnode(filetemp));
  819. { assign the address of the file to the temp }
  820. if do_read then
  821. name := 'input'
  822. else
  823. name := 'output';
  824. addstatement(newstatement,
  825. cassignmentnode.create(ctemprefnode.create(filetemp),
  826. ccallnode.createintern('fpc_get_'+name,nil)));
  827. { create a new fileparameter as follows: file_type(temp^) }
  828. { (so that we pass the value and not the address of the temp }
  829. { to the read/write routine) }
  830. textsym:=search_system_type('TEXT');
  831. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  832. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  833. end
  834. else
  835. { remove filepara from the parameter chain }
  836. begin
  837. left := filepara.right;
  838. filepara.right := nil;
  839. { the file para is a var parameter, but it must be valid already }
  840. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  841. { check if we should make a temp to store the result of a complex }
  842. { expression (better heuristics, anyone?) (JM) }
  843. if (filepara.left.nodetype <> loadn) then
  844. begin
  845. { create a temp which will hold a pointer to the file }
  846. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  847. { add it to the statements }
  848. addstatement(newstatement,filetemp);
  849. { make sure the resultdef of the temp (and as such of the }
  850. { temprefs coming after it) is set (necessary because the }
  851. { temprefs will be part of the filepara, of which we need }
  852. { the resultdef later on and temprefs can only be }
  853. { typecheckpassed if the resultdef of the temp is known) }
  854. typecheckpass(tnode(filetemp));
  855. { assign the address of the file to the temp }
  856. addstatement(newstatement,
  857. cassignmentnode.create(ctemprefnode.create(filetemp),
  858. caddrnode.create_internal(filepara.left)));
  859. typecheckpass(newstatement.left);
  860. { create a new fileparameter as follows: file_type(temp^) }
  861. { (so that we pass the value and not the address of the temp }
  862. { to the read/write routine) }
  863. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  864. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  865. { replace the old file para with the new one }
  866. filepara.left := nil;
  867. filepara.free;
  868. filepara := nextpara;
  869. end;
  870. end;
  871. { the resultdef of the filepara must be set since it's }
  872. { used below }
  873. filepara.get_paratype;
  874. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  875. { if something goes wrong or at the end of the procedure }
  876. { we're going to reuse the paranodes, so make sure they don't get freed }
  877. { twice }
  878. params:=Tcallparanode(left);
  879. left := nil;
  880. if is_typed then
  881. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),newstatement)
  882. else
  883. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),newstatement);
  884. { if we found an error, simply delete the generated blocknode }
  885. if found_error then
  886. newblock.free
  887. else
  888. begin
  889. { deallocate the temp for the file para if we used one }
  890. if assigned(filetemp) then
  891. addstatement(newstatement,ctempdeletenode.create(filetemp));
  892. { otherwise return the newly generated block of instructions, }
  893. { but first free the errornode we generated at the beginning }
  894. result.free;
  895. result := newblock
  896. end;
  897. end;
  898. function tinlinenode.handle_val: tnode;
  899. var
  900. procname,
  901. suffix : string[31];
  902. sourcepara,
  903. destpara,
  904. codepara,
  905. sizepara,
  906. newparas : tcallparanode;
  907. orgcode,tc : tnode;
  908. newstatement : tstatementnode;
  909. newblock : tblocknode;
  910. tempcode : ttempcreatenode;
  911. begin
  912. { for easy exiting if something goes wrong }
  913. result := cerrornode.create;
  914. { check the amount of parameters }
  915. if not(assigned(left)) or
  916. not(assigned(tcallparanode(left).right)) then
  917. begin
  918. CGMessage1(parser_e_wrong_parameter_size,'Val');
  919. exit;
  920. end;
  921. { reverse parameters for easier processing }
  922. left := reverseparameters(tcallparanode(left));
  923. { get the parameters }
  924. tempcode := nil;
  925. orgcode := nil;
  926. sizepara := nil;
  927. sourcepara := tcallparanode(left);
  928. destpara := tcallparanode(sourcepara.right);
  929. codepara := tcallparanode(destpara.right);
  930. { check if codepara is valid }
  931. if assigned(codepara) and
  932. (
  933. (codepara.resultdef.typ <> orddef)
  934. {$ifndef cpu64bit}
  935. or is_64bitint(codepara.resultdef)
  936. {$endif cpu64bit}
  937. ) then
  938. begin
  939. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  940. exit;
  941. end;
  942. { check if dest para is valid }
  943. if not(destpara.resultdef.typ in [orddef,floatdef,enumdef]) then
  944. begin
  945. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  946. exit;
  947. end;
  948. { we're going to reuse the exisiting para's, so make sure they }
  949. { won't be disposed }
  950. left := nil;
  951. { create the blocknode which will hold the generated statements + }
  952. { an initial dummy statement }
  953. newblock:=internalstatements(newstatement);
  954. { do we need a temp for code? Yes, if no code specified, or if }
  955. { code is not a 32bit parameter (we already checked whether the }
  956. { the code para, if specified, was an orddef) }
  957. if not assigned(codepara) or
  958. (codepara.resultdef.size<>sinttype.size) then
  959. begin
  960. tempcode := ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  961. addstatement(newstatement,tempcode);
  962. { set the resultdef of the temp (needed to be able to get }
  963. { the resultdef of the tempref used in the new code para) }
  964. typecheckpass(tnode(tempcode));
  965. { create a temp codepara, but save the original code para to }
  966. { assign the result to later on }
  967. if assigned(codepara) then
  968. begin
  969. orgcode := codepara.left;
  970. codepara.left := ctemprefnode.create(tempcode);
  971. end
  972. else
  973. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  974. { we need its resultdef later on }
  975. codepara.get_paratype;
  976. end
  977. else if (torddef(codepara.resultdef).ordtype = torddef(sinttype).ordtype) then
  978. { because code is a var parameter, it must match types exactly }
  979. { however, since it will return values in [0..255], both longints }
  980. { and cardinals are fine. Since the formal code para type is }
  981. { longint, insert a typecoversion to longint for cardinal para's }
  982. begin
  983. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  984. { make it explicit, oterwise you may get a nonsense range }
  985. { check error if the cardinal already contained a value }
  986. { > $7fffffff }
  987. codepara.get_paratype;
  988. end;
  989. { create the procedure name }
  990. procname := 'fpc_val_';
  991. case destpara.resultdef.typ of
  992. orddef:
  993. begin
  994. case torddef(destpara.resultdef).ordtype of
  995. {$ifdef cpu64bit}
  996. s64bit,
  997. {$endif cpu64bit}
  998. s8bit,
  999. s16bit,
  1000. s32bit:
  1001. begin
  1002. suffix := 'sint_';
  1003. { we also need a destsize para in this case }
  1004. sizepara := ccallparanode.create(cordconstnode.create
  1005. (destpara.resultdef.size,s32inttype,true),nil);
  1006. end;
  1007. {$ifdef cpu64bit}
  1008. u64bit,
  1009. {$endif cpu64bit}
  1010. u8bit,
  1011. u16bit,
  1012. u32bit:
  1013. suffix := 'uint_';
  1014. {$ifndef cpu64bit}
  1015. s64bit: suffix := 'int64_';
  1016. u64bit: suffix := 'qword_';
  1017. {$endif cpu64bit}
  1018. scurrency: suffix := 'currency_';
  1019. else
  1020. internalerror(200304225);
  1021. end;
  1022. end;
  1023. floatdef:
  1024. suffix:='real_';
  1025. enumdef:
  1026. begin
  1027. suffix:='enum_';
  1028. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1029. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1030. ),nil);
  1031. end;
  1032. end;
  1033. procname := procname + suffix;
  1034. { play a trick to have tcallnode handle invalid source parameters: }
  1035. { the shortstring-longint val routine by default }
  1036. if (sourcepara.resultdef.typ = stringdef) then
  1037. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1038. { zero-based arrays (of char) can be implicitely converted to ansistring }
  1039. else if is_zero_based_array(sourcepara.resultdef) then
  1040. procname := procname + 'ansistr'
  1041. else
  1042. procname := procname + 'shortstr';
  1043. { set up the correct parameters for the call: the code para... }
  1044. newparas := codepara;
  1045. { and the source para }
  1046. codepara.right := sourcepara;
  1047. { sizepara either contains nil if none is needed (which is ok, since }
  1048. { then the next statement severes any possible links with other paras }
  1049. { that sourcepara may have) or it contains the necessary size para and }
  1050. { its right field is nil }
  1051. sourcepara.right := sizepara;
  1052. { create the call and assign the result to dest (val helpers are functions).
  1053. Use a trick to prevent a type size mismatch warning to be generated by the
  1054. assignment node. First convert implicitly to the resultdef. This will insert
  1055. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1056. for the assignment node and therefor preventing the warning (PFV)
  1057. The implicit conversion is avoided for enums because implicit conversion between
  1058. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1059. possible. (DM).}
  1060. if destpara.resultdef.typ=enumdef then
  1061. tc:=ccallnode.createintern(procname,newparas)
  1062. else
  1063. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1064. addstatement(newstatement,cassignmentnode.create(
  1065. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1066. { dispose of the enclosing paranode of the destination }
  1067. destpara.left := nil;
  1068. destpara.right := nil;
  1069. destpara.free;
  1070. { check if we used a temp for code and whether we have to store }
  1071. { it to the real code parameter }
  1072. if assigned(orgcode) then
  1073. addstatement(newstatement,cassignmentnode.create(
  1074. orgcode,
  1075. ctypeconvnode.create_internal(
  1076. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1077. { release the temp if we allocated one }
  1078. if assigned(tempcode) then
  1079. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1080. { free the errornode }
  1081. result.free;
  1082. { and return it }
  1083. result := newblock;
  1084. end;
  1085. {$maxfpuregisters 0}
  1086. function getpi : bestreal;
  1087. begin
  1088. {$ifdef x86}
  1089. { x86 has pi in hardware }
  1090. result:=pi;
  1091. {$else x86}
  1092. {$ifdef cpuextended}
  1093. result:=MathPiExtended.Value;
  1094. {$else cpuextended}
  1095. result:=MathPi.Value;
  1096. {$endif cpuextended}
  1097. {$endif x86}
  1098. end;
  1099. function tinlinenode.pass_typecheck:tnode;
  1100. function do_lowhigh(def:tdef) : tnode;
  1101. var
  1102. v : tconstexprint;
  1103. enum : tenumsym;
  1104. hp : tnode;
  1105. begin
  1106. case def.typ of
  1107. orddef:
  1108. begin
  1109. set_varstate(left,vs_read,[]);
  1110. if inlinenumber=in_low_x then
  1111. v:=torddef(def).low
  1112. else
  1113. v:=torddef(def).high;
  1114. { low/high of torddef are longints, so we need special }
  1115. { handling for cardinal and 64bit types (JM) }
  1116. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  1117. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  1118. shl twice (PFV) }
  1119. case torddef(def).ordtype of
  1120. s64bit,scurrency :
  1121. begin
  1122. if (inlinenumber=in_low_x) then
  1123. v := int64($80000000) shl 32
  1124. else
  1125. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  1126. end;
  1127. u64bit :
  1128. begin
  1129. { we have to use a dirty trick for high(qword), }
  1130. { because it's bigger than high(tconstexprint) (JM) }
  1131. v := 0
  1132. end
  1133. else
  1134. begin
  1135. if not is_signed(def) then
  1136. v := cardinal(v);
  1137. end;
  1138. end;
  1139. hp:=cordconstnode.create(v,def,true);
  1140. typecheckpass(hp);
  1141. { fix high(qword) }
  1142. if (torddef(def).ordtype=u64bit) and
  1143. (inlinenumber = in_high_x) then
  1144. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  1145. do_lowhigh:=hp;
  1146. end;
  1147. enumdef:
  1148. begin
  1149. set_varstate(left,vs_read,[]);
  1150. enum:=tenumsym(tenumdef(def).firstenum);
  1151. v:=tenumdef(def).maxval;
  1152. if inlinenumber=in_high_x then
  1153. while assigned(enum) and (enum.value <> v) do
  1154. enum:=enum.nextenum;
  1155. if not assigned(enum) then
  1156. internalerror(309993)
  1157. else
  1158. hp:=genenumnode(enum);
  1159. do_lowhigh:=hp;
  1160. end;
  1161. else
  1162. internalerror(87);
  1163. end;
  1164. end;
  1165. function getconstrealvalue : bestreal;
  1166. begin
  1167. case left.nodetype of
  1168. ordconstn:
  1169. getconstrealvalue:=tordconstnode(left).value;
  1170. realconstn:
  1171. getconstrealvalue:=trealconstnode(left).value_real;
  1172. else
  1173. internalerror(309992);
  1174. end;
  1175. end;
  1176. procedure setconstrealvalue(r : bestreal);
  1177. begin
  1178. result:=crealconstnode.create(r,pbestrealtype^);
  1179. end;
  1180. function handle_ln_const(r : bestreal) : tnode;
  1181. begin
  1182. if r<=0.0 then
  1183. if (cs_check_range in current_settings.localswitches) or
  1184. (cs_check_overflow in current_settings.localswitches) then
  1185. begin
  1186. result:=crealconstnode.create(0,pbestrealtype^);
  1187. CGMessage(type_e_wrong_math_argument)
  1188. end
  1189. else
  1190. begin
  1191. if r=0.0 then
  1192. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1193. else
  1194. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1195. end
  1196. else
  1197. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1198. end;
  1199. function handle_sqrt_const(r : bestreal) : tnode;
  1200. begin
  1201. if r<0.0 then
  1202. if (cs_check_range in current_settings.localswitches) or
  1203. (cs_check_overflow in current_settings.localswitches) then
  1204. begin
  1205. result:=crealconstnode.create(0,pbestrealtype^);
  1206. CGMessage(type_e_wrong_math_argument)
  1207. end
  1208. else
  1209. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1210. else
  1211. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1212. end;
  1213. procedure setfloatresultdef;
  1214. begin
  1215. if (left.resultdef.typ=floatdef) and
  1216. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1217. resultdef:=left.resultdef
  1218. else
  1219. begin
  1220. inserttypeconv(left,pbestrealtype^);
  1221. resultdef:=pbestrealtype^;
  1222. end;
  1223. end;
  1224. procedure handle_pack_unpack;
  1225. var
  1226. source, target, index: tcallparanode;
  1227. unpackedarraydef, packedarraydef: tarraydef;
  1228. tempindex: TConstExprInt;
  1229. begin
  1230. resultdef:=voidtype;
  1231. unpackedarraydef := nil;
  1232. packedarraydef := nil;
  1233. source := tcallparanode(left);
  1234. if (inlinenumber = in_unpack_x_y_z) then
  1235. begin
  1236. target := tcallparanode(source.right);
  1237. index := tcallparanode(target.right);
  1238. { source must be a packed array }
  1239. if not is_packed_array(source.left.resultdef) then
  1240. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1241. else
  1242. packedarraydef := tarraydef(source.left.resultdef);
  1243. { target can be any kind of array, as long as it's not packed }
  1244. if (target.left.resultdef.typ <> arraydef) or
  1245. is_packed_array(target.left.resultdef) then
  1246. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1247. else
  1248. unpackedarraydef := tarraydef(target.left.resultdef);
  1249. end
  1250. else
  1251. begin
  1252. index := tcallparanode(source.right);
  1253. target := tcallparanode(index.right);
  1254. { source can be any kind of array, as long as it's not packed }
  1255. if (source.left.resultdef.typ <> arraydef) or
  1256. is_packed_array(source.left.resultdef) then
  1257. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1258. else
  1259. unpackedarraydef := tarraydef(source.left.resultdef);
  1260. { target must be a packed array }
  1261. if not is_packed_array(target.left.resultdef) then
  1262. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1263. else
  1264. packedarraydef := tarraydef(target.left.resultdef);
  1265. end;
  1266. if assigned(unpackedarraydef) then
  1267. begin
  1268. { index must be compatible with the unpacked array's indextype }
  1269. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1270. { range check at compile time if possible }
  1271. if assigned(packedarraydef) and
  1272. (index.left.nodetype = ordconstn) and
  1273. not is_special_array(unpackedarraydef) then
  1274. begin
  1275. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1276. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1277. testrange(unpackedarraydef,tempindex,false);
  1278. end;
  1279. end;
  1280. { source array is read and must be valid }
  1281. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1282. { target array is written }
  1283. valid_for_assignment(target.left,true);
  1284. set_varstate(target.left,vs_written,[]);
  1285. { index in the unpacked array is read and must be valid }
  1286. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1287. end;
  1288. var
  1289. vl,vl2 : TConstExprInt;
  1290. vr : bestreal;
  1291. hightree,
  1292. hp : tnode;
  1293. checkrange : boolean;
  1294. label
  1295. myexit;
  1296. begin
  1297. result:=nil;
  1298. { if we handle writeln; left contains no valid address }
  1299. if assigned(left) then
  1300. begin
  1301. if left.nodetype=callparan then
  1302. tcallparanode(left).get_paratype
  1303. else
  1304. typecheckpass(left);
  1305. end;
  1306. inc(parsing_para_level);
  1307. { handle intern constant functions in separate case }
  1308. if nf_inlineconst in flags then
  1309. begin
  1310. { no parameters? }
  1311. if not assigned(left) then
  1312. internalerror(200501231)
  1313. else
  1314. begin
  1315. vl:=0;
  1316. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1317. case left.nodetype of
  1318. realconstn :
  1319. begin
  1320. { Real functions are all handled with internproc below }
  1321. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1322. end;
  1323. ordconstn :
  1324. vl:=tordconstnode(left).value;
  1325. callparan :
  1326. begin
  1327. { both exists, else it was not generated }
  1328. vl:=tordconstnode(tcallparanode(left).left).value;
  1329. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1330. end;
  1331. else
  1332. CGMessage(parser_e_illegal_expression);
  1333. end;
  1334. case inlinenumber of
  1335. in_const_abs :
  1336. hp:=genintconstnode(abs(vl));
  1337. in_const_sqr :
  1338. hp:=genintconstnode(sqr(vl));
  1339. in_const_odd :
  1340. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1341. in_const_swap_word :
  1342. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1343. in_const_swap_long :
  1344. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1345. in_const_swap_qword :
  1346. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1347. in_const_ptr :
  1348. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1349. else
  1350. internalerror(88);
  1351. end;
  1352. end;
  1353. if hp=nil then
  1354. hp:=cerrornode.create;
  1355. result:=hp;
  1356. goto myexit;
  1357. end
  1358. else
  1359. begin
  1360. case inlinenumber of
  1361. in_lo_long,
  1362. in_hi_long,
  1363. in_lo_qword,
  1364. in_hi_qword,
  1365. in_lo_word,
  1366. in_hi_word :
  1367. begin
  1368. { give warning for incompatibility with tp and delphi }
  1369. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1370. ((m_tp7 in current_settings.modeswitches) or
  1371. (m_delphi in current_settings.modeswitches)) then
  1372. CGMessage(type_w_maybe_wrong_hi_lo);
  1373. { constant folding }
  1374. if left.nodetype=ordconstn then
  1375. begin
  1376. case inlinenumber of
  1377. in_lo_word :
  1378. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resultdef,true);
  1379. in_hi_word :
  1380. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resultdef,true);
  1381. in_lo_long :
  1382. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resultdef,true);
  1383. in_hi_long :
  1384. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resultdef,true);
  1385. in_lo_qword :
  1386. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resultdef,true);
  1387. in_hi_qword :
  1388. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resultdef,true);
  1389. end;
  1390. result:=hp;
  1391. goto myexit;
  1392. end;
  1393. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1394. if not is_integer(left.resultdef) then
  1395. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1396. case inlinenumber of
  1397. in_lo_word,
  1398. in_hi_word :
  1399. resultdef:=u8inttype;
  1400. in_lo_long,
  1401. in_hi_long :
  1402. resultdef:=u16inttype;
  1403. in_lo_qword,
  1404. in_hi_qword :
  1405. resultdef:=u32inttype;
  1406. end;
  1407. end;
  1408. in_sizeof_x:
  1409. begin
  1410. set_varstate(left,vs_read,[]);
  1411. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1412. begin
  1413. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1414. if assigned(hightree) then
  1415. begin
  1416. hp:=caddnode.create(addn,hightree,
  1417. cordconstnode.create(1,sinttype,false));
  1418. if (left.resultdef.typ=arraydef) then
  1419. if not is_packed_array(tarraydef(left.resultdef)) then
  1420. begin
  1421. if (tarraydef(left.resultdef).elesize<>1) then
  1422. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1423. left.resultdef).elesize,sinttype,true));
  1424. end
  1425. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1426. begin
  1427. { no packed open array support yet }
  1428. if (hp.nodetype <> ordconstn) then
  1429. internalerror(2006081511);
  1430. hp.free;
  1431. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1432. {
  1433. hp:=
  1434. ctypeconvnode.create_explicit(sinttype,
  1435. cmoddivnode.create(divn,
  1436. caddnode.create(addn,
  1437. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1438. left.resultdef).elepackedbitsize,s64inttype,true)),
  1439. cordconstnode.create(a,s64inttype,true)),
  1440. cordconstnode.create(8,s64inttype,true)),
  1441. sinttype);
  1442. }
  1443. end;
  1444. result:=hp;
  1445. end;
  1446. end
  1447. else
  1448. resultdef:=sinttype;
  1449. end;
  1450. in_typeof_x:
  1451. begin
  1452. set_varstate(left,vs_read,[]);
  1453. resultdef:=voidpointertype;
  1454. end;
  1455. in_ord_x:
  1456. begin
  1457. if (left.nodetype=ordconstn) then
  1458. begin
  1459. hp:=cordconstnode.create(
  1460. tordconstnode(left).value,sinttype,true);
  1461. result:=hp;
  1462. goto myexit;
  1463. end;
  1464. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1465. case left.resultdef.typ of
  1466. orddef :
  1467. begin
  1468. case torddef(left.resultdef).ordtype of
  1469. bool8bit,
  1470. uchar:
  1471. begin
  1472. { change to byte() }
  1473. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1474. left:=nil;
  1475. result:=hp;
  1476. end;
  1477. bool16bit,
  1478. uwidechar :
  1479. begin
  1480. { change to word() }
  1481. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1482. left:=nil;
  1483. result:=hp;
  1484. end;
  1485. bool32bit :
  1486. begin
  1487. { change to dword() }
  1488. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1489. left:=nil;
  1490. result:=hp;
  1491. end;
  1492. bool64bit :
  1493. begin
  1494. { change to qword() }
  1495. hp:=ctypeconvnode.create_internal(left,u64inttype);
  1496. left:=nil;
  1497. result:=hp;
  1498. end;
  1499. uvoid :
  1500. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1501. else
  1502. begin
  1503. { all other orddef need no transformation }
  1504. hp:=left;
  1505. left:=nil;
  1506. result:=hp;
  1507. end;
  1508. end;
  1509. end;
  1510. enumdef :
  1511. begin
  1512. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1513. left:=nil;
  1514. result:=hp;
  1515. end;
  1516. pointerdef :
  1517. begin
  1518. if m_mac in current_settings.modeswitches then
  1519. begin
  1520. hp:=ctypeconvnode.create_internal(left,ptruinttype);
  1521. left:=nil;
  1522. result:=hp;
  1523. end
  1524. else
  1525. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1526. end
  1527. else
  1528. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1529. end;
  1530. end;
  1531. in_chr_byte:
  1532. begin
  1533. { convert to explicit char() }
  1534. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1535. hp:=ctypeconvnode.create_internal(left,cchartype);
  1536. left:=nil;
  1537. result:=hp;
  1538. end;
  1539. in_length_x:
  1540. begin
  1541. if ((left.resultdef.typ=arraydef) and
  1542. not is_special_array(left.resultdef)) or
  1543. (left.resultdef.typ=orddef) then
  1544. set_varstate(left,vs_read,[])
  1545. else
  1546. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1547. case left.resultdef.typ of
  1548. variantdef:
  1549. begin
  1550. inserttypeconv(left,cansistringtype);
  1551. end;
  1552. stringdef :
  1553. begin
  1554. { we don't need string convertions here }
  1555. if (left.nodetype=typeconvn) and
  1556. (ttypeconvnode(left).left.resultdef.typ=stringdef) then
  1557. begin
  1558. hp:=ttypeconvnode(left).left;
  1559. ttypeconvnode(left).left:=nil;
  1560. left.free;
  1561. left:=hp;
  1562. end;
  1563. { evaluates length of constant strings direct }
  1564. if (left.nodetype=stringconstn) then
  1565. begin
  1566. hp:=cordconstnode.create(
  1567. tstringconstnode(left).len,s32inttype,true);
  1568. result:=hp;
  1569. goto myexit;
  1570. end;
  1571. end;
  1572. orddef :
  1573. begin
  1574. { length of char is one allways }
  1575. if is_char(left.resultdef) or
  1576. is_widechar(left.resultdef) then
  1577. begin
  1578. hp:=cordconstnode.create(1,s32inttype,false);
  1579. result:=hp;
  1580. goto myexit;
  1581. end
  1582. else
  1583. CGMessage(type_e_mismatch);
  1584. end;
  1585. pointerdef :
  1586. begin
  1587. if is_pchar(left.resultdef) then
  1588. begin
  1589. hp := ccallparanode.create(left,nil);
  1590. result := ccallnode.createintern('fpc_pchar_length',hp);
  1591. { make sure the left node doesn't get disposed, since it's }
  1592. { reused in the new node (JM) }
  1593. left:=nil;
  1594. goto myexit;
  1595. end
  1596. else if is_pwidechar(left.resultdef) then
  1597. begin
  1598. hp := ccallparanode.create(left,nil);
  1599. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1600. { make sure the left node doesn't get disposed, since it's }
  1601. { reused in the new node (JM) }
  1602. left:=nil;
  1603. goto myexit;
  1604. end
  1605. else
  1606. CGMessage(type_e_mismatch);
  1607. end;
  1608. arraydef :
  1609. begin
  1610. if is_open_array(left.resultdef) or
  1611. is_array_of_const(left.resultdef) then
  1612. begin
  1613. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1614. if assigned(hightree) then
  1615. begin
  1616. hp:=caddnode.create(addn,hightree,
  1617. cordconstnode.create(1,s32inttype,false));
  1618. result:=hp;
  1619. end;
  1620. goto myexit;
  1621. end
  1622. else
  1623. if not is_dynamic_array(left.resultdef) then
  1624. begin
  1625. hp:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1626. tarraydef(left.resultdef).lowrange+1,
  1627. s32inttype,true);
  1628. result:=hp;
  1629. goto myexit;
  1630. end
  1631. else
  1632. begin
  1633. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1634. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1635. { make sure the left node doesn't get disposed, since it's }
  1636. { reused in the new node (JM) }
  1637. left:=nil;
  1638. goto myexit;
  1639. end;
  1640. end;
  1641. else
  1642. CGMessage(type_e_mismatch);
  1643. end;
  1644. { shortstring return an 8 bit value as the length
  1645. is the first byte of the string }
  1646. if is_shortstring(left.resultdef) then
  1647. resultdef:=u8inttype
  1648. else
  1649. resultdef:=sinttype;
  1650. end;
  1651. in_typeinfo_x:
  1652. begin
  1653. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1654. resultdef:=voidpointertype;
  1655. end;
  1656. in_assigned_x:
  1657. begin
  1658. { the parser has already made sure the expression is valid }
  1659. { handle constant expressions }
  1660. if is_constnode(tcallparanode(left).left) or
  1661. (tcallparanode(left).left.nodetype = pointerconstn) then
  1662. begin
  1663. { let an add node figure it out }
  1664. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1665. tcallparanode(left).left := nil;
  1666. { free left, because otherwise some code at 'myexit' tries }
  1667. { to run get_paratype for it, which crashes since left.left }
  1668. { is now nil }
  1669. left.free;
  1670. left := nil;
  1671. goto myexit;
  1672. end;
  1673. { otherwise handle separately, because there could be a procvar, which }
  1674. { is 2*sizeof(pointer), while we must only check the first pointer }
  1675. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1676. resultdef:=booltype;
  1677. end;
  1678. in_ofs_x :
  1679. internalerror(2000101001);
  1680. in_seg_x :
  1681. begin
  1682. set_varstate(left,vs_read,[]);
  1683. result:=cordconstnode.create(0,s32inttype,false);
  1684. goto myexit;
  1685. end;
  1686. in_pred_x,
  1687. in_succ_x:
  1688. begin
  1689. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1690. resultdef:=left.resultdef;
  1691. if not is_ordinal(resultdef) then
  1692. CGMessage(type_e_ordinal_expr_expected)
  1693. else
  1694. begin
  1695. if (resultdef.typ=enumdef) and
  1696. (tenumdef(resultdef).has_jumps) and
  1697. not(m_delphi in current_settings.modeswitches) then
  1698. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1699. end;
  1700. { only if the result is an enum do we do range checking }
  1701. if (resultdef.typ=enumdef) then
  1702. checkrange := true
  1703. else
  1704. checkrange := false;
  1705. { do constant folding after check for jumps }
  1706. if left.nodetype=ordconstn then
  1707. begin
  1708. if inlinenumber=in_succ_x then
  1709. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1710. else
  1711. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1712. end;
  1713. end;
  1714. in_initialize_x,
  1715. in_finalize_x,
  1716. in_setlength_x:
  1717. begin
  1718. { inlined from pinline }
  1719. internalerror(200204231);
  1720. end;
  1721. in_inc_x,
  1722. in_dec_x:
  1723. begin
  1724. resultdef:=voidtype;
  1725. if assigned(left) then
  1726. begin
  1727. { first param must be var }
  1728. valid_for_var(tcallparanode(left).left,true);
  1729. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1730. if (left.resultdef.typ in [enumdef,pointerdef]) or
  1731. is_ordinal(left.resultdef) or
  1732. is_currency(left.resultdef) then
  1733. begin
  1734. { value of left gets changed -> must be unique }
  1735. set_unique(tcallparanode(left).left);
  1736. { two paras ? }
  1737. if assigned(tcallparanode(left).right) then
  1738. begin
  1739. if is_integer(tcallparanode(left).right.resultdef) then
  1740. begin
  1741. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1742. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  1743. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1744. { should be handled in the parser (JM) }
  1745. internalerror(2006020901);
  1746. end
  1747. else
  1748. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1749. end;
  1750. end
  1751. else
  1752. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1753. end
  1754. else
  1755. CGMessagePos(fileinfo,type_e_mismatch);
  1756. end;
  1757. in_read_x,
  1758. in_readln_x,
  1759. in_write_x,
  1760. in_writeln_x :
  1761. begin
  1762. result := handle_read_write;
  1763. end;
  1764. in_settextbuf_file_x :
  1765. begin
  1766. resultdef:=voidtype;
  1767. { now we know the type of buffer }
  1768. hp:=ccallparanode.create(cordconstnode.create(
  1769. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  1770. result:=ccallnode.createintern('SETTEXTBUF',hp);
  1771. left:=nil;
  1772. end;
  1773. { the firstpass of the arg has been done in firstcalln ? }
  1774. in_reset_typedfile,
  1775. in_rewrite_typedfile :
  1776. begin
  1777. result := handle_reset_rewrite_typed;
  1778. end;
  1779. in_str_x_string :
  1780. begin
  1781. result := handle_str;
  1782. end;
  1783. in_val_x :
  1784. begin
  1785. result := handle_val;
  1786. end;
  1787. in_include_x_y,
  1788. in_exclude_x_y:
  1789. begin
  1790. resultdef:=voidtype;
  1791. { the parser already checks whether we have two (and exactly two) }
  1792. { parameters (JM) }
  1793. { first param must be var }
  1794. valid_for_var(tcallparanode(left).left,true);
  1795. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1796. { check type }
  1797. if (left.resultdef.typ=setdef) then
  1798. begin
  1799. { insert a type conversion }
  1800. { to the type of the set elements }
  1801. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1802. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1803. tsetdef(left.resultdef).elementdef);
  1804. end
  1805. else
  1806. CGMessage(type_e_mismatch);
  1807. end;
  1808. in_pack_x_y_z,
  1809. in_unpack_x_y_z :
  1810. begin
  1811. handle_pack_unpack;
  1812. end;
  1813. in_slice_x:
  1814. begin
  1815. result:=nil;
  1816. resultdef:=tcallparanode(left).left.resultdef;
  1817. if (resultdef.typ <> arraydef) then
  1818. CGMessagePos(left.fileinfo,type_e_mismatch)
  1819. else if is_packed_array(resultdef) then
  1820. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  1821. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  1822. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1823. type_e_integer_expr_expected,
  1824. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  1825. end;
  1826. in_low_x,
  1827. in_high_x:
  1828. begin
  1829. case left.resultdef.typ of
  1830. orddef,
  1831. enumdef:
  1832. begin
  1833. result:=do_lowhigh(left.resultdef);
  1834. end;
  1835. setdef:
  1836. begin
  1837. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1838. end;
  1839. arraydef:
  1840. begin
  1841. if inlinenumber=in_low_x then
  1842. begin
  1843. set_varstate(left,vs_read,[]);
  1844. result:=cordconstnode.create(tarraydef(
  1845. left.resultdef).lowrange,tarraydef(left.resultdef).rangedef,true);
  1846. end
  1847. else
  1848. begin
  1849. if is_open_array(left.resultdef) or
  1850. is_array_of_const(left.resultdef) then
  1851. begin
  1852. set_varstate(left,vs_read,[]);
  1853. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1854. end
  1855. else
  1856. if is_dynamic_array(left.resultdef) then
  1857. begin
  1858. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1859. { can't use inserttypeconv because we need }
  1860. { an explicit type conversion (JM) }
  1861. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1862. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1863. { make sure the left node doesn't get disposed, since it's }
  1864. { reused in the new node (JM) }
  1865. left:=nil;
  1866. end
  1867. else
  1868. begin
  1869. set_varstate(left,vs_read,[]);
  1870. result:=cordconstnode.create(tarraydef(
  1871. left.resultdef).highrange,tarraydef(left.resultdef).rangedef,true);
  1872. end;
  1873. end;
  1874. end;
  1875. stringdef:
  1876. begin
  1877. if inlinenumber=in_low_x then
  1878. begin
  1879. result:=cordconstnode.create(0,u8inttype,false);
  1880. end
  1881. else
  1882. begin
  1883. if is_open_string(left.resultdef) then
  1884. begin
  1885. set_varstate(left,vs_read,[]);
  1886. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1887. end
  1888. else
  1889. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true);
  1890. end;
  1891. end;
  1892. else
  1893. CGMessage(type_e_mismatch);
  1894. end;
  1895. end;
  1896. in_exp_real :
  1897. begin
  1898. if left.nodetype in [ordconstn,realconstn] then
  1899. begin
  1900. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1901. if (trealconstnode(result).value_real=MathInf.Value) and
  1902. ((cs_check_range in current_settings.localswitches) or
  1903. (cs_check_overflow in current_settings.localswitches)) then
  1904. begin
  1905. result:=crealconstnode.create(0,pbestrealtype^);
  1906. CGMessage(parser_e_range_check_error);
  1907. end;
  1908. end
  1909. else
  1910. begin
  1911. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1912. inserttypeconv(left,pbestrealtype^);
  1913. resultdef:=pbestrealtype^;
  1914. end;
  1915. end;
  1916. in_trunc_real :
  1917. begin
  1918. if left.nodetype in [ordconstn,realconstn] then
  1919. begin
  1920. vr:=getconstrealvalue;
  1921. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1922. begin
  1923. CGMessage(parser_e_range_check_error);
  1924. result:=cordconstnode.create(1,s64inttype,false)
  1925. end
  1926. else
  1927. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1928. end
  1929. else
  1930. begin
  1931. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1932. inserttypeconv(left,pbestrealtype^);
  1933. resultdef:=s64inttype;
  1934. end;
  1935. end;
  1936. in_round_real :
  1937. begin
  1938. if left.nodetype in [ordconstn,realconstn] then
  1939. begin
  1940. vr:=getconstrealvalue;
  1941. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1942. begin
  1943. CGMessage(parser_e_range_check_error);
  1944. result:=cordconstnode.create(1,s64inttype,false)
  1945. end
  1946. else
  1947. result:=cordconstnode.create(round(vr),s64inttype,true)
  1948. end
  1949. else
  1950. begin
  1951. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1952. inserttypeconv(left,pbestrealtype^);
  1953. resultdef:=s64inttype;
  1954. end;
  1955. end;
  1956. in_frac_real :
  1957. begin
  1958. if left.nodetype in [ordconstn,realconstn] then
  1959. setconstrealvalue(frac(getconstrealvalue))
  1960. else
  1961. begin
  1962. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1963. inserttypeconv(left,pbestrealtype^);
  1964. resultdef:=pbestrealtype^;
  1965. end;
  1966. end;
  1967. in_int_real :
  1968. begin
  1969. if left.nodetype in [ordconstn,realconstn] then
  1970. setconstrealvalue(int(getconstrealvalue))
  1971. else
  1972. begin
  1973. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1974. inserttypeconv(left,pbestrealtype^);
  1975. resultdef:=pbestrealtype^;
  1976. end;
  1977. end;
  1978. in_pi_real :
  1979. begin
  1980. if block_type=bt_const then
  1981. setconstrealvalue(getpi)
  1982. else
  1983. resultdef:=pbestrealtype^;
  1984. end;
  1985. in_cos_real :
  1986. begin
  1987. if left.nodetype in [ordconstn,realconstn] then
  1988. setconstrealvalue(cos(getconstrealvalue))
  1989. else
  1990. begin
  1991. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1992. inserttypeconv(left,pbestrealtype^);
  1993. resultdef:=pbestrealtype^;
  1994. end;
  1995. end;
  1996. in_sin_real :
  1997. begin
  1998. if left.nodetype in [ordconstn,realconstn] then
  1999. setconstrealvalue(sin(getconstrealvalue))
  2000. else
  2001. begin
  2002. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2003. inserttypeconv(left,pbestrealtype^);
  2004. resultdef:=pbestrealtype^;
  2005. end;
  2006. end;
  2007. in_arctan_real :
  2008. begin
  2009. if left.nodetype in [ordconstn,realconstn] then
  2010. setconstrealvalue(arctan(getconstrealvalue))
  2011. else
  2012. begin
  2013. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2014. inserttypeconv(left,pbestrealtype^);
  2015. resultdef:=pbestrealtype^;
  2016. end;
  2017. end;
  2018. in_abs_real :
  2019. begin
  2020. if left.nodetype in [ordconstn,realconstn] then
  2021. setconstrealvalue(abs(getconstrealvalue))
  2022. else
  2023. begin
  2024. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2025. inserttypeconv(left,pbestrealtype^);
  2026. resultdef:=pbestrealtype^;
  2027. end;
  2028. end;
  2029. in_sqr_real :
  2030. begin
  2031. if left.nodetype in [ordconstn,realconstn] then
  2032. setconstrealvalue(sqr(getconstrealvalue))
  2033. else
  2034. begin
  2035. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2036. setfloatresultdef;
  2037. end;
  2038. end;
  2039. in_sqrt_real :
  2040. begin
  2041. if left.nodetype in [ordconstn,realconstn] then
  2042. begin
  2043. vr:=getconstrealvalue;
  2044. if vr<0.0 then
  2045. result:=handle_sqrt_const(vr)
  2046. else
  2047. setconstrealvalue(sqrt(vr));
  2048. end
  2049. else
  2050. begin
  2051. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2052. setfloatresultdef;
  2053. end;
  2054. end;
  2055. in_ln_real :
  2056. begin
  2057. if left.nodetype in [ordconstn,realconstn] then
  2058. begin
  2059. vr:=getconstrealvalue;
  2060. if vr<=0.0 then
  2061. result:=handle_ln_const(vr)
  2062. else
  2063. setconstrealvalue(ln(vr));
  2064. end
  2065. else
  2066. begin
  2067. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2068. inserttypeconv(left,pbestrealtype^);
  2069. resultdef:=pbestrealtype^;
  2070. end;
  2071. end;
  2072. {$ifdef SUPPORT_MMX}
  2073. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2074. begin
  2075. end;
  2076. {$endif SUPPORT_MMX}
  2077. in_prefetch_var:
  2078. begin
  2079. resultdef:=voidtype;
  2080. end;
  2081. {$ifdef SUPPORT_UNALIGNED}
  2082. in_unaligned_x:
  2083. begin
  2084. resultdef:=left.resultdef;
  2085. end;
  2086. {$endif SUPPORT_UNALIGNED}
  2087. in_assert_x_y :
  2088. begin
  2089. resultdef:=voidtype;
  2090. if assigned(left) then
  2091. begin
  2092. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2093. { check type }
  2094. if is_boolean(left.resultdef) then
  2095. begin
  2096. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2097. { must always be a string }
  2098. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2099. end
  2100. else
  2101. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2102. end
  2103. else
  2104. CGMessage(type_e_mismatch);
  2105. { We've checked the whole statement for correctness, now we
  2106. can remove it if assertions are off }
  2107. if not(cs_do_assertion in current_settings.localswitches) then
  2108. begin
  2109. { we need a valid node, so insert a nothingn }
  2110. result:=cnothingnode.create;
  2111. end
  2112. else
  2113. include(current_procinfo.flags,pi_do_call);
  2114. end;
  2115. in_get_frame,
  2116. in_get_caller_frame,
  2117. in_get_caller_addr:
  2118. begin
  2119. resultdef:=voidpointertype;
  2120. end;
  2121. else
  2122. internalerror(8);
  2123. end;
  2124. end;
  2125. myexit:
  2126. { Run get_paratype again to update maybe inserted typeconvs }
  2127. if not codegenerror then
  2128. begin
  2129. if assigned(left) and
  2130. (left.nodetype=callparan) then
  2131. tcallparanode(left).get_paratype;
  2132. end;
  2133. dec(parsing_para_level);
  2134. end;
  2135. function tinlinenode.pass_1 : tnode;
  2136. var
  2137. hp,hpp,resultnode : tnode;
  2138. shiftconst: longint;
  2139. tempnode: ttempcreatenode;
  2140. newstatement: tstatementnode;
  2141. newblock: tblocknode;
  2142. begin
  2143. result:=nil;
  2144. { if we handle writeln; left contains no valid address }
  2145. if assigned(left) then
  2146. begin
  2147. if left.nodetype=callparan then
  2148. tcallparanode(left).firstcallparan
  2149. else
  2150. firstpass(left);
  2151. left_max;
  2152. end;
  2153. inc(parsing_para_level);
  2154. { intern const should already be handled }
  2155. if nf_inlineconst in flags then
  2156. internalerror(200104044);
  2157. case inlinenumber of
  2158. in_lo_qword,
  2159. in_hi_qword,
  2160. in_lo_long,
  2161. in_hi_long,
  2162. in_lo_word,
  2163. in_hi_word:
  2164. begin
  2165. shiftconst := 0;
  2166. case inlinenumber of
  2167. in_hi_qword:
  2168. shiftconst := 32;
  2169. in_hi_long:
  2170. shiftconst := 16;
  2171. in_hi_word:
  2172. shiftconst := 8;
  2173. end;
  2174. if shiftconst <> 0 then
  2175. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2176. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2177. else
  2178. result := ctypeconvnode.create_internal(left,resultdef);
  2179. left := nil;
  2180. firstpass(result);
  2181. end;
  2182. in_sizeof_x:
  2183. begin
  2184. if registersint<1 then
  2185. registersint:=1;
  2186. expectloc:=LOC_REGISTER;
  2187. end;
  2188. in_typeof_x:
  2189. begin
  2190. if registersint<1 then
  2191. registersint:=1;
  2192. expectloc:=LOC_REGISTER;
  2193. end;
  2194. in_length_x:
  2195. begin
  2196. if is_shortstring(left.resultdef) then
  2197. expectloc:=left.expectloc
  2198. else
  2199. begin
  2200. { ansi/wide string }
  2201. if registersint<1 then
  2202. registersint:=1;
  2203. expectloc:=LOC_REGISTER;
  2204. end;
  2205. end;
  2206. in_typeinfo_x:
  2207. begin
  2208. expectloc:=LOC_REGISTER;
  2209. registersint:=1;
  2210. end;
  2211. in_assigned_x:
  2212. begin
  2213. expectloc := LOC_JUMP;
  2214. registersint:=1;
  2215. end;
  2216. in_pred_x,
  2217. in_succ_x:
  2218. begin
  2219. if is_64bit(resultdef) then
  2220. begin
  2221. if (registersint<2) then
  2222. registersint:=2
  2223. end
  2224. else
  2225. begin
  2226. if (registersint<1) then
  2227. registersint:=1;
  2228. end;
  2229. expectloc:=LOC_REGISTER;
  2230. end;
  2231. in_setlength_x,
  2232. in_initialize_x,
  2233. in_finalize_x:
  2234. begin
  2235. expectloc:=LOC_VOID;
  2236. end;
  2237. in_inc_x,
  2238. in_dec_x:
  2239. begin
  2240. expectloc:=LOC_VOID;
  2241. { range/overflow checking doesn't work properly }
  2242. { with the inc/dec code that's generated (JM) }
  2243. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2244. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2245. trigger an overflow. For uint32 it works because then the operation is done
  2246. in 64bit. Range checking is not applicable to pointers either }
  2247. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2248. { convert to simple add (JM) }
  2249. begin
  2250. newblock := internalstatements(newstatement);
  2251. { extra parameter? }
  2252. if assigned(tcallparanode(left).right) then
  2253. begin
  2254. { Yes, use for add node }
  2255. hpp := tcallparanode(tcallparanode(left).right).left;
  2256. tcallparanode(tcallparanode(left).right).left := nil;
  2257. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2258. CGMessage(parser_e_illegal_expression);
  2259. end
  2260. else
  2261. begin
  2262. { no, create constant 1 }
  2263. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2264. end;
  2265. typecheckpass(hpp);
  2266. if not((hpp.resultdef.typ=orddef) and
  2267. {$ifndef cpu64bit}
  2268. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2269. {$else not cpu64bit}
  2270. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2271. {$endif not cpu64bit}
  2272. inserttypeconv_internal(hpp,sinttype);
  2273. { make sure we don't call functions part of the left node twice (and generally }
  2274. { optimize the code generation) }
  2275. if node_complexity(tcallparanode(left).left) > 1 then
  2276. begin
  2277. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2278. addstatement(newstatement,tempnode);
  2279. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2280. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2281. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2282. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2283. end
  2284. else
  2285. begin
  2286. hp := tcallparanode(left).left.getcopy;
  2287. tempnode := nil;
  2288. end;
  2289. resultnode := hp.getcopy;
  2290. { avoid type errors from the addn/subn }
  2291. if not is_integer(resultnode.resultdef) then
  2292. begin
  2293. inserttypeconv_internal(hp,sinttype);
  2294. inserttypeconv_internal(hpp,sinttype);
  2295. end;
  2296. { addition/substraction depending on inc/dec }
  2297. if inlinenumber = in_inc_x then
  2298. hpp := caddnode.create(addn,hp,hpp)
  2299. else
  2300. hpp := caddnode.create(subn,hp,hpp);
  2301. { assign result of addition }
  2302. if not(is_integer(resultnode.resultdef)) then
  2303. inserttypeconv(hpp,torddef.create(
  2304. {$ifdef cpu64bit}
  2305. s64bit,
  2306. {$else cpu64bit}
  2307. s32bit,
  2308. {$endif cpu64bit}
  2309. get_min_value(resultnode.resultdef),
  2310. get_max_value(resultnode.resultdef)))
  2311. else
  2312. inserttypeconv(hpp,resultnode.resultdef);
  2313. { avoid any possible warnings }
  2314. inserttypeconv_internal(hpp,resultnode.resultdef);
  2315. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2316. { deallocate the temp }
  2317. if assigned(tempnode) then
  2318. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2319. { firstpass it }
  2320. firstpass(newblock);
  2321. { return new node }
  2322. result := newblock;
  2323. end
  2324. else if (left.resultdef.typ in [enumdef,pointerdef]) or
  2325. is_ordinal(left.resultdef) then
  2326. begin
  2327. { two paras ? }
  2328. if assigned(tcallparanode(left).right) then
  2329. begin
  2330. { need we an additional register ? }
  2331. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2332. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2333. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2334. inc(registersint);
  2335. { do we need an additional register to restore the first parameter? }
  2336. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2337. inc(registersint);
  2338. end;
  2339. end;
  2340. end;
  2341. in_include_x_y,
  2342. in_exclude_x_y:
  2343. begin
  2344. expectloc:=LOC_VOID;
  2345. registersint:=left.registersint;
  2346. registersfpu:=left.registersfpu;
  2347. {$ifdef SUPPORT_MMX}
  2348. registersmmx:=left.registersmmx;
  2349. {$endif SUPPORT_MMX}
  2350. end;
  2351. in_pack_x_y_z,
  2352. in_unpack_x_y_z:
  2353. begin
  2354. result:=first_pack_unpack;
  2355. end;
  2356. in_exp_real:
  2357. begin
  2358. result:= first_exp_real;
  2359. end;
  2360. in_round_real:
  2361. begin
  2362. result:= first_round_real;
  2363. end;
  2364. in_trunc_real:
  2365. begin
  2366. result:= first_trunc_real;
  2367. end;
  2368. in_int_real:
  2369. begin
  2370. result:= first_int_real;
  2371. end;
  2372. in_frac_real:
  2373. begin
  2374. result:= first_frac_real;
  2375. end;
  2376. in_cos_real:
  2377. begin
  2378. result:= first_cos_real;
  2379. end;
  2380. in_sin_real:
  2381. begin
  2382. result := first_sin_real;
  2383. end;
  2384. in_arctan_real:
  2385. begin
  2386. result := first_arctan_real;
  2387. end;
  2388. in_pi_real :
  2389. begin
  2390. result := first_pi;
  2391. end;
  2392. in_abs_real:
  2393. begin
  2394. result := first_abs_real;
  2395. end;
  2396. in_sqr_real:
  2397. begin
  2398. result := first_sqr_real;
  2399. end;
  2400. in_sqrt_real:
  2401. begin
  2402. result := first_sqrt_real;
  2403. end;
  2404. in_ln_real:
  2405. begin
  2406. result := first_ln_real;
  2407. end;
  2408. {$ifdef SUPPORT_MMX}
  2409. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2410. begin
  2411. end;
  2412. {$endif SUPPORT_MMX}
  2413. in_assert_x_y :
  2414. begin
  2415. expectloc:=LOC_VOID;
  2416. registersint:=left.registersint;
  2417. registersfpu:=left.registersfpu;
  2418. {$ifdef SUPPORT_MMX}
  2419. registersmmx:=left.registersmmx;
  2420. {$endif SUPPORT_MMX}
  2421. end;
  2422. in_low_x,
  2423. in_high_x:
  2424. internalerror(200104047);
  2425. in_slice_x:
  2426. internalerror(2005101501);
  2427. in_ord_x,
  2428. in_chr_byte:
  2429. begin
  2430. { should not happend as it's converted to typeconv }
  2431. internalerror(200104045);
  2432. end;
  2433. in_ofs_x :
  2434. internalerror(2000101001);
  2435. in_seg_x :
  2436. internalerror(200104046);
  2437. in_settextbuf_file_x,
  2438. in_reset_typedfile,
  2439. in_rewrite_typedfile,
  2440. in_str_x_string,
  2441. in_val_x,
  2442. in_read_x,
  2443. in_readln_x,
  2444. in_write_x,
  2445. in_writeln_x :
  2446. begin
  2447. { should be handled by pass_typecheck }
  2448. internalerror(200108234);
  2449. end;
  2450. in_get_frame:
  2451. begin
  2452. include(current_procinfo.flags,pi_needs_stackframe);
  2453. expectloc:=LOC_CREGISTER;
  2454. end;
  2455. in_get_caller_frame:
  2456. begin
  2457. expectloc:=LOC_REGISTER;
  2458. registersint:=1;
  2459. end;
  2460. in_get_caller_addr:
  2461. begin
  2462. expectloc:=LOC_REGISTER;
  2463. registersint:=1;
  2464. end;
  2465. in_prefetch_var:
  2466. begin
  2467. expectloc:=LOC_VOID;
  2468. end;
  2469. {$ifdef SUPPORT_UNALIGNED}
  2470. in_unaligned_x:
  2471. begin
  2472. expectloc:=tcallparanode(left).left.expectloc;
  2473. end;
  2474. {$endif SUPPORT_UNALIGNED}
  2475. else
  2476. internalerror(89);
  2477. end;
  2478. dec(parsing_para_level);
  2479. end;
  2480. {$maxfpuregisters default}
  2481. function tinlinenode.docompare(p: tnode): boolean;
  2482. begin
  2483. docompare :=
  2484. inherited docompare(p) and
  2485. (inlinenumber = tinlinenode(p).inlinenumber);
  2486. end;
  2487. function tinlinenode.first_pi : tnode;
  2488. begin
  2489. result:=crealconstnode.create(getpi,pbestrealtype^);
  2490. end;
  2491. function tinlinenode.first_arctan_real : tnode;
  2492. begin
  2493. { create the call to the helper }
  2494. { on entry left node contains the parameter }
  2495. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2496. ccallparanode.create(left,nil));
  2497. left := nil;
  2498. end;
  2499. function tinlinenode.first_abs_real : tnode;
  2500. begin
  2501. { create the call to the helper }
  2502. { on entry left node contains the parameter }
  2503. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2504. ccallparanode.create(left,nil));
  2505. left := nil;
  2506. end;
  2507. function tinlinenode.first_sqr_real : tnode;
  2508. begin
  2509. { create the call to the helper }
  2510. { on entry left node contains the parameter }
  2511. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2512. ccallparanode.create(left,nil)),resultdef);
  2513. left := nil;
  2514. end;
  2515. function tinlinenode.first_sqrt_real : tnode;
  2516. begin
  2517. { create the call to the helper }
  2518. { on entry left node contains the parameter }
  2519. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2520. ccallparanode.create(left,nil)),resultdef);
  2521. left := nil;
  2522. end;
  2523. function tinlinenode.first_ln_real : tnode;
  2524. begin
  2525. { create the call to the helper }
  2526. { on entry left node contains the parameter }
  2527. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2528. ccallparanode.create(left,nil));
  2529. left := nil;
  2530. end;
  2531. function tinlinenode.first_cos_real : tnode;
  2532. begin
  2533. { create the call to the helper }
  2534. { on entry left node contains the parameter }
  2535. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2536. ccallparanode.create(left,nil));
  2537. left := nil;
  2538. end;
  2539. function tinlinenode.first_sin_real : tnode;
  2540. begin
  2541. { create the call to the helper }
  2542. { on entry left node contains the parameter }
  2543. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2544. ccallparanode.create(left,nil));
  2545. left := nil;
  2546. end;
  2547. function tinlinenode.first_exp_real : tnode;
  2548. begin
  2549. { create the call to the helper }
  2550. { on entry left node contains the parameter }
  2551. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2552. left := nil;
  2553. end;
  2554. function tinlinenode.first_int_real : tnode;
  2555. begin
  2556. { create the call to the helper }
  2557. { on entry left node contains the parameter }
  2558. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2559. left := nil;
  2560. end;
  2561. function tinlinenode.first_frac_real : tnode;
  2562. begin
  2563. { create the call to the helper }
  2564. { on entry left node contains the parameter }
  2565. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2566. left := nil;
  2567. end;
  2568. function tinlinenode.first_round_real : tnode;
  2569. begin
  2570. { create the call to the helper }
  2571. { on entry left node contains the parameter }
  2572. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2573. left := nil;
  2574. end;
  2575. function tinlinenode.first_trunc_real : tnode;
  2576. begin
  2577. { create the call to the helper }
  2578. { on entry left node contains the parameter }
  2579. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2580. left := nil;
  2581. end;
  2582. function tinlinenode.first_pack_unpack: tnode;
  2583. var
  2584. loopstatement : tstatementnode;
  2585. loop : tblocknode;
  2586. loopvar : ttempcreatenode;
  2587. tempnode,
  2588. source,
  2589. target,
  2590. index,
  2591. unpackednode,
  2592. packednode,
  2593. sourcevecindex,
  2594. targetvecindex,
  2595. loopbody : tnode;
  2596. temprangedef : tdef;
  2597. ulorange,
  2598. uhirange,
  2599. plorange,
  2600. phirange : TConstExprInt;
  2601. begin
  2602. { transform into a for loop which assigns the data of the (un)packed }
  2603. { array to the other one }
  2604. source := left;
  2605. if (inlinenumber = in_unpack_x_y_z) then
  2606. begin
  2607. target := tcallparanode(source).right;
  2608. index := tcallparanode(target).right;
  2609. packednode := tcallparanode(source).left;
  2610. unpackednode := tcallparanode(target).left;
  2611. end
  2612. else
  2613. begin
  2614. index := tcallparanode(source).right;
  2615. target := tcallparanode(index).right;
  2616. packednode := tcallparanode(target).left;
  2617. unpackednode := tcallparanode(source).left;
  2618. end;
  2619. source := tcallparanode(source).left;
  2620. target := tcallparanode(target).left;
  2621. index := tcallparanode(index).left;
  2622. loop := internalstatements(loopstatement);
  2623. loopvar := ctempcreatenode.create(
  2624. tarraydef(packednode.resultdef).rangedef,
  2625. tarraydef(packednode.resultdef).rangedef.size,
  2626. tt_persistent,true);
  2627. addstatement(loopstatement,loopvar);
  2628. { For range checking: we have to convert to an integer type (in case the index type }
  2629. { is an enum), add the index and loop variable together, convert the result }
  2630. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2631. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2632. { errors }
  2633. temprangedef:=nil;
  2634. getrange(unpackednode.resultdef,ulorange,uhirange);
  2635. getrange(packednode.resultdef,plorange,phirange);
  2636. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2637. sourcevecindex := ctemprefnode.create(loopvar);
  2638. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2639. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2640. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2641. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2642. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2643. if (inlinenumber = in_pack_x_y_z) then
  2644. begin
  2645. { swap source and target vec indices }
  2646. tempnode := sourcevecindex;
  2647. sourcevecindex := targetvecindex;
  2648. targetvecindex := tempnode;
  2649. end;
  2650. { create the assignment in the loop body }
  2651. loopbody :=
  2652. cassignmentnode.create(
  2653. cvecnode.create(target.getcopy,targetvecindex),
  2654. cvecnode.create(source.getcopy,sourcevecindex)
  2655. );
  2656. { create the actual for loop }
  2657. tempnode := cfornode.create(
  2658. ctemprefnode.create(loopvar),
  2659. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2660. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2661. loopbody,
  2662. false);
  2663. addstatement(loopstatement,tempnode);
  2664. { free the loop counter }
  2665. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2666. result := loop;
  2667. end;
  2668. begin
  2669. cinlinenode:=tinlinenode;
  2670. end.