ninl.pas 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  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. i : integer;
  1198. begin
  1199. case def.typ of
  1200. orddef:
  1201. begin
  1202. set_varstate(left,vs_read,[]);
  1203. if inlinenumber=in_low_x then
  1204. v:=torddef(def).low
  1205. else
  1206. v:=torddef(def).high;
  1207. hp:=cordconstnode.create(v,def,true);
  1208. typecheckpass(hp);
  1209. do_lowhigh:=hp;
  1210. end;
  1211. enumdef:
  1212. begin
  1213. set_varstate(left,vs_read,[]);
  1214. if inlinenumber=in_high_x then
  1215. v:=tenumdef(def).maxval
  1216. else
  1217. v:=tenumdef(def).minval;
  1218. enum:=nil;
  1219. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1220. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1221. begin
  1222. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1223. break;
  1224. end;
  1225. if not assigned(enum) then
  1226. internalerror(309993)
  1227. else
  1228. hp:=genenumnode(enum);
  1229. do_lowhigh:=hp;
  1230. end;
  1231. else
  1232. internalerror(87);
  1233. end;
  1234. end;
  1235. function getconstrealvalue : bestreal;
  1236. begin
  1237. case left.nodetype of
  1238. ordconstn:
  1239. getconstrealvalue:=tordconstnode(left).value;
  1240. realconstn:
  1241. getconstrealvalue:=trealconstnode(left).value_real;
  1242. else
  1243. internalerror(309992);
  1244. end;
  1245. end;
  1246. procedure setconstrealvalue(r : bestreal);
  1247. begin
  1248. result:=crealconstnode.create(r,pbestrealtype^);
  1249. end;
  1250. function handle_ln_const(r : bestreal) : tnode;
  1251. begin
  1252. if r<=0.0 then
  1253. if floating_point_range_check_error then
  1254. begin
  1255. result:=crealconstnode.create(0,pbestrealtype^);
  1256. CGMessage(type_e_wrong_math_argument)
  1257. end
  1258. else
  1259. begin
  1260. if r=0.0 then
  1261. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1262. else
  1263. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1264. end
  1265. else
  1266. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1267. end;
  1268. function handle_sqrt_const(r : bestreal) : tnode;
  1269. begin
  1270. if r<0.0 then
  1271. if floating_point_range_check_error then
  1272. begin
  1273. result:=crealconstnode.create(0,pbestrealtype^);
  1274. CGMessage(type_e_wrong_math_argument)
  1275. end
  1276. else
  1277. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1278. else
  1279. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1280. end;
  1281. function handle_const_sar : tnode;
  1282. var
  1283. vl,vl2 : TConstExprInt;
  1284. bits,shift: integer;
  1285. mask : qword;
  1286. def : tdef;
  1287. begin
  1288. result:=nil;
  1289. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1290. begin
  1291. if (left.nodetype=callparan) and
  1292. assigned(tcallparanode(left).right) then
  1293. begin
  1294. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1295. begin
  1296. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1297. vl:=tordconstnode(tcallparanode(left).left).value;
  1298. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1299. end
  1300. else
  1301. exit;
  1302. end
  1303. else
  1304. begin
  1305. def:=left.resultdef;
  1306. vl:=1;
  1307. vl2:=tordconstnode(left).value;
  1308. end;
  1309. bits:=def.size*8;
  1310. shift:=vl.svalue and (bits-1);
  1311. case bits of
  1312. 8:
  1313. mask:=$ff;
  1314. 16:
  1315. mask:=$ffff;
  1316. 32:
  1317. mask:=$ffffffff;
  1318. 64:
  1319. mask:=qword($ffffffffffffffff);
  1320. else
  1321. mask:=qword(1 shl bits)-1;
  1322. end;
  1323. {$push}
  1324. {$r-,q-}
  1325. if shift=0 then
  1326. result:=cordconstnode.create(vl2.svalue,def,false)
  1327. else if vl2.svalue<0 then
  1328. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1329. else
  1330. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1331. {$pop}
  1332. end
  1333. else
  1334. end;
  1335. var
  1336. hp : tnode;
  1337. vl,vl2 : TConstExprInt;
  1338. vr : bestreal;
  1339. begin { simplify }
  1340. result:=nil;
  1341. { handle intern constant functions in separate case }
  1342. if nf_inlineconst in flags then
  1343. begin
  1344. { no parameters? }
  1345. if not assigned(left) then
  1346. internalerror(200501231)
  1347. else
  1348. begin
  1349. vl:=0;
  1350. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1351. case left.nodetype of
  1352. realconstn :
  1353. begin
  1354. { Real functions are all handled with internproc below }
  1355. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1356. end;
  1357. ordconstn :
  1358. vl:=tordconstnode(left).value;
  1359. callparan :
  1360. begin
  1361. { both exists, else it was not generated }
  1362. vl:=tordconstnode(tcallparanode(left).left).value;
  1363. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1364. end;
  1365. else
  1366. CGMessage(parser_e_illegal_expression);
  1367. end;
  1368. case inlinenumber of
  1369. in_const_abs :
  1370. if vl.signed then
  1371. hp:=genintconstnode(abs(vl.svalue))
  1372. else
  1373. hp:=genintconstnode(vl.uvalue);
  1374. in_const_sqr:
  1375. if vl.signed then
  1376. hp:=genintconstnode(sqr(vl.svalue))
  1377. else
  1378. hp:=genintconstnode(sqr(vl.uvalue));
  1379. in_const_odd :
  1380. hp:=cordconstnode.create(qword(odd(int64(vl))),booltype,true);
  1381. in_const_swap_word :
  1382. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1383. in_const_swap_long :
  1384. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1385. in_const_swap_qword :
  1386. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1387. in_const_ptr:
  1388. begin
  1389. {Don't construct pointers from negative values.}
  1390. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1391. cgmessage(parser_e_range_check_error);
  1392. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1393. end
  1394. else
  1395. internalerror(88);
  1396. end;
  1397. end;
  1398. if hp=nil then
  1399. hp:=cerrornode.create;
  1400. result:=hp;
  1401. end
  1402. else
  1403. begin
  1404. case inlinenumber of
  1405. in_lo_long,
  1406. in_hi_long,
  1407. in_lo_qword,
  1408. in_hi_qword,
  1409. in_lo_word,
  1410. in_hi_word :
  1411. begin
  1412. if left.nodetype=ordconstn then
  1413. begin
  1414. case inlinenumber of
  1415. in_lo_word :
  1416. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1417. in_hi_word :
  1418. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1419. in_lo_long :
  1420. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1421. in_hi_long :
  1422. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1423. in_lo_qword :
  1424. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1425. in_hi_qword :
  1426. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1427. end;
  1428. end;
  1429. end;
  1430. in_ord_x:
  1431. begin
  1432. case left.resultdef.typ of
  1433. orddef :
  1434. begin
  1435. case torddef(left.resultdef).ordtype of
  1436. pasbool,
  1437. uchar:
  1438. begin
  1439. { change to byte() }
  1440. result:=ctypeconvnode.create_internal(left,u8inttype);
  1441. left:=nil;
  1442. end;
  1443. uwidechar :
  1444. begin
  1445. { change to word() }
  1446. result:=ctypeconvnode.create_internal(left,u16inttype);
  1447. left:=nil;
  1448. end;
  1449. bool8bit:
  1450. begin
  1451. { change to shortint() }
  1452. result:=ctypeconvnode.create_internal(left,s8inttype);
  1453. left:=nil;
  1454. end;
  1455. bool16bit :
  1456. begin
  1457. { change to smallint() }
  1458. result:=ctypeconvnode.create_internal(left,s16inttype);
  1459. left:=nil;
  1460. end;
  1461. bool32bit :
  1462. begin
  1463. { change to longint() }
  1464. result:=ctypeconvnode.create_internal(left,s32inttype);
  1465. left:=nil;
  1466. end;
  1467. bool64bit :
  1468. begin
  1469. { change to int64() }
  1470. result:=ctypeconvnode.create_internal(left,s64inttype);
  1471. left:=nil;
  1472. end;
  1473. uvoid :
  1474. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1475. else
  1476. begin
  1477. { all other orddef need no transformation }
  1478. result:=left;
  1479. left:=nil;
  1480. end;
  1481. end;
  1482. end;
  1483. enumdef :
  1484. begin
  1485. result:=ctypeconvnode.create_internal(left,s32inttype);
  1486. left:=nil;
  1487. end;
  1488. pointerdef :
  1489. begin
  1490. if m_mac in current_settings.modeswitches then
  1491. begin
  1492. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1493. left:=nil;
  1494. end
  1495. end;
  1496. end;
  1497. (*
  1498. if (left.nodetype=ordconstn) then
  1499. begin
  1500. result:=cordconstnode.create(
  1501. tordconstnode(left).value,sinttype,true);
  1502. end
  1503. else if (m_mac in current_settings.modeswitches) and
  1504. (left.ndoetype=pointerconstn) then
  1505. result:=cordconstnode.create(
  1506. tpointerconstnode(left).value,ptruinttype,true);
  1507. *)
  1508. end;
  1509. in_chr_byte:
  1510. begin
  1511. { convert to explicit char() }
  1512. result:=ctypeconvnode.create_internal(left,cchartype);
  1513. left:=nil;
  1514. end;
  1515. in_length_x:
  1516. begin
  1517. case left.resultdef.typ of
  1518. stringdef :
  1519. begin
  1520. if (left.nodetype=stringconstn) then
  1521. begin
  1522. result:=cordconstnode.create(
  1523. tstringconstnode(left).len,sinttype,true);
  1524. end;
  1525. end;
  1526. orddef :
  1527. begin
  1528. { length of char is always one }
  1529. if is_char(left.resultdef) or
  1530. is_widechar(left.resultdef) then
  1531. begin
  1532. result:=cordconstnode.create(1,sinttype,false);
  1533. end
  1534. end;
  1535. arraydef :
  1536. begin
  1537. if not is_open_array(left.resultdef) and
  1538. not is_array_of_const(left.resultdef) and
  1539. not is_dynamic_array(left.resultdef) then
  1540. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1541. tarraydef(left.resultdef).lowrange+1,
  1542. sinttype,true);
  1543. end;
  1544. end;
  1545. end;
  1546. in_assigned_x:
  1547. begin
  1548. if is_constnode(tcallparanode(left).left) or
  1549. (tcallparanode(left).left.nodetype = pointerconstn) then
  1550. begin
  1551. { let an add node figure it out }
  1552. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1553. tcallparanode(left).left := nil;
  1554. end;
  1555. end;
  1556. in_pred_x,
  1557. in_succ_x:
  1558. begin
  1559. if (left.nodetype=ordconstn) then
  1560. begin
  1561. if (inlinenumber=in_succ_x) then
  1562. vl:=tordconstnode(left).value+1
  1563. else
  1564. vl:=tordconstnode(left).value-1;
  1565. if is_integer(left.resultdef) then
  1566. { the type of the original integer constant is irrelevant,
  1567. it should be automatically adapted to the new value }
  1568. result:=genintconstnode(vl)
  1569. else
  1570. { check the range for enums, chars, booleans }
  1571. result:=cordconstnode.create(vl,left.resultdef,true)
  1572. end
  1573. end;
  1574. in_low_x,
  1575. in_high_x:
  1576. begin
  1577. case left.resultdef.typ of
  1578. orddef,
  1579. enumdef:
  1580. begin
  1581. result:=do_lowhigh(left.resultdef);
  1582. end;
  1583. setdef:
  1584. begin
  1585. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1586. end;
  1587. arraydef:
  1588. begin
  1589. if (inlinenumber=in_low_x) then
  1590. begin
  1591. result:=cordconstnode.create(int64(tarraydef(
  1592. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  1593. end
  1594. else if not is_open_array(left.resultdef) and
  1595. not is_array_of_const(left.resultdef) and
  1596. not is_dynamic_array(left.resultdef) then
  1597. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  1598. tarraydef(left.resultdef).rangedef,true);
  1599. end;
  1600. stringdef:
  1601. begin
  1602. if inlinenumber=in_low_x then
  1603. begin
  1604. result:=cordconstnode.create(0,u8inttype,false);
  1605. end
  1606. else if not is_ansistring(left.resultdef) and
  1607. not is_wide_or_unicode_string(left.resultdef) then
  1608. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  1609. end;
  1610. end;
  1611. end;
  1612. in_exp_real :
  1613. begin
  1614. if left.nodetype in [ordconstn,realconstn] then
  1615. begin
  1616. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1617. if (trealconstnode(result).value_real=MathInf.Value) and
  1618. floating_point_range_check_error then
  1619. begin
  1620. result:=crealconstnode.create(0,pbestrealtype^);
  1621. CGMessage(parser_e_range_check_error);
  1622. end;
  1623. end
  1624. end;
  1625. in_trunc_real :
  1626. begin
  1627. if left.nodetype in [ordconstn,realconstn] then
  1628. begin
  1629. vr:=getconstrealvalue;
  1630. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1631. begin
  1632. CGMessage(parser_e_range_check_error);
  1633. result:=cordconstnode.create(1,s64inttype,false)
  1634. end
  1635. else
  1636. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1637. end
  1638. end;
  1639. in_round_real :
  1640. begin
  1641. if left.nodetype in [ordconstn,realconstn] then
  1642. begin
  1643. vr:=getconstrealvalue;
  1644. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1645. begin
  1646. CGMessage(parser_e_range_check_error);
  1647. result:=cordconstnode.create(1,s64inttype,false)
  1648. end
  1649. else
  1650. result:=cordconstnode.create(round(vr),s64inttype,true)
  1651. end
  1652. end;
  1653. in_frac_real :
  1654. begin
  1655. if left.nodetype in [ordconstn,realconstn] then
  1656. setconstrealvalue(frac(getconstrealvalue))
  1657. end;
  1658. in_int_real :
  1659. begin
  1660. if left.nodetype in [ordconstn,realconstn] then
  1661. setconstrealvalue(int(getconstrealvalue));
  1662. end;
  1663. in_pi_real :
  1664. begin
  1665. if block_type=bt_const then
  1666. setconstrealvalue(getpi)
  1667. end;
  1668. in_cos_real :
  1669. begin
  1670. if left.nodetype in [ordconstn,realconstn] then
  1671. setconstrealvalue(cos(getconstrealvalue))
  1672. end;
  1673. in_sin_real :
  1674. begin
  1675. if left.nodetype in [ordconstn,realconstn] then
  1676. setconstrealvalue(sin(getconstrealvalue))
  1677. end;
  1678. in_arctan_real :
  1679. begin
  1680. if left.nodetype in [ordconstn,realconstn] then
  1681. setconstrealvalue(arctan(getconstrealvalue))
  1682. end;
  1683. in_abs_real :
  1684. begin
  1685. if left.nodetype in [ordconstn,realconstn] then
  1686. setconstrealvalue(abs(getconstrealvalue))
  1687. end;
  1688. in_abs_long:
  1689. begin
  1690. if left.nodetype=ordconstn then
  1691. begin
  1692. if tordconstnode(left).value<0 then
  1693. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  1694. else
  1695. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  1696. end
  1697. end;
  1698. in_sqr_real :
  1699. begin
  1700. if left.nodetype in [ordconstn,realconstn] then
  1701. setconstrealvalue(sqr(getconstrealvalue))
  1702. end;
  1703. in_sqrt_real :
  1704. begin
  1705. if left.nodetype in [ordconstn,realconstn] then
  1706. begin
  1707. vr:=getconstrealvalue;
  1708. if vr<0.0 then
  1709. result:=handle_sqrt_const(vr)
  1710. else
  1711. setconstrealvalue(sqrt(vr));
  1712. end
  1713. end;
  1714. in_ln_real :
  1715. begin
  1716. if left.nodetype in [ordconstn,realconstn] then
  1717. begin
  1718. vr:=getconstrealvalue;
  1719. if vr<=0.0 then
  1720. result:=handle_ln_const(vr)
  1721. else
  1722. setconstrealvalue(ln(vr));
  1723. end
  1724. end;
  1725. in_assert_x_y :
  1726. begin
  1727. if not(cs_do_assertion in current_settings.localswitches) then
  1728. { we need a valid node, so insert a nothingn }
  1729. result:=cnothingnode.create;
  1730. end;
  1731. in_sar_x,
  1732. in_sar_x_y :
  1733. begin
  1734. result:=handle_const_sar;
  1735. end;
  1736. end;
  1737. end;
  1738. end;
  1739. function tinlinenode.pass_typecheck:tnode;
  1740. procedure setfloatresultdef;
  1741. begin
  1742. if (left.resultdef.typ=floatdef) and
  1743. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  1744. resultdef:=left.resultdef
  1745. else
  1746. begin
  1747. if (left.nodetype <> ordconstn) then
  1748. inserttypeconv(left,pbestrealtype^);
  1749. resultdef:=pbestrealtype^;
  1750. end;
  1751. end;
  1752. procedure handle_pack_unpack;
  1753. var
  1754. source, target, index: tcallparanode;
  1755. unpackedarraydef, packedarraydef: tarraydef;
  1756. tempindex: TConstExprInt;
  1757. begin
  1758. resultdef:=voidtype;
  1759. unpackedarraydef := nil;
  1760. packedarraydef := nil;
  1761. source := tcallparanode(left);
  1762. if (inlinenumber = in_unpack_x_y_z) then
  1763. begin
  1764. target := tcallparanode(source.right);
  1765. index := tcallparanode(target.right);
  1766. { source must be a packed array }
  1767. if not is_packed_array(source.left.resultdef) then
  1768. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1769. else
  1770. packedarraydef := tarraydef(source.left.resultdef);
  1771. { target can be any kind of array, as long as it's not packed }
  1772. if (target.left.resultdef.typ <> arraydef) or
  1773. is_packed_array(target.left.resultdef) then
  1774. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1775. else
  1776. unpackedarraydef := tarraydef(target.left.resultdef);
  1777. end
  1778. else
  1779. begin
  1780. index := tcallparanode(source.right);
  1781. target := tcallparanode(index.right);
  1782. { source can be any kind of array, as long as it's not packed }
  1783. if (source.left.resultdef.typ <> arraydef) or
  1784. is_packed_array(source.left.resultdef) then
  1785. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1786. else
  1787. unpackedarraydef := tarraydef(source.left.resultdef);
  1788. { target must be a packed array }
  1789. if not is_packed_array(target.left.resultdef) then
  1790. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1791. else
  1792. packedarraydef := tarraydef(target.left.resultdef);
  1793. end;
  1794. if assigned(unpackedarraydef) then
  1795. begin
  1796. { index must be compatible with the unpacked array's indextype }
  1797. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1798. { range check at compile time if possible }
  1799. if assigned(packedarraydef) and
  1800. (index.left.nodetype = ordconstn) and
  1801. not is_special_array(unpackedarraydef) then
  1802. begin
  1803. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1804. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1805. testrange(unpackedarraydef,tempindex,false);
  1806. end;
  1807. end;
  1808. { source array is read and must be valid }
  1809. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1810. { target array is written }
  1811. valid_for_assignment(target.left,true);
  1812. set_varstate(target.left,vs_written,[]);
  1813. { index in the unpacked array is read and must be valid }
  1814. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1815. { if the size of the arrays is 0 (array of empty records), }
  1816. { do nothing }
  1817. if (source.resultdef.size = 0) then
  1818. result:=cnothingnode.create;
  1819. end;
  1820. function handle_objc_encode: tnode;
  1821. var
  1822. encodedtype: ansistring;
  1823. errordef: tdef;
  1824. begin
  1825. encodedtype:='';
  1826. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  1827. Message1(type_e_objc_type_unsupported,errordef.typename);
  1828. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype));
  1829. end;
  1830. var
  1831. hightree,
  1832. hp : tnode;
  1833. begin
  1834. result:=nil;
  1835. { when handling writeln "left" contains no valid address }
  1836. if assigned(left) then
  1837. begin
  1838. if left.nodetype=callparan then
  1839. tcallparanode(left).get_paratype
  1840. else
  1841. typecheckpass(left);
  1842. end;
  1843. if not(nf_inlineconst in flags) then
  1844. begin
  1845. case inlinenumber of
  1846. in_lo_long,
  1847. in_hi_long,
  1848. in_lo_qword,
  1849. in_hi_qword,
  1850. in_lo_word,
  1851. in_hi_word :
  1852. begin
  1853. { give warning for incompatibility with tp and delphi }
  1854. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1855. ((m_tp7 in current_settings.modeswitches) or
  1856. (m_delphi in current_settings.modeswitches)) then
  1857. CGMessage(type_w_maybe_wrong_hi_lo);
  1858. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1859. if not is_integer(left.resultdef) then
  1860. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1861. case inlinenumber of
  1862. in_lo_word,
  1863. in_hi_word :
  1864. resultdef:=u8inttype;
  1865. in_lo_long,
  1866. in_hi_long :
  1867. resultdef:=u16inttype;
  1868. in_lo_qword,
  1869. in_hi_qword :
  1870. resultdef:=u32inttype;
  1871. end;
  1872. end;
  1873. in_sizeof_x:
  1874. begin
  1875. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  1876. set_varstate(left,vs_read,[]);
  1877. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1878. begin
  1879. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1880. if assigned(hightree) then
  1881. begin
  1882. hp:=caddnode.create(addn,hightree,
  1883. cordconstnode.create(1,sinttype,false));
  1884. if (left.resultdef.typ=arraydef) then
  1885. if not is_packed_array(tarraydef(left.resultdef)) then
  1886. begin
  1887. if (tarraydef(left.resultdef).elesize<>1) then
  1888. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1889. left.resultdef).elesize,sinttype,true));
  1890. end
  1891. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1892. begin
  1893. { no packed open array support yet }
  1894. if (hp.nodetype <> ordconstn) then
  1895. internalerror(2006081511);
  1896. hp.free;
  1897. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1898. {
  1899. hp:=
  1900. ctypeconvnode.create_explicit(sinttype,
  1901. cmoddivnode.create(divn,
  1902. caddnode.create(addn,
  1903. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1904. left.resultdef).elepackedbitsize,s64inttype,true)),
  1905. cordconstnode.create(a,s64inttype,true)),
  1906. cordconstnode.create(8,s64inttype,true)),
  1907. sinttype);
  1908. }
  1909. end;
  1910. result:=hp;
  1911. end;
  1912. end
  1913. else
  1914. resultdef:=sinttype;
  1915. end;
  1916. in_typeof_x:
  1917. begin
  1918. set_varstate(left,vs_read,[]);
  1919. resultdef:=voidpointertype;
  1920. end;
  1921. in_ord_x:
  1922. begin
  1923. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1924. case left.resultdef.typ of
  1925. orddef,
  1926. enumdef :
  1927. ;
  1928. pointerdef :
  1929. begin
  1930. if not(m_mac in current_settings.modeswitches) then
  1931. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1932. end
  1933. else
  1934. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1935. end;
  1936. end;
  1937. in_chr_byte:
  1938. begin
  1939. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1940. end;
  1941. in_length_x:
  1942. begin
  1943. if ((left.resultdef.typ=arraydef) and
  1944. (not is_special_array(left.resultdef) or
  1945. is_open_array(left.resultdef))) or
  1946. (left.resultdef.typ=orddef) then
  1947. set_varstate(left,vs_read,[])
  1948. else
  1949. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1950. case left.resultdef.typ of
  1951. variantdef:
  1952. begin
  1953. inserttypeconv(left,cansistringtype);
  1954. end;
  1955. stringdef :
  1956. begin
  1957. { we don't need string convertions here, }
  1958. { except if from widestring to ansistring }
  1959. { and vice versa (that can change the }
  1960. { length) }
  1961. if (left.nodetype=typeconvn) and
  1962. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  1963. not(is_wide_or_unicode_string(left.resultdef) xor
  1964. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  1965. begin
  1966. hp:=ttypeconvnode(left).left;
  1967. ttypeconvnode(left).left:=nil;
  1968. left.free;
  1969. left:=hp;
  1970. end;
  1971. end;
  1972. orddef :
  1973. begin
  1974. { will be handled in simplify }
  1975. if not is_char(left.resultdef) and
  1976. not is_widechar(left.resultdef) then
  1977. CGMessage(type_e_mismatch);
  1978. end;
  1979. pointerdef :
  1980. begin
  1981. if is_pchar(left.resultdef) then
  1982. begin
  1983. hp := ccallparanode.create(left,nil);
  1984. result := ccallnode.createintern('fpc_pchar_length',hp);
  1985. { make sure the left node doesn't get disposed, since it's }
  1986. { reused in the new node (JM) }
  1987. left:=nil;
  1988. exit;
  1989. end
  1990. else if is_pwidechar(left.resultdef) then
  1991. begin
  1992. hp := ccallparanode.create(left,nil);
  1993. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1994. { make sure the left node doesn't get disposed, since it's }
  1995. { reused in the new node (JM) }
  1996. left:=nil;
  1997. exit;
  1998. end
  1999. else
  2000. CGMessage(type_e_mismatch);
  2001. end;
  2002. arraydef :
  2003. begin
  2004. if is_open_array(left.resultdef) or
  2005. is_array_of_const(left.resultdef) then
  2006. begin
  2007. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2008. if assigned(hightree) then
  2009. result:=caddnode.create(addn,hightree,
  2010. cordconstnode.create(1,sinttype,false));
  2011. exit;
  2012. end
  2013. else if is_dynamic_array(left.resultdef) then
  2014. begin
  2015. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2016. result := ccallnode.createintern('fpc_dynarray_length',hp);
  2017. { make sure the left node doesn't get disposed, since it's }
  2018. { reused in the new node (JM) }
  2019. left:=nil;
  2020. exit;
  2021. end
  2022. else
  2023. begin
  2024. { will be handled in simplify }
  2025. end;
  2026. end
  2027. else
  2028. CGMessage(type_e_mismatch);
  2029. end;
  2030. { shortstring return an 8 bit value as the length
  2031. is the first byte of the string }
  2032. if is_shortstring(left.resultdef) then
  2033. resultdef:=u8inttype
  2034. else
  2035. resultdef:=sinttype;
  2036. end;
  2037. in_typeinfo_x:
  2038. begin
  2039. if (left.resultdef.typ=enumdef) and
  2040. (tenumdef(left.resultdef).has_jumps) then
  2041. CGMessage(type_e_no_type_info);
  2042. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2043. resultdef:=voidpointertype;
  2044. end;
  2045. in_assigned_x:
  2046. begin
  2047. { the parser has already made sure the expression is valid }
  2048. { there could be a procvar, which is 2*sizeof(pointer), while we }
  2049. { must only check the first pointer -> can't just convert to an }
  2050. { add node in all cases }
  2051. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2052. resultdef:=booltype;
  2053. end;
  2054. in_ofs_x :
  2055. internalerror(2000101001);
  2056. in_seg_x :
  2057. begin
  2058. set_varstate(left,vs_read,[]);
  2059. result:=cordconstnode.create(0,s32inttype,false);
  2060. end;
  2061. in_pred_x,
  2062. in_succ_x:
  2063. begin
  2064. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2065. resultdef:=left.resultdef;
  2066. if not is_ordinal(resultdef) then
  2067. CGMessage(type_e_ordinal_expr_expected)
  2068. else
  2069. begin
  2070. if (resultdef.typ=enumdef) and
  2071. (tenumdef(resultdef).has_jumps) and
  2072. not(m_delphi in current_settings.modeswitches) then
  2073. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2074. end;
  2075. end;
  2076. in_initialize_x,
  2077. in_finalize_x,
  2078. in_setlength_x:
  2079. begin
  2080. { inlined from pinline }
  2081. internalerror(200204231);
  2082. end;
  2083. in_inc_x,
  2084. in_dec_x:
  2085. begin
  2086. resultdef:=voidtype;
  2087. if assigned(left) then
  2088. begin
  2089. { first param must be var }
  2090. valid_for_var(tcallparanode(left).left,true);
  2091. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2092. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2093. is_ordinal(left.resultdef) or
  2094. is_currency(left.resultdef) then
  2095. begin
  2096. { value of left gets changed -> must be unique }
  2097. set_unique(tcallparanode(left).left);
  2098. { two paras ? }
  2099. if assigned(tcallparanode(left).right) then
  2100. begin
  2101. if is_integer(tcallparanode(left).right.resultdef) then
  2102. begin
  2103. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2104. { when range/overflow checking is on, we
  2105. convert this to a regular add, and for proper
  2106. checking we need the original type }
  2107. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2108. if is_integer(tcallparanode(left).left.resultdef) then
  2109. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2110. else
  2111. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2112. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2113. { should be handled in the parser (JM) }
  2114. internalerror(2006020901);
  2115. end
  2116. else
  2117. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2118. end;
  2119. end
  2120. else
  2121. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2122. end
  2123. else
  2124. CGMessagePos(fileinfo,type_e_mismatch);
  2125. end;
  2126. in_read_x,
  2127. in_readln_x,
  2128. in_readstr_x,
  2129. in_write_x,
  2130. in_writeln_x,
  2131. in_writestr_x :
  2132. begin
  2133. result := handle_read_write;
  2134. end;
  2135. in_settextbuf_file_x :
  2136. begin
  2137. resultdef:=voidtype;
  2138. { now we know the type of buffer }
  2139. hp:=ccallparanode.create(cordconstnode.create(
  2140. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2141. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2142. left:=nil;
  2143. end;
  2144. { the firstpass of the arg has been done in firstcalln ? }
  2145. in_reset_typedfile,
  2146. in_rewrite_typedfile :
  2147. begin
  2148. result := handle_reset_rewrite_typed;
  2149. end;
  2150. in_str_x_string :
  2151. begin
  2152. result:=handle_str;
  2153. end;
  2154. in_val_x :
  2155. begin
  2156. result:=handle_val;
  2157. end;
  2158. in_include_x_y,
  2159. in_exclude_x_y:
  2160. begin
  2161. resultdef:=voidtype;
  2162. { the parser already checks whether we have two (and exactly two) }
  2163. { parameters (JM) }
  2164. { first param must be var }
  2165. valid_for_var(tcallparanode(left).left,true);
  2166. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2167. { check type }
  2168. if (left.resultdef.typ=setdef) then
  2169. begin
  2170. { insert a type conversion }
  2171. { to the type of the set elements }
  2172. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2173. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2174. tsetdef(left.resultdef).elementdef);
  2175. end
  2176. else
  2177. CGMessage(type_e_mismatch);
  2178. end;
  2179. in_pack_x_y_z,
  2180. in_unpack_x_y_z :
  2181. begin
  2182. handle_pack_unpack;
  2183. end;
  2184. in_slice_x:
  2185. begin
  2186. result:=nil;
  2187. resultdef:=tcallparanode(left).left.resultdef;
  2188. if (resultdef.typ <> arraydef) then
  2189. CGMessagePos(left.fileinfo,type_e_mismatch)
  2190. else if is_packed_array(resultdef) then
  2191. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2192. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2193. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2194. type_e_integer_expr_expected,
  2195. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2196. end;
  2197. in_low_x,
  2198. in_high_x:
  2199. begin
  2200. case left.resultdef.typ of
  2201. orddef,
  2202. enumdef,
  2203. setdef:
  2204. ;
  2205. arraydef:
  2206. begin
  2207. if (inlinenumber=in_low_x) then
  2208. set_varstate(left,vs_read,[])
  2209. else
  2210. begin
  2211. if is_open_array(left.resultdef) or
  2212. is_array_of_const(left.resultdef) then
  2213. begin
  2214. set_varstate(left,vs_read,[]);
  2215. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2216. end
  2217. else
  2218. if is_dynamic_array(left.resultdef) then
  2219. begin
  2220. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2221. { can't use inserttypeconv because we need }
  2222. { an explicit type conversion (JM) }
  2223. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2224. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2225. { make sure the left node doesn't get disposed, since it's }
  2226. { reused in the new node (JM) }
  2227. left:=nil;
  2228. end
  2229. else
  2230. begin
  2231. set_varstate(left,vs_read,[]);
  2232. end;
  2233. end;
  2234. end;
  2235. stringdef:
  2236. begin
  2237. if inlinenumber=in_low_x then
  2238. begin
  2239. set_varstate(left,vs_read,[]);
  2240. end
  2241. else
  2242. begin
  2243. if is_open_string(left.resultdef) then
  2244. begin
  2245. set_varstate(left,vs_read,[]);
  2246. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2247. end
  2248. else if is_ansistring(left.resultdef) or
  2249. is_wide_or_unicode_string(left.resultdef) then
  2250. CGMessage(type_e_mismatch)
  2251. end;
  2252. end;
  2253. else
  2254. CGMessage(type_e_mismatch);
  2255. end;
  2256. end;
  2257. in_exp_real,
  2258. in_frac_real,
  2259. in_int_real,
  2260. in_cos_real,
  2261. in_sin_real,
  2262. in_arctan_real,
  2263. in_abs_real,
  2264. in_ln_real :
  2265. begin
  2266. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2267. { converting an int64 to double on platforms without }
  2268. { extended can cause precision loss }
  2269. if not(left.nodetype in [ordconstn,realconstn]) then
  2270. inserttypeconv(left,pbestrealtype^);
  2271. resultdef:=pbestrealtype^;
  2272. end;
  2273. in_trunc_real,
  2274. in_round_real :
  2275. begin
  2276. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2277. { for direct float rounding, no best real type cast should be necessary }
  2278. if not((left.resultdef.typ=floatdef) and
  2279. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2280. { converting an int64 to double on platforms without }
  2281. { extended can cause precision loss }
  2282. not(left.nodetype in [ordconstn,realconstn]) then
  2283. inserttypeconv(left,pbestrealtype^);
  2284. resultdef:=s64inttype;
  2285. end;
  2286. in_pi_real :
  2287. begin
  2288. resultdef:=pbestrealtype^;
  2289. end;
  2290. in_abs_long:
  2291. begin
  2292. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2293. inserttypeconv(left,s32inttype);
  2294. resultdef:=s32inttype;
  2295. end;
  2296. in_sqr_real,
  2297. in_sqrt_real :
  2298. begin
  2299. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2300. setfloatresultdef;
  2301. end;
  2302. {$ifdef SUPPORT_MMX}
  2303. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2304. begin
  2305. end;
  2306. {$endif SUPPORT_MMX}
  2307. in_unaligned_x:
  2308. begin
  2309. resultdef:=left.resultdef;
  2310. end;
  2311. in_assert_x_y :
  2312. begin
  2313. resultdef:=voidtype;
  2314. if assigned(left) then
  2315. begin
  2316. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2317. { check type }
  2318. if is_boolean(left.resultdef) then
  2319. begin
  2320. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2321. { must always be a string }
  2322. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2323. end
  2324. else
  2325. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2326. end
  2327. else
  2328. CGMessage(type_e_mismatch);
  2329. if (cs_do_assertion in current_settings.localswitches) then
  2330. include(current_procinfo.flags,pi_do_call);
  2331. end;
  2332. in_prefetch_var:
  2333. resultdef:=voidtype;
  2334. in_get_frame,
  2335. in_get_caller_frame,
  2336. in_get_caller_addr:
  2337. begin
  2338. resultdef:=voidpointertype;
  2339. end;
  2340. in_rol_x,
  2341. in_ror_x,
  2342. in_sar_x:
  2343. begin
  2344. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2345. resultdef:=left.resultdef;
  2346. end;
  2347. in_rol_x_x,
  2348. in_ror_x_x,
  2349. in_sar_x_y:
  2350. begin
  2351. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2352. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2353. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2354. end;
  2355. in_objc_selector_x:
  2356. begin
  2357. result:=cobjcselectornode.create(left);
  2358. { reused }
  2359. left:=nil;
  2360. end;
  2361. in_objc_protocol_x:
  2362. begin
  2363. result:=cobjcprotocolnode.create(left);
  2364. { reused }
  2365. left:=nil;
  2366. end;
  2367. in_objc_encode_x:
  2368. begin
  2369. result:=handle_objc_encode;
  2370. end;
  2371. else
  2372. internalerror(8);
  2373. end;
  2374. end;
  2375. if not assigned(result) and not
  2376. codegenerror then
  2377. result:=simplify;
  2378. end;
  2379. function tinlinenode.pass_1 : tnode;
  2380. var
  2381. hp,hpp,resultnode : tnode;
  2382. shiftconst: longint;
  2383. tempnode: ttempcreatenode;
  2384. newstatement: tstatementnode;
  2385. newblock: tblocknode;
  2386. begin
  2387. result:=nil;
  2388. { if we handle writeln; left contains no valid address }
  2389. if assigned(left) then
  2390. begin
  2391. if left.nodetype=callparan then
  2392. tcallparanode(left).firstcallparan
  2393. else
  2394. firstpass(left);
  2395. end;
  2396. { intern const should already be handled }
  2397. if nf_inlineconst in flags then
  2398. internalerror(200104044);
  2399. case inlinenumber of
  2400. in_lo_qword,
  2401. in_hi_qword,
  2402. in_lo_long,
  2403. in_hi_long,
  2404. in_lo_word,
  2405. in_hi_word:
  2406. begin
  2407. shiftconst := 0;
  2408. case inlinenumber of
  2409. in_hi_qword:
  2410. shiftconst := 32;
  2411. in_hi_long:
  2412. shiftconst := 16;
  2413. in_hi_word:
  2414. shiftconst := 8;
  2415. end;
  2416. if shiftconst <> 0 then
  2417. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2418. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2419. else
  2420. result := ctypeconvnode.create_internal(left,resultdef);
  2421. left := nil;
  2422. firstpass(result);
  2423. end;
  2424. in_sizeof_x:
  2425. begin
  2426. expectloc:=LOC_REGISTER;
  2427. end;
  2428. in_typeof_x:
  2429. begin
  2430. expectloc:=LOC_REGISTER;
  2431. end;
  2432. in_length_x:
  2433. begin
  2434. if is_shortstring(left.resultdef) then
  2435. expectloc:=left.expectloc
  2436. else
  2437. begin
  2438. { ansi/wide string }
  2439. expectloc:=LOC_REGISTER;
  2440. end;
  2441. end;
  2442. in_typeinfo_x:
  2443. begin
  2444. expectloc:=LOC_REGISTER;
  2445. end;
  2446. in_assigned_x:
  2447. begin
  2448. expectloc := LOC_JUMP;
  2449. end;
  2450. in_pred_x,
  2451. in_succ_x:
  2452. begin
  2453. expectloc:=LOC_REGISTER;
  2454. { in case of range/overflow checking, use a regular addnode
  2455. because it's too complex to handle correctly otherwise }
  2456. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  2457. begin
  2458. { create constant 1 }
  2459. hp:=cordconstnode.create(1,left.resultdef,false);
  2460. typecheckpass(hp);
  2461. if not is_integer(hp.resultdef) then
  2462. inserttypeconv_internal(hp,sinttype);
  2463. { avoid type errors from the addn/subn }
  2464. if not is_integer(left.resultdef) then
  2465. inserttypeconv_internal(left,sinttype);
  2466. { addition/substraction depending on succ/pred }
  2467. if inlinenumber=in_succ_x then
  2468. hp:=caddnode.create(addn,left,hp)
  2469. else
  2470. hp:=caddnode.create(subn,left,hp);
  2471. { assign result of addition }
  2472. if not(is_integer(resultdef)) then
  2473. inserttypeconv(hp,torddef.create(
  2474. {$ifdef cpu64bitaddr}
  2475. s64bit,
  2476. {$else cpu64bitaddr}
  2477. s32bit,
  2478. {$endif cpu64bitaddr}
  2479. get_min_value(resultdef),
  2480. get_max_value(resultdef)))
  2481. else
  2482. inserttypeconv(hp,resultdef);
  2483. { avoid any possible errors/warnings }
  2484. inserttypeconv_internal(hp,resultdef);
  2485. { firstpass it }
  2486. firstpass(hp);
  2487. { left is reused }
  2488. left:=nil;
  2489. { return new node }
  2490. result:=hp;
  2491. end;
  2492. end;
  2493. in_setlength_x,
  2494. in_initialize_x,
  2495. in_finalize_x:
  2496. begin
  2497. expectloc:=LOC_VOID;
  2498. end;
  2499. in_inc_x,
  2500. in_dec_x:
  2501. begin
  2502. expectloc:=LOC_VOID;
  2503. { range/overflow checking doesn't work properly }
  2504. { with the inc/dec code that's generated (JM) }
  2505. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2506. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2507. trigger an overflow. For uint32 it works because then the operation is done
  2508. in 64bit. Range checking is not applicable to pointers either }
  2509. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2510. { convert to simple add (JM) }
  2511. begin
  2512. newblock := internalstatements(newstatement);
  2513. { extra parameter? }
  2514. if assigned(tcallparanode(left).right) then
  2515. begin
  2516. { Yes, use for add node }
  2517. hpp := tcallparanode(tcallparanode(left).right).left;
  2518. tcallparanode(tcallparanode(left).right).left := nil;
  2519. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2520. CGMessage(parser_e_illegal_expression);
  2521. end
  2522. else
  2523. begin
  2524. { no, create constant 1 }
  2525. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2526. end;
  2527. typecheckpass(hpp);
  2528. if not((hpp.resultdef.typ=orddef) and
  2529. {$ifndef cpu64bitaddr}
  2530. (torddef(hpp.resultdef).ordtype=u32bit)) then
  2531. {$else not cpu64bitaddr}
  2532. (torddef(hpp.resultdef).ordtype=u64bit)) then
  2533. {$endif not cpu64bitaddr}
  2534. inserttypeconv_internal(hpp,sinttype);
  2535. { make sure we don't call functions part of the left node twice (and generally }
  2536. { optimize the code generation) }
  2537. if node_complexity(tcallparanode(left).left) > 1 then
  2538. begin
  2539. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2540. addstatement(newstatement,tempnode);
  2541. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2542. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2543. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2544. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2545. end
  2546. else
  2547. begin
  2548. hp := tcallparanode(left).left.getcopy;
  2549. tempnode := nil;
  2550. end;
  2551. resultnode := hp.getcopy;
  2552. { avoid type errors from the addn/subn }
  2553. if not is_integer(resultnode.resultdef) then
  2554. begin
  2555. inserttypeconv_internal(hp,sinttype);
  2556. inserttypeconv_internal(hpp,sinttype);
  2557. end;
  2558. { addition/substraction depending on inc/dec }
  2559. if inlinenumber = in_inc_x then
  2560. hpp := caddnode.create(addn,hp,hpp)
  2561. else
  2562. hpp := caddnode.create(subn,hp,hpp);
  2563. { assign result of addition }
  2564. if not(is_integer(resultnode.resultdef)) then
  2565. inserttypeconv(hpp,torddef.create(
  2566. {$ifdef cpu64bitaddr}
  2567. s64bit,
  2568. {$else cpu64bitaddr}
  2569. s32bit,
  2570. {$endif cpu64bitaddr}
  2571. get_min_value(resultnode.resultdef),
  2572. get_max_value(resultnode.resultdef)))
  2573. else
  2574. inserttypeconv(hpp,resultnode.resultdef);
  2575. { avoid any possible warnings }
  2576. inserttypeconv_internal(hpp,resultnode.resultdef);
  2577. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2578. { deallocate the temp }
  2579. if assigned(tempnode) then
  2580. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2581. { firstpass it }
  2582. firstpass(tnode(newblock));
  2583. { return new node }
  2584. result := newblock;
  2585. end;
  2586. end;
  2587. in_include_x_y,
  2588. in_exclude_x_y:
  2589. begin
  2590. expectloc:=LOC_VOID;
  2591. end;
  2592. in_pack_x_y_z,
  2593. in_unpack_x_y_z:
  2594. begin
  2595. result:=first_pack_unpack;
  2596. end;
  2597. in_exp_real:
  2598. begin
  2599. result:= first_exp_real;
  2600. end;
  2601. in_round_real:
  2602. begin
  2603. result:= first_round_real;
  2604. end;
  2605. in_trunc_real:
  2606. begin
  2607. result:= first_trunc_real;
  2608. end;
  2609. in_int_real:
  2610. begin
  2611. result:= first_int_real;
  2612. end;
  2613. in_frac_real:
  2614. begin
  2615. result:= first_frac_real;
  2616. end;
  2617. in_cos_real:
  2618. begin
  2619. result:= first_cos_real;
  2620. end;
  2621. in_sin_real:
  2622. begin
  2623. result := first_sin_real;
  2624. end;
  2625. in_arctan_real:
  2626. begin
  2627. result := first_arctan_real;
  2628. end;
  2629. in_pi_real :
  2630. begin
  2631. result := first_pi;
  2632. end;
  2633. in_abs_real:
  2634. begin
  2635. result := first_abs_real;
  2636. end;
  2637. in_abs_long:
  2638. begin
  2639. result := first_abs_long;
  2640. end;
  2641. in_sqr_real:
  2642. begin
  2643. result := first_sqr_real;
  2644. end;
  2645. in_sqrt_real:
  2646. begin
  2647. result := first_sqrt_real;
  2648. end;
  2649. in_ln_real:
  2650. begin
  2651. result := first_ln_real;
  2652. end;
  2653. {$ifdef SUPPORT_MMX}
  2654. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2655. begin
  2656. end;
  2657. {$endif SUPPORT_MMX}
  2658. in_assert_x_y :
  2659. begin
  2660. expectloc:=LOC_VOID;
  2661. end;
  2662. in_low_x,
  2663. in_high_x:
  2664. internalerror(200104047);
  2665. in_slice_x:
  2666. internalerror(2005101501);
  2667. in_ord_x,
  2668. in_chr_byte:
  2669. begin
  2670. { should not happend as it's converted to typeconv }
  2671. internalerror(200104045);
  2672. end;
  2673. in_ofs_x :
  2674. internalerror(2000101001);
  2675. in_seg_x :
  2676. internalerror(200104046);
  2677. in_settextbuf_file_x,
  2678. in_reset_typedfile,
  2679. in_rewrite_typedfile,
  2680. in_str_x_string,
  2681. in_val_x,
  2682. in_read_x,
  2683. in_readln_x,
  2684. in_write_x,
  2685. in_writeln_x :
  2686. begin
  2687. { should be handled by pass_typecheck }
  2688. internalerror(200108234);
  2689. end;
  2690. in_get_frame:
  2691. begin
  2692. include(current_procinfo.flags,pi_needs_stackframe);
  2693. expectloc:=LOC_CREGISTER;
  2694. end;
  2695. in_get_caller_frame:
  2696. begin
  2697. expectloc:=LOC_REGISTER;
  2698. end;
  2699. in_get_caller_addr:
  2700. begin
  2701. expectloc:=LOC_REGISTER;
  2702. end;
  2703. in_prefetch_var:
  2704. begin
  2705. expectloc:=LOC_VOID;
  2706. end;
  2707. in_unaligned_x:
  2708. begin
  2709. expectloc:=tcallparanode(left).left.expectloc;
  2710. end;
  2711. in_rol_x,
  2712. in_rol_x_x,
  2713. in_ror_x,
  2714. in_ror_x_x,
  2715. in_sar_x,
  2716. in_sar_x_y:
  2717. expectloc:=LOC_REGISTER;
  2718. else
  2719. internalerror(89);
  2720. end;
  2721. end;
  2722. {$maxfpuregisters default}
  2723. function tinlinenode.docompare(p: tnode): boolean;
  2724. begin
  2725. docompare :=
  2726. inherited docompare(p) and
  2727. (inlinenumber = tinlinenode(p).inlinenumber);
  2728. end;
  2729. function tinlinenode.first_pi : tnode;
  2730. begin
  2731. result:=crealconstnode.create(getpi,pbestrealtype^);
  2732. end;
  2733. function tinlinenode.first_arctan_real : tnode;
  2734. begin
  2735. { create the call to the helper }
  2736. { on entry left node contains the parameter }
  2737. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2738. ccallparanode.create(left,nil));
  2739. left := nil;
  2740. end;
  2741. function tinlinenode.first_abs_real : tnode;
  2742. begin
  2743. { create the call to the helper }
  2744. { on entry left node contains the parameter }
  2745. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2746. ccallparanode.create(left,nil));
  2747. left := nil;
  2748. end;
  2749. function tinlinenode.first_sqr_real : tnode;
  2750. begin
  2751. { create the call to the helper }
  2752. { on entry left node contains the parameter }
  2753. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2754. ccallparanode.create(left,nil)),resultdef);
  2755. left := nil;
  2756. end;
  2757. function tinlinenode.first_sqrt_real : tnode;
  2758. begin
  2759. { create the call to the helper }
  2760. { on entry left node contains the parameter }
  2761. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2762. ccallparanode.create(left,nil)),resultdef);
  2763. left := nil;
  2764. end;
  2765. function tinlinenode.first_ln_real : tnode;
  2766. begin
  2767. { create the call to the helper }
  2768. { on entry left node contains the parameter }
  2769. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2770. ccallparanode.create(left,nil));
  2771. left := nil;
  2772. end;
  2773. function tinlinenode.first_cos_real : tnode;
  2774. begin
  2775. { create the call to the helper }
  2776. { on entry left node contains the parameter }
  2777. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2778. ccallparanode.create(left,nil));
  2779. left := nil;
  2780. end;
  2781. function tinlinenode.first_sin_real : tnode;
  2782. begin
  2783. { create the call to the helper }
  2784. { on entry left node contains the parameter }
  2785. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2786. ccallparanode.create(left,nil));
  2787. left := nil;
  2788. end;
  2789. function tinlinenode.first_exp_real : tnode;
  2790. begin
  2791. { create the call to the helper }
  2792. { on entry left node contains the parameter }
  2793. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2794. left := nil;
  2795. end;
  2796. function tinlinenode.first_int_real : tnode;
  2797. begin
  2798. { create the call to the helper }
  2799. { on entry left node contains the parameter }
  2800. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2801. left := nil;
  2802. end;
  2803. function tinlinenode.first_frac_real : tnode;
  2804. begin
  2805. { create the call to the helper }
  2806. { on entry left node contains the parameter }
  2807. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2808. left := nil;
  2809. end;
  2810. function tinlinenode.first_round_real : tnode;
  2811. begin
  2812. { create the call to the helper }
  2813. { on entry left node contains the parameter }
  2814. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2815. left := nil;
  2816. end;
  2817. function tinlinenode.first_trunc_real : tnode;
  2818. begin
  2819. { create the call to the helper }
  2820. { on entry left node contains the parameter }
  2821. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2822. left := nil;
  2823. end;
  2824. function tinlinenode.first_abs_long : tnode;
  2825. begin
  2826. expectloc:=LOC_REGISTER;
  2827. result:=nil;
  2828. end;
  2829. function tinlinenode.first_pack_unpack: tnode;
  2830. var
  2831. loopstatement : tstatementnode;
  2832. loop : tblocknode;
  2833. loopvar : ttempcreatenode;
  2834. tempnode,
  2835. source,
  2836. target,
  2837. index,
  2838. unpackednode,
  2839. packednode,
  2840. sourcevecindex,
  2841. targetvecindex,
  2842. loopbody : tnode;
  2843. temprangedef : tdef;
  2844. ulorange,
  2845. uhirange,
  2846. plorange,
  2847. phirange : TConstExprInt;
  2848. begin
  2849. { transform into a for loop which assigns the data of the (un)packed }
  2850. { array to the other one }
  2851. source := left;
  2852. if (inlinenumber = in_unpack_x_y_z) then
  2853. begin
  2854. target := tcallparanode(source).right;
  2855. index := tcallparanode(target).right;
  2856. packednode := tcallparanode(source).left;
  2857. unpackednode := tcallparanode(target).left;
  2858. end
  2859. else
  2860. begin
  2861. index := tcallparanode(source).right;
  2862. target := tcallparanode(index).right;
  2863. packednode := tcallparanode(target).left;
  2864. unpackednode := tcallparanode(source).left;
  2865. end;
  2866. source := tcallparanode(source).left;
  2867. target := tcallparanode(target).left;
  2868. index := tcallparanode(index).left;
  2869. loop := internalstatements(loopstatement);
  2870. loopvar := ctempcreatenode.create(
  2871. tarraydef(packednode.resultdef).rangedef,
  2872. tarraydef(packednode.resultdef).rangedef.size,
  2873. tt_persistent,true);
  2874. addstatement(loopstatement,loopvar);
  2875. { For range checking: we have to convert to an integer type (in case the index type }
  2876. { is an enum), add the index and loop variable together, convert the result }
  2877. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2878. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2879. { errors }
  2880. temprangedef:=nil;
  2881. getrange(unpackednode.resultdef,ulorange,uhirange);
  2882. getrange(packednode.resultdef,plorange,phirange);
  2883. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2884. sourcevecindex := ctemprefnode.create(loopvar);
  2885. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2886. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2887. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2888. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2889. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2890. if (inlinenumber = in_pack_x_y_z) then
  2891. begin
  2892. { swap source and target vec indices }
  2893. tempnode := sourcevecindex;
  2894. sourcevecindex := targetvecindex;
  2895. targetvecindex := tempnode;
  2896. end;
  2897. { create the assignment in the loop body }
  2898. loopbody :=
  2899. cassignmentnode.create(
  2900. cvecnode.create(target.getcopy,targetvecindex),
  2901. cvecnode.create(source.getcopy,sourcevecindex)
  2902. );
  2903. { create the actual for loop }
  2904. tempnode := cfornode.create(
  2905. ctemprefnode.create(loopvar),
  2906. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2907. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2908. loopbody,
  2909. false);
  2910. addstatement(loopstatement,tempnode);
  2911. { free the loop counter }
  2912. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2913. result := loop;
  2914. end;
  2915. begin
  2916. cinlinenode:=tinlinenode;
  2917. end.