ninl.pas 116 KB

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