ninl.pas 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917
  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(forinline : boolean): 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. protected
  37. { All the following routines currently
  38. call compilerprocs, unless they are
  39. overridden in which case, the code
  40. generator handles them.
  41. }
  42. function first_pi: tnode ; virtual;
  43. function first_arctan_real: tnode; virtual;
  44. function first_abs_real: tnode; virtual;
  45. function first_sqr_real: tnode; virtual;
  46. function first_sqrt_real: tnode; virtual;
  47. function first_ln_real: tnode; virtual;
  48. function first_cos_real: tnode; virtual;
  49. function first_sin_real: tnode; virtual;
  50. function first_exp_real: tnode; virtual;
  51. function first_frac_real: tnode; virtual;
  52. function first_round_real: tnode; virtual;
  53. function first_trunc_real: tnode; virtual;
  54. function first_int_real: tnode; virtual;
  55. function first_abs_long: tnode; virtual;
  56. function first_IncludeExclude: tnode; virtual;
  57. function first_setlength: tnode; virtual;
  58. function first_copy: tnode; virtual;
  59. { This one by default generates an internal error, because such
  60. nodes are not generated by the parser. It's however used internally
  61. by the JVM backend to create new dynamic arrays. }
  62. function first_new: tnode; virtual;
  63. function first_length: tnode; virtual;
  64. function first_box: tnode; virtual; abstract;
  65. function first_unbox: tnode; virtual; abstract;
  66. function first_assigned: tnode; virtual;
  67. function first_assert: tnode; virtual;
  68. private
  69. function handle_str: tnode;
  70. function handle_reset_rewrite_typed: tnode;
  71. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  72. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  73. function handle_read_write: tnode;
  74. function handle_val: tnode;
  75. function handle_default: tnode;
  76. function handle_setlength: tnode;
  77. function handle_copy: tnode;
  78. function handle_box: tnode;
  79. function handle_unbox: tnode;
  80. end;
  81. tinlinenodeclass = class of tinlinenode;
  82. var
  83. cinlinenode : tinlinenodeclass = tinlinenode;
  84. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  85. implementation
  86. uses
  87. verbose,globals,systems,constexp,
  88. globtype, cutils,
  89. symconst,symdef,symsym,symtable,paramgr,defutil,symbase,
  90. pass_1,
  91. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  92. nobjc,objcdef,
  93. cgbase,procinfo
  94. ;
  95. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  96. begin
  97. geninlinenode:=cinlinenode.create(number,is_const,l);
  98. end;
  99. {*****************************************************************************
  100. TINLINENODE
  101. *****************************************************************************}
  102. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  103. begin
  104. inherited create(inlinen,l);
  105. if is_const then
  106. include(flags,nf_inlineconst);
  107. inlinenumber:=number;
  108. end;
  109. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  110. begin
  111. inherited ppuload(t,ppufile);
  112. inlinenumber:=ppufile.getbyte;
  113. end;
  114. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  115. begin
  116. inherited ppuwrite(ppufile);
  117. ppufile.putbyte(inlinenumber);
  118. end;
  119. function tinlinenode.dogetcopy : tnode;
  120. var
  121. n : tinlinenode;
  122. begin
  123. n:=tinlinenode(inherited dogetcopy);
  124. n.inlinenumber:=inlinenumber;
  125. result:=n;
  126. end;
  127. function tinlinenode.handle_str : tnode;
  128. var
  129. lenpara,
  130. fracpara,
  131. newparas,
  132. tmppara,
  133. dest,
  134. source : tcallparanode;
  135. procname: string;
  136. is_real,is_enum : boolean;
  137. rt : aint;
  138. begin
  139. result := cerrornode.create;
  140. { get destination string }
  141. dest := tcallparanode(left);
  142. { get source para (number) }
  143. source := dest;
  144. while assigned(source.right) do
  145. source := tcallparanode(source.right);
  146. { destination parameter must be a normal (not a colon) parameter, this
  147. check is needed because str(v:len) also has 2 parameters }
  148. if (source=dest) or
  149. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  150. begin
  151. CGMessage1(parser_e_wrong_parameter_size,'Str');
  152. exit;
  153. end;
  154. { in case we are in a generic definition, we cannot
  155. do all checks, the parameters might be type parameters }
  156. if df_generic in current_procinfo.procdef.defoptions then
  157. begin
  158. result.Free;
  159. result:=nil;
  160. resultdef:=voidtype;
  161. exit;
  162. end;
  163. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  164. is_enum:=source.left.resultdef.typ=enumdef;
  165. if ((dest.left.resultdef.typ<>stringdef) and
  166. not(is_chararray(dest.left.resultdef))) or
  167. not(is_real or is_enum or
  168. (source.left.resultdef.typ=orddef)) then
  169. begin
  170. CGMessagePos(fileinfo,parser_e_illegal_expression);
  171. exit;
  172. end;
  173. { get len/frac parameters }
  174. lenpara := nil;
  175. fracpara := nil;
  176. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  177. begin
  178. lenpara := tcallparanode(dest.right);
  179. { we can let the callnode do the type checking of these parameters too, }
  180. { but then the error messages aren't as nice }
  181. if not is_integer(lenpara.resultdef) then
  182. begin
  183. CGMessagePos1(lenpara.fileinfo,
  184. type_e_integer_expr_expected,lenpara.resultdef.typename);
  185. exit;
  186. end;
  187. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  188. begin
  189. { parameters are in reverse order! }
  190. fracpara := lenpara;
  191. lenpara := tcallparanode(lenpara.right);
  192. if not is_real then
  193. begin
  194. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  195. exit
  196. end;
  197. if not is_integer(lenpara.resultdef) then
  198. begin
  199. CGMessagePos1(lenpara.fileinfo,
  200. type_e_integer_expr_expected,lenpara.resultdef.typename);
  201. exit;
  202. end;
  203. end;
  204. end;
  205. { generate the parameter list for the compilerproc }
  206. newparas := dest;
  207. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  208. if is_real then
  209. begin
  210. { insert realtype parameter }
  211. if not is_currency(source.resultdef) then
  212. begin
  213. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  214. newparas.right := ccallparanode.create(cordconstnode.create(
  215. rt,s32inttype,true),newparas.right);
  216. tmppara:=tcallparanode(newparas.right);
  217. end
  218. else
  219. tmppara:=newparas;
  220. { if necessary, insert a fraction parameter }
  221. if not assigned(fracpara) then
  222. begin
  223. tmppara.right := ccallparanode.create(
  224. cordconstnode.create(int64(-1),s32inttype,false),
  225. tmppara.right);
  226. fracpara := tcallparanode(tmppara.right);
  227. end;
  228. { if necessary, insert a length para }
  229. if not assigned(lenpara) then
  230. fracpara.right := ccallparanode.create(
  231. cordconstnode.create(int64(-32767),s32inttype,false),
  232. fracpara.right);
  233. end
  234. else if is_enum then
  235. begin
  236. {Insert a reference to the ord2string index.}
  237. newparas.right:=Ccallparanode.create(
  238. Caddrnode.create_internal(
  239. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  240. ),
  241. newparas.right);
  242. {Insert a reference to the typinfo.}
  243. newparas.right:=Ccallparanode.create(
  244. Caddrnode.create_internal(
  245. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  246. ),
  247. newparas.right);
  248. {Insert a type conversion from the enumeration to longint.}
  249. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  250. typecheckpass(source.left);
  251. { if necessary, insert a length para }
  252. if not assigned(lenpara) then
  253. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  254. Ccallparanode.create(
  255. cordconstnode.create(int64(-1),s32inttype,false),
  256. Tcallparanode(Tcallparanode(newparas.right).right).right
  257. );
  258. end
  259. else
  260. { for a normal parameter, insert a only length parameter if one is missing }
  261. if not assigned(lenpara) then
  262. newparas.right := ccallparanode.create(cordconstnode.create(int64(-1),s32inttype,false),
  263. newparas.right);
  264. { remove the parameters from the original node so they won't get disposed, }
  265. { since they're reused }
  266. left := nil;
  267. { create procedure name }
  268. if is_chararray(dest.resultdef) then
  269. procname:='fpc_chararray_'
  270. else
  271. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  272. if is_real then
  273. if is_currency(source.resultdef) then
  274. procname := procname + 'currency'
  275. else
  276. procname := procname + 'float'
  277. else if is_enum then
  278. procname:=procname+'enum'
  279. else
  280. case torddef(source.resultdef).ordtype of
  281. {$ifdef cpu64bitaddr}
  282. u64bit:
  283. procname := procname + 'uint';
  284. {$else}
  285. u32bit:
  286. procname := procname + 'uint';
  287. u64bit:
  288. procname := procname + 'qword';
  289. scurrency,
  290. s64bit:
  291. procname := procname + 'int64';
  292. {$endif}
  293. pasbool8,pasbool16,pasbool32,pasbool64,
  294. bool8bit,bool16bit,bool32bit,bool64bit:
  295. procname := procname + 'bool';
  296. else
  297. procname := procname + 'sint';
  298. end;
  299. { for ansistrings insert the encoding argument }
  300. if is_ansistring(dest.resultdef) then
  301. newparas:=ccallparanode.create(cordconstnode.create(
  302. getparaencoding(dest.resultdef),u16inttype,true),newparas);
  303. { free the errornode we generated in the beginning }
  304. result.free;
  305. { create the call node, }
  306. result := ccallnode.createintern(procname,newparas);
  307. end;
  308. function tinlinenode.handle_default: tnode;
  309. function getdefaultvarsym(def:tdef):tnode;
  310. var
  311. hashedid : thashedidstring;
  312. srsym : tsym;
  313. srsymtable : tsymtable;
  314. defaultname : tidstring;
  315. begin
  316. if not assigned(def) or
  317. not (def.typ in [arraydef,recorddef,variantdef,objectdef,procvardef]) or
  318. ((def.typ=objectdef) and not is_object(def)) then
  319. internalerror(201202101);
  320. defaultname:=make_mangledname('zero',def.owner,def.typesym.Name);
  321. hashedid.id:=defaultname;
  322. { the default sym is always part of the current procedure/function }
  323. srsymtable:=current_procinfo.procdef.localst;
  324. srsym:=tsym(srsymtable.findwithhash(hashedid));
  325. if not assigned(srsym) then
  326. begin
  327. { no valid default variable found, so create it }
  328. srsym:=tlocalvarsym.create(defaultname,vs_const,def,[]);
  329. srsymtable.insert(srsym);
  330. { mark the staticvarsym as typedconst }
  331. include(tabstractvarsym(srsym).varoptions,vo_is_typed_const);
  332. include(tabstractvarsym(srsym).varoptions,vo_is_default_var);
  333. { The variable has a value assigned }
  334. tabstractvarsym(srsym).varstate:=vs_initialised;
  335. { the variable can't be placed in a register }
  336. tabstractvarsym(srsym).varregable:=vr_none;
  337. end;
  338. result:=cloadnode.create(srsym,srsymtable);
  339. end;
  340. var
  341. def : tdef;
  342. begin
  343. if not assigned(left) or (left.nodetype<>typen) then
  344. internalerror(2012032101);
  345. def:=ttypenode(left).typedef;
  346. result:=nil;
  347. case def.typ of
  348. enumdef,
  349. orddef:
  350. { don't do a rangecheck as Default will also return 0
  351. for the following types (Delphi compatible):
  352. TRange1 = -10..-5;
  353. TRange2 = 5..10;
  354. TEnum = (a:=5;b:=10); }
  355. result:=cordconstnode.create(0,def,false);
  356. classrefdef,
  357. pointerdef:
  358. result:=cpointerconstnode.create(0,def);
  359. procvardef:
  360. if tprocvardef(def).size<>sizeof(pint) then
  361. result:=getdefaultvarsym(def)
  362. else
  363. result:=cpointerconstnode.create(0,def);
  364. stringdef:
  365. result:=cstringconstnode.createstr('');
  366. floatdef:
  367. result:=crealconstnode.create(0,def);
  368. objectdef:
  369. begin
  370. if is_implicit_pointer_object_type(def) then
  371. result:=cpointerconstnode.create(0,def)
  372. else
  373. if is_object(def) then
  374. begin
  375. { Delphi does not recursively check whether
  376. an object contains unsupported types }
  377. if not (m_delphi in current_settings.modeswitches) and
  378. not is_valid_for_default(def) then
  379. Message(type_e_type_not_allowed_for_default);
  380. result:=getdefaultvarsym(def);
  381. end
  382. else
  383. Message(type_e_type_not_allowed_for_default);
  384. end;
  385. variantdef,
  386. recorddef:
  387. begin
  388. { Delphi does not recursively check whether a record
  389. contains unsupported types }
  390. if (def.typ=recorddef) and not (m_delphi in current_settings.modeswitches) and
  391. not is_valid_for_default(def) then
  392. Message(type_e_type_not_allowed_for_default);
  393. result:=getdefaultvarsym(def);
  394. end;
  395. setdef:
  396. begin
  397. result:=csetconstnode.create(nil,def);
  398. New(tsetconstnode(result).value_set);
  399. tsetconstnode(result).value_set^:=[];
  400. end;
  401. arraydef:
  402. begin
  403. { can other array types be parsed by single_type? }
  404. if ado_isdynamicarray in tarraydef(def).arrayoptions then
  405. result:=cpointerconstnode.create(0,def)
  406. else
  407. begin
  408. result:=getdefaultvarsym(def);
  409. end;
  410. end;
  411. undefineddef:
  412. begin
  413. if sp_generic_dummy in def.typesym.symoptions then
  414. begin
  415. { this matches the error messages that are printed
  416. in case of non-Delphi modes }
  417. Message(parser_e_no_generics_as_types);
  418. Message(type_e_type_id_expected);
  419. end
  420. else
  421. result:=cpointerconstnode.create(0,def);
  422. end;
  423. else
  424. Message(type_e_type_not_allowed_for_default);
  425. end;
  426. if not assigned(result) then
  427. result:=cerrornode.create;
  428. end;
  429. function tinlinenode.handle_reset_rewrite_typed: tnode;
  430. begin
  431. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  432. { a typed file as argument and we don't have to check it again (JM) }
  433. { add the recsize parameter }
  434. { note: for some reason, the parameter of intern procedures with only one }
  435. { parameter is gets lifted out of its original tcallparanode (see round }
  436. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  437. left := ccallparanode.create(cordconstnode.create(
  438. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  439. ccallparanode.create(left,nil));
  440. { create the correct call }
  441. if m_iso in current_settings.modeswitches then
  442. begin
  443. if inlinenumber=in_reset_typedfile then
  444. result := ccallnode.createintern('fpc_reset_typed_iso',left)
  445. else
  446. result := ccallnode.createintern('fpc_rewrite_typed_iso',left);
  447. end
  448. else
  449. begin
  450. if inlinenumber=in_reset_typedfile then
  451. result := ccallnode.createintern('fpc_reset_typed',left)
  452. else
  453. result := ccallnode.createintern('fpc_rewrite_typed',left);
  454. end;
  455. { make sure left doesn't get disposed, since we use it in the new call }
  456. left := nil;
  457. end;
  458. procedure maybe_convert_to_string(var n: tnode);
  459. begin
  460. { stringconstnodes are arrays of char. It's much more }
  461. { efficient to write a constant string, so convert }
  462. { either to shortstring or ansistring depending on }
  463. { length }
  464. if (n.nodetype=stringconstn) then
  465. if is_chararray(n.resultdef) then
  466. if (tstringconstnode(n).len<=255) then
  467. inserttypeconv(n,cshortstringtype)
  468. else
  469. inserttypeconv(n,getansistringdef)
  470. else if is_widechararray(n.resultdef) then
  471. inserttypeconv(n,cunicodestringtype);
  472. end;
  473. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  474. {Read(ln)/write(ln) for text files.}
  475. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  476. var error_para,is_real,special_handling,found_error,do_read:boolean;
  477. p1:Tnode;
  478. nextpara,
  479. indexpara,
  480. lenpara,
  481. para,
  482. fracpara:Tcallparanode;
  483. temp:Ttempcreatenode;
  484. readfunctype:Tdef;
  485. name:string[63];
  486. begin
  487. para:=Tcallparanode(params);
  488. found_error:=false;
  489. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  490. while assigned(para) do
  491. begin
  492. { is this parameter faulty? }
  493. error_para:=false;
  494. { is this parameter a real? }
  495. is_real:=false;
  496. { type used for the read(), this is used to check
  497. whether a temp is needed for range checking }
  498. readfunctype:=nil;
  499. { can't read/write types }
  500. if (para.left.nodetype=typen) and not(ttypenode(para.left).typedef.typ=undefineddef) then
  501. begin
  502. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  503. error_para := true;
  504. end;
  505. { support writeln(procvar) }
  506. if para.left.resultdef.typ=procvardef then
  507. begin
  508. p1:=ccallnode.create_procvar(nil,para.left);
  509. typecheckpass(p1);
  510. para.left:=p1;
  511. end;
  512. if inlinenumber in [in_write_x,in_writeln_x] then
  513. { prefer strings to chararrays }
  514. maybe_convert_to_string(para.left);
  515. case para.left.resultdef.typ of
  516. stringdef :
  517. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  518. pointerdef :
  519. begin
  520. if (not is_pchar(para.left.resultdef)) or do_read then
  521. begin
  522. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  523. error_para := true;
  524. end
  525. else
  526. name:=procprefixes[do_read]+'pchar_as_pointer';
  527. end;
  528. floatdef :
  529. begin
  530. is_real:=true;
  531. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  532. name := procprefixes[do_read]+'currency'
  533. else
  534. begin
  535. name := procprefixes[do_read]+'float';
  536. readfunctype:=pbestrealtype^;
  537. end;
  538. end;
  539. enumdef:
  540. begin
  541. name:=procprefixes[do_read]+'enum';
  542. readfunctype:=s32inttype;
  543. end;
  544. orddef :
  545. begin
  546. case Torddef(para.left.resultdef).ordtype of
  547. {$ifdef cpu64bitaddr}
  548. s64bit,
  549. {$endif cpu64bitaddr}
  550. s8bit,
  551. s16bit,
  552. s32bit :
  553. begin
  554. name := procprefixes[do_read]+'sint';
  555. readfunctype:=sinttype;
  556. end;
  557. {$ifdef cpu64bitaddr}
  558. u64bit,
  559. {$endif cpu64bitaddr}
  560. u8bit,
  561. u16bit,
  562. u32bit :
  563. begin
  564. name := procprefixes[do_read]+'uint';
  565. readfunctype:=uinttype;
  566. end;
  567. uchar :
  568. begin
  569. name := procprefixes[do_read]+'char';
  570. { iso pascal needs a different handler }
  571. if (m_iso in current_settings.modeswitches) and do_read then
  572. name:=name+'_iso';
  573. readfunctype:=cansichartype;
  574. end;
  575. uwidechar :
  576. begin
  577. name := procprefixes[do_read]+'widechar';
  578. readfunctype:=cwidechartype;
  579. end;
  580. {$ifndef cpu64bitaddr}
  581. s64bit :
  582. begin
  583. name := procprefixes[do_read]+'int64';
  584. readfunctype:=s64inttype;
  585. end;
  586. u64bit :
  587. begin
  588. name := procprefixes[do_read]+'qword';
  589. readfunctype:=u64inttype;
  590. end;
  591. {$endif not cpu64bitaddr}
  592. scurrency:
  593. begin
  594. name := procprefixes[do_read]+'currency';
  595. readfunctype:=s64currencytype;
  596. is_real:=true;
  597. end;
  598. pasbool8,
  599. pasbool16,
  600. pasbool32,
  601. pasbool64,
  602. bool8bit,
  603. bool16bit,
  604. bool32bit,
  605. bool64bit:
  606. if do_read then
  607. begin
  608. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  609. error_para := true;
  610. end
  611. else
  612. begin
  613. name := procprefixes[do_read]+'boolean';
  614. readfunctype:=pasbool8type;
  615. end
  616. else
  617. begin
  618. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  619. error_para := true;
  620. end;
  621. end;
  622. end;
  623. variantdef :
  624. name:=procprefixes[do_read]+'variant';
  625. arraydef :
  626. begin
  627. if is_chararray(para.left.resultdef) then
  628. name := procprefixes[do_read]+'pchar_as_array'
  629. else
  630. begin
  631. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  632. error_para := true;
  633. end
  634. end;
  635. { generic parameter }
  636. undefineddef:
  637. { don't try to generate any code for a writeln on a generic parameter }
  638. error_para:=true;
  639. else
  640. begin
  641. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  642. error_para := true;
  643. end;
  644. end;
  645. { iso pascal needs a different handler }
  646. if (m_iso in current_settings.modeswitches) and not(do_read) then
  647. name:=name+'_iso';
  648. { check for length/fractional colon para's }
  649. fracpara:=nil;
  650. lenpara:=nil;
  651. indexpara:=nil;
  652. if assigned(para.right) and
  653. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  654. begin
  655. lenpara := tcallparanode(para.right);
  656. if assigned(lenpara.right) and
  657. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  658. fracpara:=tcallparanode(lenpara.right);
  659. end;
  660. { get the next parameter now already, because we're going }
  661. { to muck around with the pointers }
  662. if assigned(fracpara) then
  663. nextpara := tcallparanode(fracpara.right)
  664. else if assigned(lenpara) then
  665. nextpara := tcallparanode(lenpara.right)
  666. else
  667. nextpara := tcallparanode(para.right);
  668. { check if a fracpara is allowed }
  669. if assigned(fracpara) and not is_real then
  670. begin
  671. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  672. error_para := true;
  673. end
  674. else if assigned(lenpara) and do_read then
  675. begin
  676. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  677. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  678. error_para := true;
  679. end;
  680. { adjust found_error }
  681. found_error := found_error or error_para;
  682. if not error_para then
  683. begin
  684. special_handling:=false;
  685. { create dummy frac/len para's if necessary }
  686. if not do_read then
  687. begin
  688. { difference in default value for floats and the rest :( }
  689. if not is_real then
  690. begin
  691. if not assigned(lenpara) then
  692. begin
  693. if m_iso in current_settings.modeswitches then
  694. lenpara := ccallparanode.create(
  695. cordconstnode.create(-1,s32inttype,false),nil)
  696. else
  697. lenpara := ccallparanode.create(
  698. cordconstnode.create(0,s32inttype,false),nil);
  699. end
  700. else
  701. { make sure we don't pass the successive }
  702. { parameters too. We also already have a }
  703. { reference to the next parameter in }
  704. { nextpara }
  705. lenpara.right := nil;
  706. end
  707. else
  708. begin
  709. if not assigned(lenpara) then
  710. lenpara := ccallparanode.create(
  711. cordconstnode.create(int64(-32767),s32inttype,false),nil);
  712. { also create a default fracpara if necessary }
  713. if not assigned(fracpara) then
  714. fracpara := ccallparanode.create(
  715. cordconstnode.create(int64(-1),s32inttype,false),nil);
  716. { add it to the lenpara }
  717. lenpara.right := fracpara;
  718. if not is_currency(para.left.resultdef) then
  719. begin
  720. { and add the realtype para (this also removes the link }
  721. { to any parameters coming after it) }
  722. fracpara.right := ccallparanode.create(
  723. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  724. s32inttype,true),nil);
  725. end
  726. else
  727. fracpara.right:=nil;
  728. end;
  729. if para.left.resultdef.typ=enumdef then
  730. begin
  731. {To write(ln) an enum we need a some extra parameters.}
  732. {Insert a reference to the ord2string index.}
  733. indexpara:=Ccallparanode.create(
  734. Caddrnode.create_internal(
  735. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  736. ),
  737. nil);
  738. {Insert a reference to the typinfo.}
  739. indexpara:=Ccallparanode.create(
  740. Caddrnode.create_internal(
  741. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  742. ),
  743. indexpara);
  744. {Insert a type conversion to to convert the enum to longint.}
  745. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  746. typecheckpass(para.left);
  747. end;
  748. end
  749. else
  750. begin
  751. {To read(ln) an enum we need a an extra parameter.}
  752. if para.left.resultdef.typ=enumdef then
  753. begin
  754. {Insert a reference to the string2ord index.}
  755. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  756. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  757. ),nil);
  758. {Insert a type conversion to to convert the enum to longint.}
  759. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  760. typecheckpass(para.left);
  761. end;
  762. { special handling of reading small numbers, because the helpers }
  763. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  764. { use functions because then the call to FPC_IOCHECK destroys }
  765. { their result before we can store it }
  766. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  767. special_handling:=true;
  768. end;
  769. if special_handling then
  770. begin
  771. { since we're not going to pass the parameter as var-parameter }
  772. { to the read function, manually check whether the parameter }
  773. { can be used as var-parameter (e.g., whether it isn't a }
  774. { property) }
  775. valid_for_var(para.left,true);
  776. { create the parameter list: the temp ... }
  777. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  778. addstatement(Tstatementnode(newstatement),temp);
  779. { ... and the file }
  780. p1 := ccallparanode.create(ctemprefnode.create(temp),
  781. filepara.getcopy);
  782. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  783. { create the call to the helper }
  784. addstatement(Tstatementnode(newstatement),
  785. ccallnode.createintern(name,tcallparanode(p1)));
  786. { assign the result to the original var (this automatically }
  787. { takes care of range checking) }
  788. addstatement(Tstatementnode(newstatement),
  789. cassignmentnode.create(para.left,
  790. ctemprefnode.create(temp)));
  791. { release the temp location }
  792. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  793. { statement of para is used }
  794. para.left := nil;
  795. { free the enclosing tcallparanode, but not the }
  796. { parameters coming after it }
  797. para.right := nil;
  798. para.free;
  799. end
  800. else
  801. { read of non s/u-8/16bit, or a write }
  802. begin
  803. { add the filepara to the current parameter }
  804. para.right := filepara.getcopy;
  805. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  806. already linked with the lenpara if necessary).}
  807. if indexpara=nil then
  808. Tcallparanode(para.right).right:=lenpara
  809. else
  810. begin
  811. if lenpara=nil then
  812. Tcallparanode(para.right).right:=indexpara
  813. else
  814. begin
  815. Tcallparanode(para.right).right:=lenpara;
  816. lenpara.right:=indexpara;
  817. end;
  818. { indexpara.right:=lenpara;}
  819. end;
  820. { in case of writing a chararray, add whether it's zero-based }
  821. if para.left.resultdef.typ=arraydef then
  822. para := ccallparanode.create(cordconstnode.create(
  823. ord(tarraydef(para.left.resultdef).lowrange=0),pasbool8type,false),para)
  824. else
  825. { in case of reading an ansistring pass a codepage argument }
  826. if do_read and is_ansistring(para.left.resultdef) then
  827. para:=ccallparanode.create(cordconstnode.create(
  828. getparaencoding(para.left.resultdef),u16inttype,true),para);
  829. { create the call statement }
  830. addstatement(Tstatementnode(newstatement),
  831. ccallnode.createintern(name,para));
  832. end
  833. end
  834. else
  835. { error_para = true }
  836. begin
  837. { free the parameter, since it isn't referenced anywhere anymore }
  838. para.right := nil;
  839. para.free;
  840. if assigned(lenpara) then
  841. begin
  842. lenpara.right := nil;
  843. lenpara.free;
  844. end;
  845. if assigned(fracpara) then
  846. begin
  847. fracpara.right := nil;
  848. fracpara.free;
  849. end;
  850. end;
  851. { process next parameter }
  852. para := nextpara;
  853. end;
  854. { if no error, add the write(ln)/read(ln) end calls }
  855. if not found_error then
  856. begin
  857. case inlinenumber of
  858. in_read_x,
  859. in_readstr_x:
  860. name:='fpc_read_end';
  861. in_write_x,
  862. in_writestr_x:
  863. name:='fpc_write_end';
  864. in_readln_x:
  865. begin
  866. name:='fpc_readln_end';
  867. if m_iso in current_settings.modeswitches then
  868. name:=name+'_iso';
  869. end;
  870. in_writeln_x:
  871. name:='fpc_writeln_end';
  872. end;
  873. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara));
  874. end;
  875. handle_text_read_write:=found_error;
  876. end;
  877. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  878. {Read/write for typed files.}
  879. const procprefixes:array[boolean] of string[15]=('fpc_typed_write','fpc_typed_read');
  880. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  881. var found_error,do_read,is_rwstr:boolean;
  882. para,nextpara:Tcallparanode;
  883. p1:Tnode;
  884. temp:Ttempcreatenode;
  885. begin
  886. found_error:=false;
  887. para:=Tcallparanode(params);
  888. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  889. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  890. { add the typesize to the filepara }
  891. if filepara.resultdef.typ=filedef then
  892. filepara.right := ccallparanode.create(cordconstnode.create(
  893. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  894. { check for "no parameters" (you need at least one extra para for typed files) }
  895. if not assigned(para) then
  896. begin
  897. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  898. found_error := true;
  899. end;
  900. { process all parameters }
  901. while assigned(para) do
  902. begin
  903. { check if valid parameter }
  904. if para.left.nodetype=typen then
  905. begin
  906. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  907. found_error := true;
  908. end;
  909. { support writeln(procvar) }
  910. if (para.left.resultdef.typ=procvardef) then
  911. begin
  912. p1:=ccallnode.create_procvar(nil,para.left);
  913. typecheckpass(p1);
  914. para.left:=p1;
  915. end;
  916. if filepara.resultdef.typ=filedef then
  917. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  918. if assigned(para.right) and
  919. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  920. begin
  921. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  922. { skip all colon para's }
  923. nextpara := tcallparanode(tcallparanode(para.right).right);
  924. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  925. nextpara := tcallparanode(nextpara.right);
  926. found_error := true;
  927. end
  928. else
  929. { get next parameter }
  930. nextpara := tcallparanode(para.right);
  931. { When we have a call, we have a problem: you can't pass the }
  932. { result of a call as a formal const parameter. Solution: }
  933. { assign the result to a temp and pass this temp as parameter }
  934. { This is not very efficient, but write(typedfile,x) is }
  935. { already slow by itself anyway (no buffering) (JM) }
  936. { Actually, thge same goes for every non-simple expression }
  937. { (such as an addition, ...) -> put everything but load nodes }
  938. { into temps (JM) }
  939. { of course, this must only be allowed for writes!!! (JM) }
  940. if not(do_read) and (para.left.nodetype <> loadn) then
  941. begin
  942. { create temp for result }
  943. temp := ctempcreatenode.create(para.left.resultdef,
  944. para.left.resultdef.size,tt_persistent,false);
  945. addstatement(Tstatementnode(newstatement),temp);
  946. { assign result to temp }
  947. addstatement(Tstatementnode(newstatement),
  948. cassignmentnode.create(ctemprefnode.create(temp),
  949. para.left));
  950. { replace (reused) paranode with temp }
  951. para.left := ctemprefnode.create(temp);
  952. end;
  953. { add fileparameter }
  954. para.right := filepara.getcopy;
  955. { create call statment }
  956. { since the parameters are in the correct order, we have to insert }
  957. { the statements always at the end of the current block }
  958. addstatement(Tstatementnode(newstatement),
  959. Ccallnode.createintern(procprefixes[do_read],para
  960. ));
  961. { if we used a temp, free it }
  962. if para.left.nodetype = temprefn then
  963. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  964. { process next parameter }
  965. para := nextpara;
  966. end;
  967. { free the file parameter }
  968. filepara.free;
  969. handle_typed_read_write:=found_error;
  970. end;
  971. function tinlinenode.handle_read_write: tnode;
  972. var
  973. filepara,
  974. nextpara,
  975. params : tcallparanode;
  976. newstatement : tstatementnode;
  977. newblock : tblocknode;
  978. filetemp : Ttempcreatenode;
  979. name : string[31];
  980. textsym : ttypesym;
  981. is_typed,
  982. do_read,
  983. is_rwstr,
  984. found_error : boolean;
  985. begin
  986. filepara := nil;
  987. is_typed := false;
  988. filetemp := nil;
  989. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  990. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  991. { if we fail, we can quickly exit this way. We must generate something }
  992. { instead of the inline node, because firstpass will bomb with an }
  993. { internalerror if it encounters a read/write }
  994. result := cerrornode.create;
  995. { reverse the parameters (needed to get the colon parameters in the }
  996. { correct order when processing write(ln) }
  997. left := reverseparameters(tcallparanode(left));
  998. if is_rwstr then
  999. begin
  1000. filepara := tcallparanode(left);
  1001. { needs at least two parameters: source/dest string + min. 1 value }
  1002. if not(assigned(filepara)) or
  1003. not(assigned(filepara.right)) then
  1004. begin
  1005. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  1006. exit;
  1007. end
  1008. else if (filepara.resultdef.typ <> stringdef) then
  1009. begin
  1010. { convert chararray to string, or give an appropriate error message }
  1011. { (if you want to optimize to use shortstring, keep in mind that }
  1012. { readstr internally always uses ansistring, and to account for }
  1013. { chararrays with > 255 characters) }
  1014. inserttypeconv(filepara.left,getansistringdef);
  1015. filepara.resultdef:=filepara.left.resultdef;
  1016. if codegenerror then
  1017. exit;
  1018. end
  1019. end
  1020. else if assigned(left) then
  1021. begin
  1022. { check if we have a file parameter and if yes, what kind it is }
  1023. filepara := tcallparanode(left);
  1024. if (filepara.resultdef.typ=filedef) then
  1025. begin
  1026. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  1027. begin
  1028. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  1029. exit;
  1030. end
  1031. else
  1032. begin
  1033. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  1034. begin
  1035. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  1036. begin
  1037. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  1038. exit;
  1039. end;
  1040. is_typed := true;
  1041. end
  1042. end;
  1043. end
  1044. else
  1045. filepara := nil;
  1046. end;
  1047. { create a blocknode in which the successive write/read statements will be }
  1048. { put, since they belong together. Also create a dummy statement already to }
  1049. { make inserting of additional statements easier }
  1050. newblock:=internalstatements(newstatement);
  1051. { if we don't have a filepara, create one containing the default }
  1052. if not assigned(filepara) or
  1053. is_rwstr then
  1054. begin
  1055. { since the input/output variables are threadvars loading them into
  1056. a temp once is faster. Create a temp which will hold a pointer to the file }
  1057. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1058. addstatement(newstatement,filetemp);
  1059. { make sure the resultdef of the temp (and as such of the }
  1060. { temprefs coming after it) is set (necessary because the }
  1061. { temprefs will be part of the filepara, of which we need }
  1062. { the resultdef later on and temprefs can only be }
  1063. { typecheckpassed if the resultdef of the temp is known) }
  1064. typecheckpass(tnode(filetemp));
  1065. if not is_rwstr then
  1066. begin
  1067. { assign the address of the file to the temp }
  1068. if do_read then
  1069. name := 'input'
  1070. else
  1071. name := 'output';
  1072. addstatement(newstatement,
  1073. cassignmentnode.create(ctemprefnode.create(filetemp),
  1074. ccallnode.createintern('fpc_get_'+name,nil)));
  1075. end
  1076. else
  1077. begin
  1078. if (do_read) then
  1079. name := 'fpc_setupreadstr_'
  1080. else
  1081. name := 'fpc_setupwritestr_';
  1082. name:=name+tstringdef(filepara.resultdef).stringtypname;
  1083. { remove the source/destination string parameter from the }
  1084. { parameter chain }
  1085. left:=filepara.right;
  1086. filepara.right:=nil;
  1087. { pass the source/destination string to the setup routine, which }
  1088. { will store the string's address in the returned textrec }
  1089. addstatement(newstatement,
  1090. cassignmentnode.create(ctemprefnode.create(filetemp),
  1091. ccallnode.createintern(name,filepara)));
  1092. end;
  1093. { create a new fileparameter as follows: file_type(temp^) }
  1094. { (so that we pass the value and not the address of the temp }
  1095. { to the read/write routine) }
  1096. textsym:=search_system_type('TEXT');
  1097. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  1098. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  1099. end
  1100. else
  1101. { remove filepara from the parameter chain }
  1102. begin
  1103. left := filepara.right;
  1104. filepara.right := nil;
  1105. { the file para is a var parameter, but it must be valid already }
  1106. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  1107. { check if we should make a temp to store the result of a complex }
  1108. { expression (better heuristics, anyone?) (JM) }
  1109. if (filepara.left.nodetype <> loadn) then
  1110. begin
  1111. { create a temp which will hold a pointer to the file }
  1112. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1113. { add it to the statements }
  1114. addstatement(newstatement,filetemp);
  1115. { make sure the resultdef of the temp (and as such of the }
  1116. { temprefs coming after it) is set (necessary because the }
  1117. { temprefs will be part of the filepara, of which we need }
  1118. { the resultdef later on and temprefs can only be }
  1119. { typecheckpassed if the resultdef of the temp is known) }
  1120. typecheckpass(tnode(filetemp));
  1121. { assign the address of the file to the temp }
  1122. addstatement(newstatement,
  1123. cassignmentnode.create(ctemprefnode.create(filetemp),
  1124. caddrnode.create_internal(filepara.left)));
  1125. typecheckpass(newstatement.left);
  1126. { create a new fileparameter as follows: file_type(temp^) }
  1127. { (so that we pass the value and not the address of the temp }
  1128. { to the read/write routine) }
  1129. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  1130. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  1131. { replace the old file para with the new one }
  1132. filepara.left := nil;
  1133. filepara.free;
  1134. filepara := nextpara;
  1135. end;
  1136. end;
  1137. { the resultdef of the filepara must be set since it's }
  1138. { used below }
  1139. filepara.get_paratype;
  1140. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  1141. { if something goes wrong or at the end of the procedure }
  1142. { we're going to reuse the paranodes, so make sure they don't get freed }
  1143. { twice }
  1144. params:=Tcallparanode(left);
  1145. left := nil;
  1146. if is_typed then
  1147. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),tnode(newstatement))
  1148. else
  1149. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),tnode(newstatement));
  1150. { if we found an error, simply delete the generated blocknode }
  1151. if found_error then
  1152. newblock.free
  1153. else
  1154. begin
  1155. { deallocate the temp for the file para if we used one }
  1156. if assigned(filetemp) then
  1157. addstatement(newstatement,ctempdeletenode.create(filetemp));
  1158. { otherwise return the newly generated block of instructions, }
  1159. { but first free the errornode we generated at the beginning }
  1160. result.free;
  1161. result := newblock
  1162. end;
  1163. end;
  1164. function tinlinenode.handle_val: tnode;
  1165. var
  1166. procname,
  1167. suffix : string[31];
  1168. sourcepara,
  1169. destpara,
  1170. codepara,
  1171. sizepara,
  1172. newparas : tcallparanode;
  1173. orgcode,tc : tnode;
  1174. newstatement : tstatementnode;
  1175. newblock : tblocknode;
  1176. tempcode : ttempcreatenode;
  1177. begin
  1178. { for easy exiting if something goes wrong }
  1179. result := cerrornode.create;
  1180. { check the amount of parameters }
  1181. if not(assigned(left)) or
  1182. not(assigned(tcallparanode(left).right)) then
  1183. begin
  1184. CGMessage1(parser_e_wrong_parameter_size,'Val');
  1185. exit;
  1186. end;
  1187. { in case we are in a generic definition, we cannot
  1188. do all checks, the parameters might be type parameters }
  1189. if df_generic in current_procinfo.procdef.defoptions then
  1190. begin
  1191. result.Free;
  1192. result:=nil;
  1193. resultdef:=voidtype;
  1194. exit;
  1195. end;
  1196. { reverse parameters for easier processing }
  1197. left := reverseparameters(tcallparanode(left));
  1198. { get the parameters }
  1199. tempcode := nil;
  1200. orgcode := nil;
  1201. sizepara := nil;
  1202. sourcepara := tcallparanode(left);
  1203. destpara := tcallparanode(sourcepara.right);
  1204. codepara := tcallparanode(destpara.right);
  1205. { check if codepara is valid }
  1206. if assigned(codepara) and
  1207. (
  1208. not is_integer(codepara.resultdef)
  1209. {$ifndef cpu64bitaddr}
  1210. or is_64bitint(codepara.resultdef)
  1211. {$endif not cpu64bitaddr}
  1212. ) then
  1213. begin
  1214. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  1215. exit;
  1216. end;
  1217. { check if dest para is valid }
  1218. if not is_integer(destpara.resultdef) and
  1219. not is_currency(destpara.resultdef) and
  1220. not(destpara.resultdef.typ in [floatdef,enumdef]) then
  1221. begin
  1222. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1223. exit;
  1224. end;
  1225. { we're going to reuse the exisiting para's, so make sure they }
  1226. { won't be disposed }
  1227. left := nil;
  1228. { create the blocknode which will hold the generated statements + }
  1229. { an initial dummy statement }
  1230. newblock:=internalstatements(newstatement);
  1231. { do we need a temp for code? Yes, if no code specified, or if }
  1232. { code is not a 32bit parameter (we already checked whether the }
  1233. { the code para, if specified, was an orddef) }
  1234. if not assigned(codepara) or
  1235. (codepara.resultdef.size<>sinttype.size) then
  1236. begin
  1237. tempcode := ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1238. addstatement(newstatement,tempcode);
  1239. { set the resultdef of the temp (needed to be able to get }
  1240. { the resultdef of the tempref used in the new code para) }
  1241. typecheckpass(tnode(tempcode));
  1242. { create a temp codepara, but save the original code para to }
  1243. { assign the result to later on }
  1244. if assigned(codepara) then
  1245. begin
  1246. orgcode := codepara.left;
  1247. codepara.left := ctemprefnode.create(tempcode);
  1248. end
  1249. else
  1250. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1251. { we need its resultdef later on }
  1252. codepara.get_paratype;
  1253. end
  1254. else if (torddef(codepara.resultdef).ordtype = torddef(sinttype).ordtype) then
  1255. { because code is a var parameter, it must match types exactly }
  1256. { however, since it will return values in [0..255], both longints }
  1257. { and cardinals are fine. Since the formal code para type is }
  1258. { longint, insert a typecoversion to longint for cardinal para's }
  1259. begin
  1260. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  1261. { make it explicit, oterwise you may get a nonsense range }
  1262. { check error if the cardinal already contained a value }
  1263. { > $7fffffff }
  1264. codepara.get_paratype;
  1265. end;
  1266. { create the procedure name }
  1267. procname := 'fpc_val_';
  1268. case destpara.resultdef.typ of
  1269. orddef:
  1270. begin
  1271. case torddef(destpara.resultdef).ordtype of
  1272. {$ifdef cpu64bitaddr}
  1273. s64bit,
  1274. {$endif cpu64bitaddr}
  1275. s8bit,
  1276. s16bit,
  1277. s32bit:
  1278. begin
  1279. suffix := 'sint_';
  1280. { we also need a destsize para in this case }
  1281. sizepara := ccallparanode.create(cordconstnode.create
  1282. (destpara.resultdef.size,s32inttype,true),nil);
  1283. end;
  1284. {$ifdef cpu64bitaddr}
  1285. u64bit,
  1286. {$endif cpu64bitaddr}
  1287. u8bit,
  1288. u16bit,
  1289. u32bit:
  1290. suffix := 'uint_';
  1291. {$ifndef cpu64bitaddr}
  1292. s64bit: suffix := 'int64_';
  1293. u64bit: suffix := 'qword_';
  1294. {$endif not cpu64bitaddr}
  1295. scurrency: suffix := 'currency_';
  1296. else
  1297. internalerror(200304225);
  1298. end;
  1299. end;
  1300. floatdef:
  1301. suffix:='real_';
  1302. enumdef:
  1303. begin
  1304. suffix:='enum_';
  1305. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1306. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1307. ),nil);
  1308. end;
  1309. end;
  1310. procname := procname + suffix;
  1311. { play a trick to have tcallnode handle invalid source parameters: }
  1312. { the shortstring-longint val routine by default }
  1313. if (sourcepara.resultdef.typ = stringdef) then
  1314. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1315. { zero-based arrays (of char) can be implicitely converted to ansistring }
  1316. else if is_zero_based_array(sourcepara.resultdef) then
  1317. procname := procname + 'ansistr'
  1318. else
  1319. procname := procname + 'shortstr';
  1320. { set up the correct parameters for the call: the code para... }
  1321. newparas := codepara;
  1322. { and the source para }
  1323. codepara.right := sourcepara;
  1324. { sizepara either contains nil if none is needed (which is ok, since }
  1325. { then the next statement severes any possible links with other paras }
  1326. { that sourcepara may have) or it contains the necessary size para and }
  1327. { its right field is nil }
  1328. sourcepara.right := sizepara;
  1329. { create the call and assign the result to dest (val helpers are functions).
  1330. Use a trick to prevent a type size mismatch warning to be generated by the
  1331. assignment node. First convert implicitly to the resultdef. This will insert
  1332. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1333. for the assignment node and therefor preventing the warning (PFV)
  1334. The implicit conversion is avoided for enums because implicit conversion between
  1335. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1336. possible. (DM).
  1337. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1338. to prevent warning about automatic type conversion. }
  1339. if (destpara.resultdef.typ=enumdef) or
  1340. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1341. then
  1342. tc:=ccallnode.createintern(procname,newparas)
  1343. else
  1344. tc:=ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef);
  1345. addstatement(newstatement,cassignmentnode.create(
  1346. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1347. { dispose of the enclosing paranode of the destination }
  1348. destpara.left := nil;
  1349. destpara.right := nil;
  1350. destpara.free;
  1351. { check if we used a temp for code and whether we have to store }
  1352. { it to the real code parameter }
  1353. if assigned(orgcode) then
  1354. addstatement(newstatement,cassignmentnode.create(
  1355. orgcode,
  1356. ctypeconvnode.create_internal(
  1357. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1358. { release the temp if we allocated one }
  1359. if assigned(tempcode) then
  1360. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1361. { free the errornode }
  1362. result.free;
  1363. { and return it }
  1364. result := newblock;
  1365. end;
  1366. function tinlinenode.handle_setlength: tnode;
  1367. var
  1368. def: tdef;
  1369. destppn,
  1370. paras: tnode;
  1371. newstatement: tstatementnode;
  1372. ppn: tcallparanode;
  1373. counter,
  1374. dims: longint;
  1375. isarray: boolean;
  1376. begin
  1377. { for easy exiting if something goes wrong }
  1378. result:=cerrornode.create;
  1379. resultdef:=voidtype;
  1380. paras:=left;
  1381. dims:=0;
  1382. if assigned(paras) then
  1383. begin
  1384. { check type of lengths }
  1385. ppn:=tcallparanode(paras);
  1386. while assigned(ppn.right) do
  1387. begin
  1388. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1389. inserttypeconv(ppn.left,sinttype);
  1390. inc(dims);
  1391. ppn:=tcallparanode(ppn.right);
  1392. end;
  1393. end;
  1394. if dims=0 then
  1395. begin
  1396. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1397. exit;
  1398. end;
  1399. { last param must be var }
  1400. destppn:=ppn.left;
  1401. valid_for_var(destppn,true);
  1402. set_varstate(destppn,vs_written,[]);
  1403. { first param must be a string or dynamic array ...}
  1404. isarray:=is_dynamic_array(destppn.resultdef);
  1405. if not((destppn.resultdef.typ=stringdef) or
  1406. isarray) then
  1407. begin
  1408. { possibly generic involved? }
  1409. if df_generic in current_procinfo.procdef.defoptions then
  1410. result:=internalstatements(newstatement)
  1411. else
  1412. CGMessage(type_e_mismatch);
  1413. exit;
  1414. end;
  1415. { only dynamic arrays accept more dimensions }
  1416. if (dims>1) then
  1417. begin
  1418. if (not isarray) then
  1419. CGMessage(type_e_mismatch)
  1420. else
  1421. begin
  1422. { check if the amount of dimensions is valid }
  1423. def:=tarraydef(destppn.resultdef).elementdef;
  1424. counter:=dims;
  1425. while counter > 1 do
  1426. begin
  1427. if not(is_dynamic_array(def)) then
  1428. begin
  1429. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1430. break;
  1431. end;
  1432. dec(counter);
  1433. def:=tarraydef(def).elementdef;
  1434. end;
  1435. end;
  1436. end;
  1437. result.free;
  1438. result:=nil;
  1439. end;
  1440. function tinlinenode.handle_copy: tnode;
  1441. var
  1442. lowppn,
  1443. highppn,
  1444. npara,
  1445. paras : tnode;
  1446. ppn : tcallparanode;
  1447. paradef : tdef;
  1448. counter : integer;
  1449. begin
  1450. result:=nil;
  1451. { determine copy function to use based on the first argument,
  1452. also count the number of arguments in this loop }
  1453. counter:=1;
  1454. paras:=left;
  1455. ppn:=tcallparanode(paras);
  1456. while assigned(ppn.right) do
  1457. begin
  1458. inc(counter);
  1459. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1460. ppn:=tcallparanode(ppn.right);
  1461. end;
  1462. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1463. paradef:=ppn.left.resultdef;
  1464. if is_ansistring(paradef) then
  1465. // set resultdef to argument def
  1466. resultdef:=paradef
  1467. else if (is_chararray(paradef) and (paradef.size>255)) or
  1468. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1469. // set resultdef to ansistring type since result will be in ansistring codepage
  1470. resultdef:=getansistringdef
  1471. else
  1472. if is_widestring(paradef) then
  1473. resultdef:=cwidestringtype
  1474. else
  1475. if is_unicodestring(paradef) or
  1476. is_widechararray(paradef) or
  1477. is_pwidechar(paradef) then
  1478. resultdef:=cunicodestringtype
  1479. else
  1480. if is_char(paradef) then
  1481. resultdef:=cshortstringtype
  1482. else
  1483. if is_dynamic_array(paradef) then
  1484. begin
  1485. { Only allow 1 or 3 arguments }
  1486. if (counter<>1) and (counter<>3) then
  1487. begin
  1488. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  1489. exit;
  1490. end;
  1491. resultdef:=paradef;
  1492. end
  1493. else
  1494. begin
  1495. { generic fallback that will give an error if a wrong
  1496. type is passed }
  1497. if (counter=3) then
  1498. resultdef:=cshortstringtype
  1499. else
  1500. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  1501. end;
  1502. end;
  1503. {$maxfpuregisters 0}
  1504. function getpi : bestreal;
  1505. begin
  1506. {$ifdef x86}
  1507. { x86 has pi in hardware }
  1508. result:=pi;
  1509. {$else x86}
  1510. {$ifdef cpuextended}
  1511. result:=MathPiExtended.Value;
  1512. {$else cpuextended}
  1513. result:=MathPi.Value;
  1514. {$endif cpuextended}
  1515. {$endif x86}
  1516. end;
  1517. function tinlinenode.simplify(forinline : boolean): tnode;
  1518. function do_lowhigh(def:tdef) : tnode;
  1519. var
  1520. v : tconstexprint;
  1521. enum : tenumsym;
  1522. hp : tnode;
  1523. i : integer;
  1524. begin
  1525. case def.typ of
  1526. orddef:
  1527. begin
  1528. set_varstate(left,vs_read,[]);
  1529. if inlinenumber=in_low_x then
  1530. v:=torddef(def).low
  1531. else
  1532. v:=torddef(def).high;
  1533. hp:=cordconstnode.create(v,def,true);
  1534. typecheckpass(hp);
  1535. do_lowhigh:=hp;
  1536. end;
  1537. enumdef:
  1538. begin
  1539. set_varstate(left,vs_read,[]);
  1540. if inlinenumber=in_high_x then
  1541. v:=tenumdef(def).maxval
  1542. else
  1543. v:=tenumdef(def).minval;
  1544. enum:=nil;
  1545. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1546. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1547. begin
  1548. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1549. break;
  1550. end;
  1551. if not assigned(enum) then
  1552. internalerror(309993)
  1553. else
  1554. hp:=genenumnode(enum);
  1555. do_lowhigh:=hp;
  1556. end;
  1557. else
  1558. internalerror(87);
  1559. end;
  1560. end;
  1561. function getconstrealvalue : bestreal;
  1562. begin
  1563. case left.nodetype of
  1564. ordconstn:
  1565. getconstrealvalue:=tordconstnode(left).value;
  1566. realconstn:
  1567. getconstrealvalue:=trealconstnode(left).value_real;
  1568. else
  1569. internalerror(309992);
  1570. end;
  1571. end;
  1572. procedure setconstrealvalue(r : bestreal);
  1573. begin
  1574. result:=crealconstnode.create(r,pbestrealtype^);
  1575. end;
  1576. function handle_ln_const(r : bestreal) : tnode;
  1577. begin
  1578. if r<=0.0 then
  1579. if floating_point_range_check_error then
  1580. begin
  1581. result:=crealconstnode.create(0,pbestrealtype^);
  1582. CGMessage(type_e_wrong_math_argument)
  1583. end
  1584. else
  1585. begin
  1586. if r=0.0 then
  1587. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1588. else
  1589. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1590. end
  1591. else
  1592. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1593. end;
  1594. function handle_sqrt_const(r : bestreal) : tnode;
  1595. begin
  1596. if r<0.0 then
  1597. if floating_point_range_check_error then
  1598. begin
  1599. result:=crealconstnode.create(0,pbestrealtype^);
  1600. CGMessage(type_e_wrong_math_argument)
  1601. end
  1602. else
  1603. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1604. else
  1605. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1606. end;
  1607. function handle_const_sar : tnode;
  1608. var
  1609. vl,vl2 : TConstExprInt;
  1610. bits,shift: integer;
  1611. mask : qword;
  1612. def : tdef;
  1613. begin
  1614. result:=nil;
  1615. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1616. begin
  1617. if (left.nodetype=callparan) and
  1618. assigned(tcallparanode(left).right) then
  1619. begin
  1620. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1621. begin
  1622. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1623. vl:=tordconstnode(tcallparanode(left).left).value;
  1624. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1625. end
  1626. else
  1627. exit;
  1628. end
  1629. else
  1630. begin
  1631. def:=left.resultdef;
  1632. vl:=1;
  1633. vl2:=tordconstnode(left).value;
  1634. end;
  1635. bits:=def.size*8;
  1636. shift:=vl.svalue and (bits-1);
  1637. case bits of
  1638. 8:
  1639. mask:=$ff;
  1640. 16:
  1641. mask:=$ffff;
  1642. 32:
  1643. mask:=$ffffffff;
  1644. 64:
  1645. mask:=qword($ffffffffffffffff);
  1646. else
  1647. mask:=qword(1 shl bits)-1;
  1648. end;
  1649. {$push}
  1650. {$r-,q-}
  1651. if shift=0 then
  1652. result:=cordconstnode.create(vl2.svalue,def,false)
  1653. else if vl2.svalue<0 then
  1654. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1655. else
  1656. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1657. {$pop}
  1658. end
  1659. else
  1660. end;
  1661. function handle_const_rox : tnode;
  1662. var
  1663. vl,vl2 : TConstExprInt;
  1664. bits,shift: integer;
  1665. def : tdef;
  1666. begin
  1667. result:=nil;
  1668. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1669. begin
  1670. if (left.nodetype=callparan) and
  1671. assigned(tcallparanode(left).right) then
  1672. begin
  1673. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1674. begin
  1675. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1676. vl:=tordconstnode(tcallparanode(left).left).value;
  1677. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1678. end
  1679. else
  1680. exit;
  1681. end
  1682. else
  1683. begin
  1684. def:=left.resultdef;
  1685. vl:=1;
  1686. vl2:=tordconstnode(left).value;
  1687. end;
  1688. bits:=def.size*8;
  1689. shift:=vl.svalue and (bits-1);
  1690. {$push}
  1691. {$r-,q-}
  1692. if shift=0 then
  1693. result:=cordconstnode.create(vl2.svalue,def,false)
  1694. else
  1695. case inlinenumber of
  1696. in_ror_x,in_ror_x_y:
  1697. case def.size of
  1698. 1:
  1699. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  1700. 2:
  1701. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  1702. 4:
  1703. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  1704. 8:
  1705. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  1706. else
  1707. internalerror(2011061903);
  1708. end;
  1709. in_rol_x,in_rol_x_y:
  1710. case def.size of
  1711. 1:
  1712. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  1713. 2:
  1714. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  1715. 4:
  1716. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  1717. 8:
  1718. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  1719. else
  1720. internalerror(2011061902);
  1721. end;
  1722. else
  1723. internalerror(2011061901);
  1724. end;
  1725. end;
  1726. end;
  1727. var
  1728. hp : tnode;
  1729. vl,vl2 : TConstExprInt;
  1730. vr : bestreal;
  1731. begin { simplify }
  1732. result:=nil;
  1733. { handle intern constant functions in separate case }
  1734. if nf_inlineconst in flags then
  1735. begin
  1736. { no parameters? }
  1737. if not assigned(left) then
  1738. internalerror(200501231)
  1739. else
  1740. begin
  1741. vl:=0;
  1742. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1743. case left.nodetype of
  1744. realconstn :
  1745. begin
  1746. { Real functions are all handled with internproc below }
  1747. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1748. end;
  1749. ordconstn :
  1750. vl:=tordconstnode(left).value;
  1751. callparan :
  1752. begin
  1753. { both exists, else it was not generated }
  1754. vl:=tordconstnode(tcallparanode(left).left).value;
  1755. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1756. end;
  1757. else
  1758. CGMessage(parser_e_illegal_expression);
  1759. end;
  1760. case inlinenumber of
  1761. in_const_abs :
  1762. if vl.signed then
  1763. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline)
  1764. else
  1765. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline);
  1766. in_const_sqr:
  1767. if vl.signed then
  1768. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline)
  1769. else
  1770. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline);
  1771. in_const_odd :
  1772. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool8type,true);
  1773. in_const_swap_word :
  1774. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1775. in_const_swap_long :
  1776. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1777. in_const_swap_qword :
  1778. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1779. in_const_ptr:
  1780. begin
  1781. {Don't construct pointers from negative values.}
  1782. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1783. cgmessage(parser_e_range_check_error);
  1784. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1785. end
  1786. else
  1787. internalerror(88);
  1788. end;
  1789. end;
  1790. if hp=nil then
  1791. hp:=cerrornode.create;
  1792. result:=hp;
  1793. end
  1794. else
  1795. begin
  1796. case inlinenumber of
  1797. in_lo_long,
  1798. in_hi_long,
  1799. in_lo_qword,
  1800. in_hi_qword,
  1801. in_lo_word,
  1802. in_hi_word :
  1803. begin
  1804. if left.nodetype=ordconstn then
  1805. begin
  1806. case inlinenumber of
  1807. in_lo_word :
  1808. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1809. in_hi_word :
  1810. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1811. in_lo_long :
  1812. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1813. in_hi_long :
  1814. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1815. in_lo_qword :
  1816. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1817. in_hi_qword :
  1818. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1819. end;
  1820. end;
  1821. end;
  1822. in_ord_x:
  1823. begin
  1824. case left.resultdef.typ of
  1825. orddef :
  1826. begin
  1827. case torddef(left.resultdef).ordtype of
  1828. pasbool8,
  1829. uchar:
  1830. begin
  1831. { change to byte() }
  1832. result:=ctypeconvnode.create_internal(left,u8inttype);
  1833. left:=nil;
  1834. end;
  1835. pasbool16,
  1836. uwidechar :
  1837. begin
  1838. { change to word() }
  1839. result:=ctypeconvnode.create_internal(left,u16inttype);
  1840. left:=nil;
  1841. end;
  1842. pasbool32 :
  1843. begin
  1844. { change to dword() }
  1845. result:=ctypeconvnode.create_internal(left,u32inttype);
  1846. left:=nil;
  1847. end;
  1848. pasbool64 :
  1849. begin
  1850. { change to qword() }
  1851. result:=ctypeconvnode.create_internal(left,u64inttype);
  1852. left:=nil;
  1853. end;
  1854. bool8bit:
  1855. begin
  1856. { change to shortint() }
  1857. result:=ctypeconvnode.create_internal(left,s8inttype);
  1858. left:=nil;
  1859. end;
  1860. bool16bit :
  1861. begin
  1862. { change to smallint() }
  1863. result:=ctypeconvnode.create_internal(left,s16inttype);
  1864. left:=nil;
  1865. end;
  1866. bool32bit :
  1867. begin
  1868. { change to longint() }
  1869. result:=ctypeconvnode.create_internal(left,s32inttype);
  1870. left:=nil;
  1871. end;
  1872. bool64bit :
  1873. begin
  1874. { change to int64() }
  1875. result:=ctypeconvnode.create_internal(left,s64inttype);
  1876. left:=nil;
  1877. end;
  1878. uvoid :
  1879. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1880. else
  1881. begin
  1882. { all other orddef need no transformation }
  1883. result:=left;
  1884. left:=nil;
  1885. end;
  1886. end;
  1887. end;
  1888. enumdef :
  1889. begin
  1890. result:=ctypeconvnode.create_internal(left,s32inttype);
  1891. left:=nil;
  1892. end;
  1893. pointerdef :
  1894. begin
  1895. if m_mac in current_settings.modeswitches then
  1896. begin
  1897. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1898. left:=nil;
  1899. end
  1900. end;
  1901. end;
  1902. (*
  1903. if (left.nodetype=ordconstn) then
  1904. begin
  1905. result:=cordconstnode.create(
  1906. tordconstnode(left).value,sinttype,true);
  1907. end
  1908. else if (m_mac in current_settings.modeswitches) and
  1909. (left.ndoetype=pointerconstn) then
  1910. result:=cordconstnode.create(
  1911. tpointerconstnode(left).value,ptruinttype,true);
  1912. *)
  1913. end;
  1914. in_chr_byte:
  1915. begin
  1916. { convert to explicit char() }
  1917. result:=ctypeconvnode.create_internal(left,cansichartype);
  1918. left:=nil;
  1919. end;
  1920. in_length_x:
  1921. begin
  1922. case left.resultdef.typ of
  1923. stringdef :
  1924. begin
  1925. if (left.nodetype=stringconstn) then
  1926. begin
  1927. result:=cordconstnode.create(
  1928. tstringconstnode(left).len,sinttype,true);
  1929. end;
  1930. end;
  1931. orddef :
  1932. begin
  1933. { length of char is always one }
  1934. if is_char(left.resultdef) or
  1935. is_widechar(left.resultdef) then
  1936. begin
  1937. result:=cordconstnode.create(1,sinttype,false);
  1938. end
  1939. end;
  1940. arraydef :
  1941. begin
  1942. if (left.nodetype=stringconstn) then
  1943. begin
  1944. result:=cordconstnode.create(
  1945. tstringconstnode(left).len,sinttype,true);
  1946. end
  1947. else if not is_open_array(left.resultdef) and
  1948. not is_array_of_const(left.resultdef) and
  1949. not is_dynamic_array(left.resultdef) then
  1950. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1951. tarraydef(left.resultdef).lowrange+1,
  1952. sinttype,true);
  1953. end;
  1954. end;
  1955. end;
  1956. in_assigned_x:
  1957. begin
  1958. if is_constnode(tcallparanode(left).left) or
  1959. (tcallparanode(left).left.nodetype = pointerconstn) then
  1960. begin
  1961. { let an add node figure it out }
  1962. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1963. tcallparanode(left).left := nil;
  1964. end;
  1965. end;
  1966. in_pred_x,
  1967. in_succ_x:
  1968. begin
  1969. if (left.nodetype=ordconstn) then
  1970. begin
  1971. if (inlinenumber=in_succ_x) then
  1972. vl:=tordconstnode(left).value+1
  1973. else
  1974. vl:=tordconstnode(left).value-1;
  1975. if is_integer(left.resultdef) then
  1976. { the type of the original integer constant is irrelevant,
  1977. it should be automatically adapted to the new value
  1978. (except when inlining) }
  1979. result:=create_simplified_ord_const(vl,resultdef,forinline)
  1980. else
  1981. { check the range for enums, chars, booleans }
  1982. result:=cordconstnode.create(vl,left.resultdef,true)
  1983. end
  1984. end;
  1985. in_low_x,
  1986. in_high_x:
  1987. begin
  1988. case left.resultdef.typ of
  1989. orddef,
  1990. enumdef:
  1991. begin
  1992. result:=do_lowhigh(left.resultdef);
  1993. end;
  1994. setdef:
  1995. begin
  1996. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1997. end;
  1998. arraydef:
  1999. begin
  2000. if (inlinenumber=in_low_x) then
  2001. begin
  2002. result:=cordconstnode.create(int64(tarraydef(
  2003. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2004. end
  2005. else if not is_open_array(left.resultdef) and
  2006. not is_array_of_const(left.resultdef) and
  2007. not is_dynamic_array(left.resultdef) then
  2008. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2009. tarraydef(left.resultdef).rangedef,true);
  2010. end;
  2011. stringdef:
  2012. begin
  2013. if inlinenumber=in_low_x then
  2014. begin
  2015. result:=cordconstnode.create(0,u8inttype,false);
  2016. end
  2017. else if not is_ansistring(left.resultdef) and
  2018. not is_wide_or_unicode_string(left.resultdef) then
  2019. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2020. end;
  2021. end;
  2022. end;
  2023. in_exp_real :
  2024. begin
  2025. if left.nodetype in [ordconstn,realconstn] then
  2026. begin
  2027. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2028. if (trealconstnode(result).value_real=MathInf.Value) and
  2029. floating_point_range_check_error then
  2030. begin
  2031. result:=crealconstnode.create(0,pbestrealtype^);
  2032. CGMessage(parser_e_range_check_error);
  2033. end;
  2034. end
  2035. end;
  2036. in_trunc_real :
  2037. begin
  2038. if left.nodetype in [ordconstn,realconstn] then
  2039. begin
  2040. vr:=getconstrealvalue;
  2041. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2042. begin
  2043. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2044. result:=cordconstnode.create(1,s64inttype,false)
  2045. end
  2046. else
  2047. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2048. end
  2049. end;
  2050. in_round_real :
  2051. begin
  2052. { can't evaluate while inlining, may depend on fpu setting }
  2053. if (not forinline) and
  2054. (left.nodetype in [ordconstn,realconstn]) then
  2055. begin
  2056. vr:=getconstrealvalue;
  2057. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2058. begin
  2059. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2060. result:=cordconstnode.create(1,s64inttype,false)
  2061. end
  2062. else
  2063. result:=cordconstnode.create(round(vr),s64inttype,true)
  2064. end
  2065. end;
  2066. in_frac_real :
  2067. begin
  2068. if left.nodetype in [ordconstn,realconstn] then
  2069. setconstrealvalue(frac(getconstrealvalue))
  2070. end;
  2071. in_int_real :
  2072. begin
  2073. if left.nodetype in [ordconstn,realconstn] then
  2074. setconstrealvalue(int(getconstrealvalue));
  2075. end;
  2076. in_pi_real :
  2077. begin
  2078. if block_type=bt_const then
  2079. setconstrealvalue(getpi)
  2080. end;
  2081. in_cos_real :
  2082. begin
  2083. if left.nodetype in [ordconstn,realconstn] then
  2084. setconstrealvalue(cos(getconstrealvalue))
  2085. end;
  2086. in_sin_real :
  2087. begin
  2088. if left.nodetype in [ordconstn,realconstn] then
  2089. setconstrealvalue(sin(getconstrealvalue))
  2090. end;
  2091. in_arctan_real :
  2092. begin
  2093. if left.nodetype in [ordconstn,realconstn] then
  2094. setconstrealvalue(arctan(getconstrealvalue))
  2095. end;
  2096. in_abs_real :
  2097. begin
  2098. if left.nodetype in [ordconstn,realconstn] then
  2099. setconstrealvalue(abs(getconstrealvalue))
  2100. end;
  2101. in_abs_long:
  2102. begin
  2103. if left.nodetype=ordconstn then
  2104. begin
  2105. if tordconstnode(left).value<0 then
  2106. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  2107. else
  2108. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  2109. end
  2110. end;
  2111. in_sqr_real :
  2112. begin
  2113. if left.nodetype in [ordconstn,realconstn] then
  2114. setconstrealvalue(sqr(getconstrealvalue))
  2115. end;
  2116. in_sqrt_real :
  2117. begin
  2118. if left.nodetype in [ordconstn,realconstn] then
  2119. begin
  2120. vr:=getconstrealvalue;
  2121. if vr<0.0 then
  2122. result:=handle_sqrt_const(vr)
  2123. else
  2124. setconstrealvalue(sqrt(vr));
  2125. end
  2126. end;
  2127. in_ln_real :
  2128. begin
  2129. if left.nodetype in [ordconstn,realconstn] then
  2130. begin
  2131. vr:=getconstrealvalue;
  2132. if vr<=0.0 then
  2133. result:=handle_ln_const(vr)
  2134. else
  2135. setconstrealvalue(ln(vr));
  2136. end
  2137. end;
  2138. in_assert_x_y :
  2139. begin
  2140. if not(cs_do_assertion in current_settings.localswitches) then
  2141. { we need a valid node, so insert a nothingn }
  2142. result:=cnothingnode.create;
  2143. end;
  2144. in_sar_x,
  2145. in_sar_x_y :
  2146. begin
  2147. result:=handle_const_sar;
  2148. end;
  2149. in_rol_x,
  2150. in_rol_x_y,
  2151. in_ror_x,
  2152. in_ror_x_y :
  2153. result:=handle_const_rox;
  2154. end;
  2155. end;
  2156. end;
  2157. function tinlinenode.pass_typecheck:tnode;
  2158. procedure setfloatresultdef;
  2159. begin
  2160. if (left.resultdef.typ=floatdef) and
  2161. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2162. resultdef:=left.resultdef
  2163. else
  2164. begin
  2165. if (left.nodetype <> ordconstn) then
  2166. inserttypeconv(left,pbestrealtype^);
  2167. resultdef:=pbestrealtype^;
  2168. end;
  2169. end;
  2170. procedure handle_pack_unpack;
  2171. var
  2172. source, target, index: tcallparanode;
  2173. unpackedarraydef, packedarraydef: tarraydef;
  2174. tempindex: TConstExprInt;
  2175. begin
  2176. resultdef:=voidtype;
  2177. unpackedarraydef := nil;
  2178. packedarraydef := nil;
  2179. source := tcallparanode(left);
  2180. if (inlinenumber = in_unpack_x_y_z) then
  2181. begin
  2182. target := tcallparanode(source.right);
  2183. index := tcallparanode(target.right);
  2184. { source must be a packed array }
  2185. if not is_packed_array(source.left.resultdef) then
  2186. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  2187. else
  2188. packedarraydef := tarraydef(source.left.resultdef);
  2189. { target can be any kind of array, as long as it's not packed }
  2190. if (target.left.resultdef.typ <> arraydef) or
  2191. is_packed_array(target.left.resultdef) then
  2192. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  2193. else
  2194. unpackedarraydef := tarraydef(target.left.resultdef);
  2195. end
  2196. else
  2197. begin
  2198. index := tcallparanode(source.right);
  2199. target := tcallparanode(index.right);
  2200. { source can be any kind of array, as long as it's not packed }
  2201. if (source.left.resultdef.typ <> arraydef) or
  2202. is_packed_array(source.left.resultdef) then
  2203. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  2204. else
  2205. unpackedarraydef := tarraydef(source.left.resultdef);
  2206. { target must be a packed array }
  2207. if not is_packed_array(target.left.resultdef) then
  2208. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  2209. else
  2210. packedarraydef := tarraydef(target.left.resultdef);
  2211. end;
  2212. if assigned(unpackedarraydef) then
  2213. begin
  2214. { index must be compatible with the unpacked array's indextype }
  2215. inserttypeconv(index.left,unpackedarraydef.rangedef);
  2216. { range check at compile time if possible }
  2217. if assigned(packedarraydef) and
  2218. (index.left.nodetype = ordconstn) and
  2219. not is_special_array(unpackedarraydef) then
  2220. begin
  2221. testrange(unpackedarraydef,tordconstnode(index.left).value,false,false);
  2222. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  2223. testrange(unpackedarraydef,tempindex,false,false);
  2224. end;
  2225. end;
  2226. { source array is read and must be valid }
  2227. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  2228. { target array is written }
  2229. valid_for_assignment(target.left,true);
  2230. set_varstate(target.left,vs_written,[]);
  2231. { index in the unpacked array is read and must be valid }
  2232. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  2233. { if the size of the arrays is 0 (array of empty records), }
  2234. { do nothing }
  2235. if (source.resultdef.size = 0) then
  2236. result:=cnothingnode.create;
  2237. end;
  2238. function handle_objc_encode: tnode;
  2239. var
  2240. encodedtype: ansistring;
  2241. errordef: tdef;
  2242. begin
  2243. encodedtype:='';
  2244. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  2245. Message1(type_e_objc_type_unsupported,errordef.typename);
  2246. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype));
  2247. end;
  2248. var
  2249. hightree,
  2250. hp : tnode;
  2251. begin
  2252. result:=nil;
  2253. { when handling writeln "left" contains no valid address }
  2254. if assigned(left) then
  2255. begin
  2256. if left.nodetype=callparan then
  2257. tcallparanode(left).get_paratype
  2258. else
  2259. typecheckpass(left);
  2260. end;
  2261. if not(nf_inlineconst in flags) then
  2262. begin
  2263. case inlinenumber of
  2264. in_lo_long,
  2265. in_hi_long,
  2266. in_lo_qword,
  2267. in_hi_qword,
  2268. in_lo_word,
  2269. in_hi_word :
  2270. begin
  2271. { give warning for incompatibility with tp and delphi }
  2272. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  2273. ((m_tp7 in current_settings.modeswitches) or
  2274. (m_delphi in current_settings.modeswitches)) then
  2275. CGMessage(type_w_maybe_wrong_hi_lo);
  2276. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2277. if not is_integer(left.resultdef) then
  2278. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2279. case inlinenumber of
  2280. in_lo_word,
  2281. in_hi_word :
  2282. resultdef:=u8inttype;
  2283. in_lo_long,
  2284. in_hi_long :
  2285. resultdef:=u16inttype;
  2286. in_lo_qword,
  2287. in_hi_qword :
  2288. resultdef:=u32inttype;
  2289. end;
  2290. end;
  2291. in_sizeof_x:
  2292. begin
  2293. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  2294. set_varstate(left,vs_read,[]);
  2295. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  2296. begin
  2297. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2298. if assigned(hightree) then
  2299. begin
  2300. hp:=caddnode.create(addn,hightree,
  2301. cordconstnode.create(1,sinttype,false));
  2302. if (left.resultdef.typ=arraydef) then
  2303. if not is_packed_array(tarraydef(left.resultdef)) then
  2304. begin
  2305. if (tarraydef(left.resultdef).elesize<>1) then
  2306. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2307. left.resultdef).elesize,sinttype,true));
  2308. end
  2309. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2310. begin
  2311. { no packed open array support yet }
  2312. if (hp.nodetype <> ordconstn) then
  2313. internalerror(2006081511);
  2314. hp.free;
  2315. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2316. {
  2317. hp:=
  2318. ctypeconvnode.create_explicit(sinttype,
  2319. cmoddivnode.create(divn,
  2320. caddnode.create(addn,
  2321. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2322. left.resultdef).elepackedbitsize,s64inttype,true)),
  2323. cordconstnode.create(a,s64inttype,true)),
  2324. cordconstnode.create(8,s64inttype,true)),
  2325. sinttype);
  2326. }
  2327. end;
  2328. result:=hp;
  2329. end;
  2330. end
  2331. else
  2332. resultdef:=sinttype;
  2333. end;
  2334. in_typeof_x:
  2335. begin
  2336. if target_info.system in systems_managed_vm then
  2337. message(parser_e_feature_unsupported_for_vm);
  2338. set_varstate(left,vs_read,[]);
  2339. resultdef:=voidpointertype;
  2340. end;
  2341. in_ord_x:
  2342. begin
  2343. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2344. case left.resultdef.typ of
  2345. orddef,
  2346. enumdef :
  2347. ;
  2348. pointerdef :
  2349. begin
  2350. if not(m_mac in current_settings.modeswitches) then
  2351. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2352. end
  2353. else
  2354. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2355. end;
  2356. end;
  2357. in_chr_byte:
  2358. begin
  2359. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2360. end;
  2361. in_length_x:
  2362. begin
  2363. if ((left.resultdef.typ=arraydef) and
  2364. (not is_special_array(left.resultdef) or
  2365. is_open_array(left.resultdef))) or
  2366. (left.resultdef.typ=orddef) then
  2367. set_varstate(left,vs_read,[])
  2368. else
  2369. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2370. case left.resultdef.typ of
  2371. variantdef:
  2372. begin
  2373. inserttypeconv(left,getansistringdef);
  2374. end;
  2375. stringdef :
  2376. begin
  2377. { we don't need string convertions here, }
  2378. { except if from widestring to ansistring }
  2379. { and vice versa (that can change the }
  2380. { length) }
  2381. if (left.nodetype=typeconvn) and
  2382. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2383. not(is_wide_or_unicode_string(left.resultdef) xor
  2384. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2385. begin
  2386. hp:=ttypeconvnode(left).left;
  2387. ttypeconvnode(left).left:=nil;
  2388. left.free;
  2389. left:=hp;
  2390. end;
  2391. end;
  2392. orddef :
  2393. begin
  2394. { will be handled in simplify }
  2395. if not is_char(left.resultdef) and
  2396. not is_widechar(left.resultdef) then
  2397. CGMessage(type_e_mismatch);
  2398. end;
  2399. pointerdef :
  2400. begin
  2401. if is_pchar(left.resultdef) then
  2402. begin
  2403. hp := ccallparanode.create(left,nil);
  2404. result := ccallnode.createintern('fpc_pchar_length',hp);
  2405. { make sure the left node doesn't get disposed, since it's }
  2406. { reused in the new node (JM) }
  2407. left:=nil;
  2408. exit;
  2409. end
  2410. else if is_pwidechar(left.resultdef) then
  2411. begin
  2412. hp := ccallparanode.create(left,nil);
  2413. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2414. { make sure the left node doesn't get disposed, since it's }
  2415. { reused in the new node (JM) }
  2416. left:=nil;
  2417. exit;
  2418. end
  2419. else
  2420. CGMessage(type_e_mismatch);
  2421. end;
  2422. arraydef :
  2423. begin
  2424. if is_open_array(left.resultdef) or
  2425. is_array_of_const(left.resultdef) then
  2426. begin
  2427. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2428. if assigned(hightree) then
  2429. result:=caddnode.create(addn,hightree,
  2430. cordconstnode.create(1,sinttype,false));
  2431. exit;
  2432. end
  2433. else if is_dynamic_array(left.resultdef) then
  2434. begin
  2435. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2436. result := ccallnode.createintern('fpc_dynarray_length',hp);
  2437. { make sure the left node doesn't get disposed, since it's }
  2438. { reused in the new node (JM) }
  2439. left:=nil;
  2440. exit;
  2441. end
  2442. else
  2443. begin
  2444. { will be handled in simplify }
  2445. end;
  2446. end
  2447. else
  2448. CGMessage(type_e_mismatch);
  2449. end;
  2450. { shortstring return an 8 bit value as the length
  2451. is the first byte of the string }
  2452. if is_shortstring(left.resultdef) then
  2453. resultdef:=u8inttype
  2454. else
  2455. resultdef:=sinttype;
  2456. end;
  2457. in_typeinfo_x:
  2458. begin
  2459. if target_info.system in systems_managed_vm then
  2460. message(parser_e_feature_unsupported_for_vm);
  2461. if (left.resultdef.typ=enumdef) and
  2462. (tenumdef(left.resultdef).has_jumps) then
  2463. CGMessage(type_e_no_type_info);
  2464. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2465. resultdef:=voidpointertype;
  2466. end;
  2467. in_assigned_x:
  2468. begin
  2469. { the parser has already made sure the expression is valid }
  2470. { in case of a complex procvar, only check the "code" pointer }
  2471. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2472. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2473. begin
  2474. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2475. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2476. tcallparanode(left).get_paratype;
  2477. end;
  2478. { converting to an add node is tricky because of differences
  2479. in procvar handling between FPC and Delphi handling, so
  2480. handle specially }
  2481. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2482. resultdef:=pasbool8type;
  2483. end;
  2484. in_ofs_x :
  2485. internalerror(2000101001);
  2486. in_seg_x :
  2487. begin
  2488. if target_info.system in systems_managed_vm then
  2489. message(parser_e_feature_unsupported_for_vm);
  2490. set_varstate(left,vs_read,[]);
  2491. result:=cordconstnode.create(0,s32inttype,false);
  2492. end;
  2493. in_pred_x,
  2494. in_succ_x:
  2495. begin
  2496. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2497. resultdef:=left.resultdef;
  2498. if not is_ordinal(resultdef) then
  2499. CGMessage(type_e_ordinal_expr_expected)
  2500. else
  2501. begin
  2502. if (resultdef.typ=enumdef) and
  2503. (tenumdef(resultdef).has_jumps) and
  2504. not(m_delphi in current_settings.modeswitches) then
  2505. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2506. end;
  2507. end;
  2508. in_copy_x:
  2509. result:=handle_copy;
  2510. in_initialize_x,
  2511. in_finalize_x:
  2512. begin
  2513. { inlined from pinline }
  2514. internalerror(200204231);
  2515. end;
  2516. in_setlength_x:
  2517. begin
  2518. result:=handle_setlength;
  2519. end;
  2520. in_inc_x,
  2521. in_dec_x:
  2522. begin
  2523. resultdef:=voidtype;
  2524. if assigned(left) then
  2525. begin
  2526. { first param must be var }
  2527. valid_for_var(tcallparanode(left).left,true);
  2528. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2529. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2530. is_ordinal(left.resultdef) or
  2531. is_currency(left.resultdef) then
  2532. begin
  2533. { value of left gets changed -> must be unique }
  2534. set_unique(tcallparanode(left).left);
  2535. { two paras ? }
  2536. if assigned(tcallparanode(left).right) then
  2537. begin
  2538. if is_integer(tcallparanode(left).right.resultdef) then
  2539. begin
  2540. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2541. { when range/overflow checking is on, we
  2542. convert this to a regular add, and for proper
  2543. checking we need the original type }
  2544. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2545. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2546. begin
  2547. { don't convert values added to pointers into the pointer types themselves,
  2548. because that will turn signed values into unsigned ones, which then
  2549. goes wrong when they have to be multiplied with the size of the elements
  2550. to which the pointer points in ncginl (mantis #17342) }
  2551. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2552. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2553. else
  2554. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2555. end
  2556. else if is_integer(tcallparanode(left).left.resultdef) then
  2557. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2558. else
  2559. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2560. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2561. { should be handled in the parser (JM) }
  2562. internalerror(2006020901);
  2563. end
  2564. else
  2565. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2566. end;
  2567. end
  2568. else
  2569. begin
  2570. hp:=self;
  2571. if isunaryoverloaded(hp) then
  2572. begin
  2573. { inc(rec) and dec(rec) assigns result value to argument }
  2574. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2575. exit;
  2576. end
  2577. else
  2578. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2579. end;
  2580. end
  2581. else
  2582. CGMessagePos(fileinfo,type_e_mismatch);
  2583. end;
  2584. in_read_x,
  2585. in_readln_x,
  2586. in_readstr_x,
  2587. in_write_x,
  2588. in_writeln_x,
  2589. in_writestr_x :
  2590. begin
  2591. result := handle_read_write;
  2592. end;
  2593. in_settextbuf_file_x :
  2594. begin
  2595. if target_info.system in systems_managed_vm then
  2596. message(parser_e_feature_unsupported_for_vm);
  2597. resultdef:=voidtype;
  2598. { now we know the type of buffer }
  2599. hp:=ccallparanode.create(cordconstnode.create(
  2600. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2601. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2602. left:=nil;
  2603. end;
  2604. { the firstpass of the arg has been done in firstcalln ? }
  2605. in_reset_typedfile,
  2606. in_rewrite_typedfile :
  2607. begin
  2608. result := handle_reset_rewrite_typed;
  2609. end;
  2610. in_str_x_string :
  2611. begin
  2612. result:=handle_str;
  2613. end;
  2614. in_val_x :
  2615. begin
  2616. result:=handle_val;
  2617. end;
  2618. in_include_x_y,
  2619. in_exclude_x_y:
  2620. begin
  2621. resultdef:=voidtype;
  2622. { the parser already checks whether we have two (and exactly two) }
  2623. { parameters (JM) }
  2624. { first param must be var }
  2625. valid_for_var(tcallparanode(left).left,true);
  2626. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2627. { check type }
  2628. if (left.resultdef.typ=setdef) then
  2629. begin
  2630. { insert a type conversion }
  2631. { to the type of the set elements }
  2632. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2633. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2634. tsetdef(left.resultdef).elementdef);
  2635. end
  2636. else
  2637. CGMessage(type_e_mismatch);
  2638. end;
  2639. in_pack_x_y_z,
  2640. in_unpack_x_y_z :
  2641. begin
  2642. handle_pack_unpack;
  2643. end;
  2644. in_slice_x:
  2645. begin
  2646. if target_info.system in systems_managed_vm then
  2647. message(parser_e_feature_unsupported_for_vm);
  2648. result:=nil;
  2649. resultdef:=tcallparanode(left).left.resultdef;
  2650. if (resultdef.typ <> arraydef) then
  2651. CGMessagePos(left.fileinfo,type_e_mismatch)
  2652. else if is_packed_array(resultdef) then
  2653. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2654. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2655. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2656. type_e_integer_expr_expected,
  2657. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2658. end;
  2659. in_low_x,
  2660. in_high_x:
  2661. begin
  2662. case left.resultdef.typ of
  2663. orddef,
  2664. enumdef,
  2665. setdef:
  2666. ;
  2667. arraydef:
  2668. begin
  2669. if (inlinenumber=in_low_x) then
  2670. set_varstate(left,vs_read,[])
  2671. else
  2672. begin
  2673. if is_open_array(left.resultdef) or
  2674. is_array_of_const(left.resultdef) then
  2675. begin
  2676. set_varstate(left,vs_read,[]);
  2677. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2678. end
  2679. else
  2680. if is_dynamic_array(left.resultdef) then
  2681. begin
  2682. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2683. { can't use inserttypeconv because we need }
  2684. { an explicit type conversion (JM) }
  2685. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2686. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2687. { make sure the left node doesn't get disposed, since it's }
  2688. { reused in the new node (JM) }
  2689. left:=nil;
  2690. end
  2691. else
  2692. begin
  2693. set_varstate(left,vs_read,[]);
  2694. end;
  2695. end;
  2696. end;
  2697. stringdef:
  2698. begin
  2699. if inlinenumber=in_low_x then
  2700. begin
  2701. set_varstate(left,vs_read,[]);
  2702. end
  2703. else
  2704. begin
  2705. if is_open_string(left.resultdef) then
  2706. begin
  2707. set_varstate(left,vs_read,[]);
  2708. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2709. end
  2710. else if is_ansistring(left.resultdef) or
  2711. is_wide_or_unicode_string(left.resultdef) then
  2712. CGMessage(type_e_mismatch)
  2713. end;
  2714. end;
  2715. else
  2716. CGMessage(type_e_mismatch);
  2717. end;
  2718. end;
  2719. in_exp_real,
  2720. in_frac_real,
  2721. in_int_real,
  2722. in_cos_real,
  2723. in_sin_real,
  2724. in_arctan_real,
  2725. in_abs_real,
  2726. in_ln_real :
  2727. begin
  2728. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2729. { converting an int64 to double on platforms without }
  2730. { extended can cause precision loss }
  2731. if not(left.nodetype in [ordconstn,realconstn]) then
  2732. inserttypeconv(left,pbestrealtype^);
  2733. resultdef:=pbestrealtype^;
  2734. end;
  2735. in_trunc_real,
  2736. in_round_real :
  2737. begin
  2738. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2739. { for direct float rounding, no best real type cast should be necessary }
  2740. if not((left.resultdef.typ=floatdef) and
  2741. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2742. { converting an int64 to double on platforms without }
  2743. { extended can cause precision loss }
  2744. not(left.nodetype in [ordconstn,realconstn]) then
  2745. inserttypeconv(left,pbestrealtype^);
  2746. resultdef:=s64inttype;
  2747. end;
  2748. in_pi_real :
  2749. begin
  2750. resultdef:=pbestrealtype^;
  2751. end;
  2752. in_abs_long:
  2753. begin
  2754. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2755. inserttypeconv(left,s32inttype);
  2756. resultdef:=s32inttype;
  2757. end;
  2758. in_sqr_real,
  2759. in_sqrt_real :
  2760. begin
  2761. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2762. setfloatresultdef;
  2763. end;
  2764. {$ifdef SUPPORT_MMX}
  2765. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2766. begin
  2767. end;
  2768. {$endif SUPPORT_MMX}
  2769. in_unaligned_x:
  2770. begin
  2771. resultdef:=left.resultdef;
  2772. end;
  2773. in_assert_x_y :
  2774. begin
  2775. resultdef:=voidtype;
  2776. if assigned(left) then
  2777. begin
  2778. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2779. { check type }
  2780. if is_boolean(left.resultdef) then
  2781. begin
  2782. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2783. { must always be a string }
  2784. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2785. end
  2786. else
  2787. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2788. end
  2789. else
  2790. CGMessage(type_e_mismatch);
  2791. if (cs_do_assertion in current_settings.localswitches) then
  2792. include(current_procinfo.flags,pi_do_call);
  2793. end;
  2794. in_prefetch_var:
  2795. resultdef:=voidtype;
  2796. in_get_frame,
  2797. in_get_caller_frame,
  2798. in_get_caller_addr:
  2799. begin
  2800. resultdef:=voidpointertype;
  2801. end;
  2802. in_rol_x,
  2803. in_ror_x,
  2804. in_sar_x:
  2805. begin
  2806. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2807. resultdef:=left.resultdef;
  2808. end;
  2809. in_rol_x_y,
  2810. in_ror_x_y,
  2811. in_sar_x_y:
  2812. begin
  2813. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2814. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2815. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2816. end;
  2817. in_bsf_x,
  2818. in_bsr_x:
  2819. begin
  2820. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2821. if not is_integer(left.resultdef) then
  2822. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2823. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2824. resultdef:=u64inttype
  2825. else
  2826. resultdef:=u32inttype
  2827. end;
  2828. in_objc_selector_x:
  2829. begin
  2830. result:=cobjcselectornode.create(left);
  2831. { reused }
  2832. left:=nil;
  2833. end;
  2834. in_objc_protocol_x:
  2835. begin
  2836. result:=cobjcprotocolnode.create(left);
  2837. { reused }
  2838. left:=nil;
  2839. end;
  2840. in_objc_encode_x:
  2841. begin
  2842. result:=handle_objc_encode;
  2843. end;
  2844. in_default_x:
  2845. begin
  2846. result:=handle_default;
  2847. end;
  2848. in_box_x:
  2849. begin
  2850. result:=handle_box;
  2851. end;
  2852. in_unbox_x_y:
  2853. begin
  2854. result:=handle_unbox;
  2855. end;
  2856. else
  2857. internalerror(8);
  2858. end;
  2859. end;
  2860. if not assigned(result) and not
  2861. codegenerror then
  2862. result:=simplify(false);
  2863. end;
  2864. function tinlinenode.pass_1 : tnode;
  2865. var
  2866. hp,hpp,resultnode : tnode;
  2867. shiftconst: longint;
  2868. tempnode: ttempcreatenode;
  2869. newstatement: tstatementnode;
  2870. newblock: tblocknode;
  2871. begin
  2872. result:=nil;
  2873. { if we handle writeln; left contains no valid address }
  2874. if assigned(left) then
  2875. begin
  2876. if left.nodetype=callparan then
  2877. tcallparanode(left).firstcallparan
  2878. else
  2879. firstpass(left);
  2880. end;
  2881. { intern const should already be handled }
  2882. if nf_inlineconst in flags then
  2883. internalerror(200104044);
  2884. case inlinenumber of
  2885. in_lo_qword,
  2886. in_hi_qword,
  2887. in_lo_long,
  2888. in_hi_long,
  2889. in_lo_word,
  2890. in_hi_word:
  2891. begin
  2892. shiftconst := 0;
  2893. case inlinenumber of
  2894. in_hi_qword:
  2895. shiftconst := 32;
  2896. in_hi_long:
  2897. shiftconst := 16;
  2898. in_hi_word:
  2899. shiftconst := 8;
  2900. end;
  2901. if shiftconst <> 0 then
  2902. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2903. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2904. else
  2905. result := ctypeconvnode.create_internal(left,resultdef);
  2906. left := nil;
  2907. firstpass(result);
  2908. end;
  2909. in_sizeof_x:
  2910. begin
  2911. expectloc:=LOC_REGISTER;
  2912. end;
  2913. in_typeof_x:
  2914. begin
  2915. expectloc:=LOC_REGISTER;
  2916. end;
  2917. in_length_x:
  2918. begin
  2919. result:=first_length;
  2920. end;
  2921. in_typeinfo_x:
  2922. begin
  2923. expectloc:=LOC_REGISTER;
  2924. end;
  2925. in_assigned_x:
  2926. begin
  2927. result:=first_assigned;
  2928. end;
  2929. in_pred_x,
  2930. in_succ_x:
  2931. begin
  2932. expectloc:=LOC_REGISTER;
  2933. { in case of range/overflow checking, use a regular addnode
  2934. because it's too complex to handle correctly otherwise }
  2935. {$ifndef jvm}
  2936. { enums are class instances in the JVM -> always need conversion }
  2937. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  2938. {$endif}
  2939. begin
  2940. { create constant 1 }
  2941. hp:=cordconstnode.create(1,left.resultdef,false);
  2942. typecheckpass(hp);
  2943. if not is_integer(hp.resultdef) then
  2944. inserttypeconv_internal(hp,sinttype);
  2945. { avoid type errors from the addn/subn }
  2946. if not is_integer(left.resultdef) then
  2947. inserttypeconv_internal(left,sinttype);
  2948. { addition/substraction depending on succ/pred }
  2949. if inlinenumber=in_succ_x then
  2950. hp:=caddnode.create(addn,left,hp)
  2951. else
  2952. hp:=caddnode.create(subn,left,hp);
  2953. { assign result of addition }
  2954. if not(is_integer(resultdef)) then
  2955. inserttypeconv(hp,torddef.create(
  2956. {$ifdef cpu64bitaddr}
  2957. s64bit,
  2958. {$else cpu64bitaddr}
  2959. s32bit,
  2960. {$endif cpu64bitaddr}
  2961. get_min_value(resultdef),
  2962. get_max_value(resultdef)))
  2963. else
  2964. inserttypeconv(hp,resultdef);
  2965. { avoid any possible errors/warnings }
  2966. inserttypeconv_internal(hp,resultdef);
  2967. { firstpass it }
  2968. firstpass(hp);
  2969. { left is reused }
  2970. left:=nil;
  2971. { return new node }
  2972. result:=hp;
  2973. end;
  2974. end;
  2975. in_setlength_x:
  2976. result:=first_setlength;
  2977. in_copy_x:
  2978. result:=first_copy;
  2979. in_initialize_x,
  2980. in_finalize_x:
  2981. begin
  2982. expectloc:=LOC_VOID;
  2983. end;
  2984. in_inc_x,
  2985. in_dec_x:
  2986. begin
  2987. expectloc:=LOC_VOID;
  2988. { range/overflow checking doesn't work properly }
  2989. { with the inc/dec code that's generated (JM) }
  2990. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2991. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2992. trigger an overflow. For uint32 it works because then the operation is done
  2993. in 64bit. Range checking is not applicable to pointers either }
  2994. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  2995. {$ifdef jvm}
  2996. { enums are class instances on the JVM -> special treatment }
  2997. or (tcallparanode(left).left.resultdef.typ=enumdef)
  2998. {$endif}
  2999. then
  3000. { convert to simple add (JM) }
  3001. begin
  3002. newblock := internalstatements(newstatement);
  3003. { extra parameter? }
  3004. if assigned(tcallparanode(left).right) then
  3005. begin
  3006. { Yes, use for add node }
  3007. hpp := tcallparanode(tcallparanode(left).right).left;
  3008. tcallparanode(tcallparanode(left).right).left := nil;
  3009. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3010. CGMessage(parser_e_illegal_expression);
  3011. end
  3012. else
  3013. begin
  3014. { no, create constant 1 }
  3015. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3016. end;
  3017. typecheckpass(hpp);
  3018. { make sure we don't call functions part of the left node twice (and generally }
  3019. { optimize the code generation) }
  3020. if node_complexity(tcallparanode(left).left) > 1 then
  3021. begin
  3022. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3023. addstatement(newstatement,tempnode);
  3024. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3025. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3026. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3027. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3028. end
  3029. else
  3030. begin
  3031. hp := tcallparanode(left).left.getcopy;
  3032. tempnode := nil;
  3033. end;
  3034. resultnode := hp.getcopy;
  3035. { avoid type errors from the addn/subn }
  3036. if not is_integer(resultnode.resultdef) then
  3037. begin
  3038. inserttypeconv_internal(hp,sinttype);
  3039. inserttypeconv_internal(hpp,sinttype);
  3040. end;
  3041. { addition/substraction depending on inc/dec }
  3042. if inlinenumber = in_inc_x then
  3043. hpp := caddnode.create(addn,hp,hpp)
  3044. else
  3045. hpp := caddnode.create(subn,hp,hpp);
  3046. { assign result of addition }
  3047. if not(is_integer(resultnode.resultdef)) then
  3048. inserttypeconv(hpp,torddef.create(
  3049. {$ifdef cpu64bitaddr}
  3050. s64bit,
  3051. {$else cpu64bitaddr}
  3052. s32bit,
  3053. {$endif cpu64bitaddr}
  3054. get_min_value(resultnode.resultdef),
  3055. get_max_value(resultnode.resultdef)))
  3056. else
  3057. inserttypeconv(hpp,resultnode.resultdef);
  3058. { avoid any possible warnings }
  3059. inserttypeconv_internal(hpp,resultnode.resultdef);
  3060. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3061. { deallocate the temp }
  3062. if assigned(tempnode) then
  3063. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3064. { firstpass it }
  3065. firstpass(tnode(newblock));
  3066. { return new node }
  3067. result := newblock;
  3068. end;
  3069. end;
  3070. in_include_x_y,
  3071. in_exclude_x_y:
  3072. begin
  3073. result:=first_IncludeExclude;
  3074. end;
  3075. in_pack_x_y_z,
  3076. in_unpack_x_y_z:
  3077. begin
  3078. result:=first_pack_unpack;
  3079. end;
  3080. in_exp_real:
  3081. begin
  3082. result:= first_exp_real;
  3083. end;
  3084. in_round_real:
  3085. begin
  3086. result:= first_round_real;
  3087. end;
  3088. in_trunc_real:
  3089. begin
  3090. result:= first_trunc_real;
  3091. end;
  3092. in_int_real:
  3093. begin
  3094. result:= first_int_real;
  3095. end;
  3096. in_frac_real:
  3097. begin
  3098. result:= first_frac_real;
  3099. end;
  3100. in_cos_real:
  3101. begin
  3102. result:= first_cos_real;
  3103. end;
  3104. in_sin_real:
  3105. begin
  3106. result := first_sin_real;
  3107. end;
  3108. in_arctan_real:
  3109. begin
  3110. result := first_arctan_real;
  3111. end;
  3112. in_pi_real :
  3113. begin
  3114. result := first_pi;
  3115. end;
  3116. in_abs_real:
  3117. begin
  3118. result := first_abs_real;
  3119. end;
  3120. in_abs_long:
  3121. begin
  3122. result := first_abs_long;
  3123. end;
  3124. in_sqr_real:
  3125. begin
  3126. result := first_sqr_real;
  3127. end;
  3128. in_sqrt_real:
  3129. begin
  3130. result := first_sqrt_real;
  3131. end;
  3132. in_ln_real:
  3133. begin
  3134. result := first_ln_real;
  3135. end;
  3136. {$ifdef SUPPORT_MMX}
  3137. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3138. begin
  3139. end;
  3140. {$endif SUPPORT_MMX}
  3141. in_assert_x_y :
  3142. begin
  3143. result:=first_assert;
  3144. end;
  3145. in_low_x,
  3146. in_high_x:
  3147. internalerror(200104047);
  3148. in_slice_x:
  3149. internalerror(2005101501);
  3150. in_ord_x,
  3151. in_chr_byte:
  3152. begin
  3153. { should not happend as it's converted to typeconv }
  3154. internalerror(200104045);
  3155. end;
  3156. in_ofs_x :
  3157. internalerror(2000101001);
  3158. in_seg_x :
  3159. internalerror(200104046);
  3160. in_settextbuf_file_x,
  3161. in_reset_typedfile,
  3162. in_rewrite_typedfile,
  3163. in_str_x_string,
  3164. in_val_x,
  3165. in_read_x,
  3166. in_readln_x,
  3167. in_write_x,
  3168. in_writeln_x :
  3169. begin
  3170. { should be handled by pass_typecheck }
  3171. internalerror(200108234);
  3172. end;
  3173. in_get_frame:
  3174. begin
  3175. include(current_procinfo.flags,pi_needs_stackframe);
  3176. expectloc:=LOC_CREGISTER;
  3177. end;
  3178. in_get_caller_frame:
  3179. begin
  3180. expectloc:=LOC_REGISTER;
  3181. end;
  3182. in_get_caller_addr:
  3183. begin
  3184. expectloc:=LOC_REGISTER;
  3185. end;
  3186. in_prefetch_var:
  3187. begin
  3188. expectloc:=LOC_VOID;
  3189. end;
  3190. in_unaligned_x:
  3191. begin
  3192. expectloc:=tcallparanode(left).left.expectloc;
  3193. end;
  3194. in_rol_x,
  3195. in_rol_x_y,
  3196. in_ror_x,
  3197. in_ror_x_y,
  3198. in_sar_x,
  3199. in_sar_x_y,
  3200. in_bsf_x,
  3201. in_bsr_x:
  3202. expectloc:=LOC_REGISTER;
  3203. in_new_x:
  3204. result:=first_new;
  3205. in_box_x:
  3206. result:=first_box;
  3207. in_unbox_x_y:
  3208. result:=first_unbox;
  3209. else
  3210. internalerror(89);
  3211. end;
  3212. end;
  3213. {$maxfpuregisters default}
  3214. function tinlinenode.docompare(p: tnode): boolean;
  3215. begin
  3216. docompare :=
  3217. inherited docompare(p) and
  3218. (inlinenumber = tinlinenode(p).inlinenumber);
  3219. end;
  3220. function tinlinenode.first_pi : tnode;
  3221. begin
  3222. result:=crealconstnode.create(getpi,pbestrealtype^);
  3223. end;
  3224. function tinlinenode.first_arctan_real : tnode;
  3225. begin
  3226. { create the call to the helper }
  3227. { on entry left node contains the parameter }
  3228. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3229. ccallparanode.create(left,nil));
  3230. left := nil;
  3231. end;
  3232. function tinlinenode.first_abs_real : tnode;
  3233. begin
  3234. { create the call to the helper }
  3235. { on entry left node contains the parameter }
  3236. first_abs_real := ccallnode.createintern('fpc_abs_real',
  3237. ccallparanode.create(left,nil));
  3238. left := nil;
  3239. end;
  3240. function tinlinenode.first_sqr_real : tnode;
  3241. begin
  3242. {$ifndef cpufpemu}
  3243. { this procedure might be only used for cpus definining cpufpemu else
  3244. the optimizer might go into an endless loop when doing x*x -> changes }
  3245. internalerror(2011092401);
  3246. {$endif cpufpemu}
  3247. { create the call to the helper }
  3248. { on entry left node contains the parameter }
  3249. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3250. ccallparanode.create(left,nil)),resultdef);
  3251. left := nil;
  3252. end;
  3253. function tinlinenode.first_sqrt_real : tnode;
  3254. begin
  3255. { create the call to the helper }
  3256. { on entry left node contains the parameter }
  3257. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3258. ccallparanode.create(left,nil)),resultdef);
  3259. left := nil;
  3260. end;
  3261. function tinlinenode.first_ln_real : tnode;
  3262. begin
  3263. { create the call to the helper }
  3264. { on entry left node contains the parameter }
  3265. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3266. ccallparanode.create(left,nil));
  3267. left := nil;
  3268. end;
  3269. function tinlinenode.first_cos_real : tnode;
  3270. begin
  3271. { create the call to the helper }
  3272. { on entry left node contains the parameter }
  3273. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3274. ccallparanode.create(left,nil));
  3275. left := nil;
  3276. end;
  3277. function tinlinenode.first_sin_real : tnode;
  3278. begin
  3279. { create the call to the helper }
  3280. { on entry left node contains the parameter }
  3281. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3282. ccallparanode.create(left,nil));
  3283. left := nil;
  3284. end;
  3285. function tinlinenode.first_exp_real : tnode;
  3286. begin
  3287. { create the call to the helper }
  3288. { on entry left node contains the parameter }
  3289. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3290. left := nil;
  3291. end;
  3292. function tinlinenode.first_int_real : tnode;
  3293. begin
  3294. { create the call to the helper }
  3295. { on entry left node contains the parameter }
  3296. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3297. left := nil;
  3298. end;
  3299. function tinlinenode.first_frac_real : tnode;
  3300. begin
  3301. { create the call to the helper }
  3302. { on entry left node contains the parameter }
  3303. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3304. left := nil;
  3305. end;
  3306. function tinlinenode.first_round_real : tnode;
  3307. begin
  3308. { create the call to the helper }
  3309. { on entry left node contains the parameter }
  3310. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3311. left := nil;
  3312. end;
  3313. function tinlinenode.first_trunc_real : tnode;
  3314. begin
  3315. { create the call to the helper }
  3316. { on entry left node contains the parameter }
  3317. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3318. left := nil;
  3319. end;
  3320. function tinlinenode.first_abs_long : tnode;
  3321. begin
  3322. expectloc:=LOC_REGISTER;
  3323. result:=nil;
  3324. end;
  3325. function tinlinenode.first_IncludeExclude: tnode;
  3326. begin
  3327. result:=nil;
  3328. expectloc:=LOC_VOID;
  3329. end;
  3330. function tinlinenode.first_setlength: tnode;
  3331. var
  3332. paras : tnode;
  3333. npara,
  3334. ppn : tcallparanode;
  3335. dims,
  3336. counter : integer;
  3337. isarray : boolean;
  3338. destppn : tnode;
  3339. newstatement : tstatementnode;
  3340. temp : ttempcreatenode;
  3341. newblock : tnode;
  3342. begin
  3343. paras:=left;
  3344. ppn:=tcallparanode(paras);
  3345. dims:=0;
  3346. while assigned(ppn.right) do
  3347. begin
  3348. inc(dims);
  3349. ppn:=tcallparanode(ppn.right);
  3350. end;
  3351. destppn:=ppn.left;
  3352. isarray:=is_dynamic_array(destppn.resultdef);
  3353. { first param must be a string or dynamic array ...}
  3354. if isarray then
  3355. begin
  3356. { create statements with call initialize the arguments and
  3357. call fpc_dynarr_setlength }
  3358. newblock:=internalstatements(newstatement);
  3359. { get temp for array of lengths }
  3360. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3361. addstatement(newstatement,temp);
  3362. { load array of lengths }
  3363. ppn:=tcallparanode(paras);
  3364. counter:=dims-1;
  3365. while assigned(ppn.right) do
  3366. begin
  3367. addstatement(newstatement,cassignmentnode.create(
  3368. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3369. ppn.left));
  3370. ppn.left:=nil;
  3371. dec(counter);
  3372. ppn:=tcallparanode(ppn.right);
  3373. end;
  3374. { destppn is also reused }
  3375. ppn.left:=nil;
  3376. { create call to fpc_dynarr_setlength }
  3377. npara:=ccallparanode.create(caddrnode.create_internal
  3378. (ctemprefnode.create(temp)),
  3379. ccallparanode.create(cordconstnode.create
  3380. (dims,sinttype,true),
  3381. ccallparanode.create(caddrnode.create_internal
  3382. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3383. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3384. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3385. addstatement(newstatement,ctempdeletenode.create(temp));
  3386. end
  3387. else if is_ansistring(destppn.resultdef) then
  3388. begin
  3389. newblock:=ccallnode.createintern(
  3390. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3391. ccallparanode.create(
  3392. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3393. paras
  3394. )
  3395. );
  3396. { we reused the parameters, make sure we don't release them }
  3397. left:=nil;
  3398. end
  3399. else
  3400. begin
  3401. { we can reuse the supplied parameters }
  3402. newblock:=ccallnode.createintern(
  3403. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3404. { we reused the parameters, make sure we don't release them }
  3405. left:=nil;
  3406. end;
  3407. result:=newblock;
  3408. end;
  3409. function tinlinenode.first_copy: tnode;
  3410. var
  3411. lowppn,
  3412. highppn,
  3413. npara,
  3414. paras : tnode;
  3415. ppn : tcallparanode;
  3416. paradef : tdef;
  3417. counter : integer;
  3418. begin
  3419. { determine copy function to use based on the first argument,
  3420. also count the number of arguments in this loop }
  3421. counter:=1;
  3422. paras:=left;
  3423. ppn:=tcallparanode(paras);
  3424. while assigned(ppn.right) do
  3425. begin
  3426. inc(counter);
  3427. ppn:=tcallparanode(ppn.right);
  3428. end;
  3429. paradef:=ppn.left.resultdef;
  3430. if is_ansistring(resultdef) then
  3431. { keep the specific kind of ansistringdef as result }
  3432. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3433. else if is_widestring(resultdef) then
  3434. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3435. else if is_unicodestring(resultdef) then
  3436. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3437. { can't check for resultdef = cansichartype, because resultdef=
  3438. cshortstringtype here }
  3439. else if is_char(paradef) then
  3440. result:=ccallnode.createintern('fpc_char_copy',paras)
  3441. else if is_dynamic_array(resultdef) then
  3442. begin
  3443. { create statements with call }
  3444. if (counter=3) then
  3445. begin
  3446. highppn:=tcallparanode(paras).left.getcopy;
  3447. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3448. end
  3449. else
  3450. begin
  3451. { copy the whole array using [0..high(sizeint)] range }
  3452. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3453. lowppn:=cordconstnode.create(0,sinttype,false);
  3454. end;
  3455. { create call to fpc_dynarray_copy }
  3456. npara:=ccallparanode.create(highppn,
  3457. ccallparanode.create(lowppn,
  3458. ccallparanode.create(caddrnode.create_internal
  3459. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3460. ccallparanode.create
  3461. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3462. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3463. ppn.left:=nil;
  3464. paras.free;
  3465. end
  3466. else
  3467. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3468. { parameters are reused }
  3469. left:=nil;
  3470. end;
  3471. function tinlinenode.first_new: tnode;
  3472. begin
  3473. internalerror(2011012201);
  3474. result:=nil;
  3475. end;
  3476. function tinlinenode.first_length: tnode;
  3477. begin
  3478. result:=nil;
  3479. if is_shortstring(left.resultdef) then
  3480. expectloc:=left.expectloc
  3481. else
  3482. begin
  3483. { ansi/wide string }
  3484. expectloc:=LOC_REGISTER;
  3485. end;
  3486. end;
  3487. function tinlinenode.first_assigned: tnode;
  3488. begin
  3489. result:=nil;
  3490. expectloc := LOC_JUMP;
  3491. end;
  3492. function tinlinenode.first_assert: tnode;
  3493. begin
  3494. result:=nil;
  3495. expectloc:=LOC_VOID;
  3496. {$ifdef i386}
  3497. { hack: on i386, the fourth parameter is passed via memory ->
  3498. we have to allocate enough stack space for it on targets that
  3499. use a fixed stack }
  3500. current_procinfo.allocate_push_parasize(4);
  3501. {$endif}
  3502. end;
  3503. function tinlinenode.handle_box: tnode;
  3504. begin
  3505. result:=nil;
  3506. if not assigned(left) or
  3507. assigned(tcallparanode(left).right) then
  3508. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3509. resultdef:=class_tobject;
  3510. end;
  3511. function tinlinenode.handle_unbox: tnode;
  3512. begin
  3513. result:=nil;
  3514. if not assigned(left) or
  3515. not assigned(tcallparanode(left).right) or
  3516. assigned(tcallparanode(tcallparanode(left).right).right) then
  3517. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3518. if tcallparanode(left).left.nodetype<>typen then
  3519. internalerror(2011071701);
  3520. ttypenode(tcallparanode(left).left).allowed:=true;
  3521. resultdef:=tcallparanode(left).left.resultdef;
  3522. end;
  3523. function tinlinenode.first_pack_unpack: tnode;
  3524. var
  3525. loopstatement : tstatementnode;
  3526. loop : tblocknode;
  3527. loopvar : ttempcreatenode;
  3528. tempnode,
  3529. source,
  3530. target,
  3531. index,
  3532. unpackednode,
  3533. packednode,
  3534. sourcevecindex,
  3535. targetvecindex,
  3536. loopbody : tnode;
  3537. temprangedef : tdef;
  3538. ulorange,
  3539. uhirange,
  3540. plorange,
  3541. phirange : TConstExprInt;
  3542. begin
  3543. { transform into a for loop which assigns the data of the (un)packed }
  3544. { array to the other one }
  3545. source := left;
  3546. if (inlinenumber = in_unpack_x_y_z) then
  3547. begin
  3548. target := tcallparanode(source).right;
  3549. index := tcallparanode(target).right;
  3550. packednode := tcallparanode(source).left;
  3551. unpackednode := tcallparanode(target).left;
  3552. end
  3553. else
  3554. begin
  3555. index := tcallparanode(source).right;
  3556. target := tcallparanode(index).right;
  3557. packednode := tcallparanode(target).left;
  3558. unpackednode := tcallparanode(source).left;
  3559. end;
  3560. source := tcallparanode(source).left;
  3561. target := tcallparanode(target).left;
  3562. index := tcallparanode(index).left;
  3563. loop := internalstatements(loopstatement);
  3564. loopvar := ctempcreatenode.create(
  3565. tarraydef(packednode.resultdef).rangedef,
  3566. tarraydef(packednode.resultdef).rangedef.size,
  3567. tt_persistent,true);
  3568. addstatement(loopstatement,loopvar);
  3569. { For range checking: we have to convert to an integer type (in case the index type }
  3570. { is an enum), add the index and loop variable together, convert the result }
  3571. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3572. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3573. { errors }
  3574. temprangedef:=nil;
  3575. getrange(unpackednode.resultdef,ulorange,uhirange);
  3576. getrange(packednode.resultdef,plorange,phirange);
  3577. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3578. sourcevecindex := ctemprefnode.create(loopvar);
  3579. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3580. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3581. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3582. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3583. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3584. if (inlinenumber = in_pack_x_y_z) then
  3585. begin
  3586. { swap source and target vec indices }
  3587. tempnode := sourcevecindex;
  3588. sourcevecindex := targetvecindex;
  3589. targetvecindex := tempnode;
  3590. end;
  3591. { create the assignment in the loop body }
  3592. loopbody :=
  3593. cassignmentnode.create(
  3594. cvecnode.create(target.getcopy,targetvecindex),
  3595. cvecnode.create(source.getcopy,sourcevecindex)
  3596. );
  3597. { create the actual for loop }
  3598. tempnode := cfornode.create(
  3599. ctemprefnode.create(loopvar),
  3600. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3601. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3602. loopbody,
  3603. false);
  3604. addstatement(loopstatement,tempnode);
  3605. { free the loop counter }
  3606. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3607. result := loop;
  3608. end;
  3609. end.