ninl.pas 121 KB

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