ninl.pas 116 KB

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