mbari.patch 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555
  1. diff --git a/ChangeLog b/ChangeLog
  2. index e66e305..e4c539a 100644
  3. --- a/ChangeLog
  4. +++ b/ChangeLog
  5. @@ -1,3 +1,20 @@
  6. +Wed Feb 27 12:01:19 2009 Brent Roman <brent@mbari.org>
  7. +
  8. + * rubysig.c: added support for __anyPowerPC__
  9. +
  10. + * version.h: bumped date
  11. +
  12. +Mon Feb 25 10:53:52 2008 Brent Roman <brent@mbari.org>
  13. +
  14. + * backported MBARI 8A patches still
  15. + fails YAML test_object_id_collision
  16. +
  17. +Mon Feb 16 10:53:52 2008 Brent Roman <brent@mbari.org>
  18. +
  19. + * backport from 1.8.7p72-mbari of MBARI patches described at
  20. + http://sites.google.com/site/brentsrubypatches
  21. + fails YAML test_object_id_collision
  22. +
  23. Fri Aug 8 10:53:52 2008 Tanaka Akira <akr@fsij.org>
  24. * lib/resolv.rb: randomize source port and transaction id.
  25. diff --git a/common.mk b/common.mk
  26. index 6d5b35d..3e43e41 100644
  27. --- a/common.mk
  28. +++ b/common.mk
  29. @@ -441,7 +441,7 @@ variable.$(OBJEXT): {$(VPATH)}variable.c {$(VPATH)}ruby.h config.h \
  30. {$(VPATH)}env.h {$(VPATH)}node.h {$(VPATH)}st.h {$(VPATH)}util.h
  31. version.$(OBJEXT): {$(VPATH)}version.c {$(VPATH)}ruby.h config.h \
  32. {$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h \
  33. - {$(VPATH)}version.h
  34. + {$(VPATH)}rubysig.h {$(VPATH)}version.h
  35. dist: $(PROGRAM)
  36. $(RUNRUBY) $(srcdir)/distruby.rb
  37. diff --git a/configure.in b/configure.in
  38. index c775862..aafeb30 100644
  39. --- a/configure.in
  40. +++ b/configure.in
  41. @@ -139,6 +139,24 @@ AC_ARG_ENABLE(frame-address,
  42. if test $frame_address = yes; then
  43. AC_DEFINE(USE_BUILTIN_FRAME_ADDRESS)
  44. fi
  45. +AC_ARG_ENABLE(mbari-api,
  46. + [ --enable-mbari-api enable API changes from the MBARI patches. ],
  47. + [mbari_api=$enableval])
  48. +if test "$mbari_api" = yes; then
  49. + AC_DEFINE(MBARI_API)
  50. +fi
  51. +AC_ARG_WITH(wipe-sites,
  52. +[ --with-wipe-sites=MASK override default STACK_WIPES_SITES mask in rubysig.h],
  53. +[wipe_sites=$withval])
  54. +if test "$wipe_sites" != ""; then
  55. + case $wipe_sites in
  56. + none|no) wipe_sites=0x0;;
  57. + yes) wipe_sites=;;
  58. + esac
  59. + if test -n "$wipe_sites"; then
  60. + AC_DEFINE_UNQUOTED(STACK_WIPE_SITES,$wipe_sites)
  61. + fi
  62. +fi
  63. AC_ARG_PROGRAM
  64. diff --git a/eval.c b/eval.c
  65. index 54a0fee..8002203 100644
  66. --- a/eval.c
  67. +++ b/eval.c
  68. @@ -222,9 +222,9 @@ VALUE rb_cProc;
  69. VALUE rb_cBinding;
  70. static VALUE proc_invoke _((VALUE,VALUE,VALUE,VALUE));
  71. static VALUE rb_f_binding _((VALUE));
  72. -static void rb_f_END _((void));
  73. +NOINLINE(static void rb_f_END _((void)));
  74. static VALUE rb_f_block_given_p _((void));
  75. -static VALUE block_pass _((VALUE,NODE*));
  76. +static VALUE block_pass _((volatile VALUE, NODE *volatile));
  77. VALUE rb_cMethod;
  78. static VALUE method_call _((int, VALUE*, VALUE));
  79. @@ -246,6 +246,30 @@ static int scope_vmode;
  80. VALUE (*ruby_sandbox_save)_((rb_thread_t));
  81. VALUE (*ruby_sandbox_restore)_((rb_thread_t));
  82. NODE* ruby_current_node;
  83. +
  84. +#if 0
  85. +#define SET_CURRENT_SOURCE() (ruby_sourcefile = ruby_current_node->nd_file, \
  86. + ruby_sourceline = nd_line(ruby_current_node))
  87. +#else
  88. +#define SET_CURRENT_SOURCE() ((void)0)
  89. +#endif
  90. +
  91. +void
  92. +ruby_set_current_source()
  93. +{
  94. + if (ruby_current_node) {
  95. + ruby_sourcefile = ruby_current_node->nd_file;
  96. + ruby_sourceline = nd_line(ruby_current_node);
  97. + }
  98. +}
  99. +
  100. +#ifdef MBARI_API
  101. +#define SET_METHOD_SOURCE() ruby_set_current_source()
  102. +#else
  103. +#define SET_METHOD_SOURCE() (void)0
  104. +#endif
  105. +
  106. +
  107. int ruby_safe_level = 0;
  108. /* safe-level:
  109. 0 - strings from streams/environment/ARGV are tainted (default)
  110. @@ -490,7 +514,7 @@ rb_get_method_body(klassp, idp, noexp)
  111. {
  112. ID id = *idp;
  113. VALUE klass = *klassp;
  114. - VALUE origin;
  115. + VALUE origin = 0;
  116. NODE * volatile body;
  117. struct cache_entry *ent;
  118. @@ -729,6 +753,7 @@ rb_attr(klass, id, read, write, ex)
  119. if (!name) {
  120. rb_raise(rb_eArgError, "argument needs to be symbol or string");
  121. }
  122. + SET_METHOD_SOURCE();
  123. len = strlen(name)+2;
  124. buf = ALLOCA_N(char,len);
  125. snprintf(buf, len, "@%s", name);
  126. @@ -769,11 +794,11 @@ static unsigned long frame_unique = 0;
  127. _frame.argc = 0; \
  128. _frame.flags = 0; \
  129. _frame.uniq = frame_unique++; \
  130. - ruby_frame = &_frame
  131. + ruby_frame = (struct FRAME *)&_frame
  132. #define POP_FRAME() \
  133. ruby_current_node = _frame.node; \
  134. - ruby_frame = _frame.prev; \
  135. + ruby_frame = _frame.prev; \
  136. } while (0)
  137. struct BLOCK {
  138. @@ -967,7 +992,7 @@ rb_svar(cnt)
  139. ID id;
  140. if (!ruby_scope->local_tbl) return NULL;
  141. - if (cnt >= ruby_scope->local_tbl[0]) return NULL;
  142. + if ((ID)cnt >= ruby_scope->local_tbl[0]) return NULL;
  143. id = ruby_scope->local_tbl[cnt+1];
  144. while (vars) {
  145. if (vars->id == id) return &vars->val;
  146. @@ -1030,7 +1055,26 @@ static struct tag *prot_tag;
  147. #define PROT_LAMBDA INT2FIX(2) /* 5 */
  148. #define PROT_YIELD INT2FIX(3) /* 7 */
  149. -#define EXEC_TAG() (FLUSH_REGISTER_WINDOWS, ruby_setjmp(((void)0), prot_tag->buf))
  150. +#if STACK_WIPE_SITES & 0x42
  151. +#ifdef __GNUC__
  152. +static inline int wipeAfter(int) __attribute__((always_inline));
  153. +#endif
  154. +static inline int wipeAfter(int status)
  155. +{
  156. + rb_gc_wipe_stack();
  157. + return status;
  158. +}
  159. +#else
  160. +#define wipeAfter(status) status
  161. +#endif
  162. +#if STACK_WIPE_SITES & 2
  163. +#define wipeAfterTag(status) wipeAfter(status)
  164. +#else
  165. +#define wipeAfterTag(status) status
  166. +#endif
  167. +
  168. +#define EXEC_TAG_0() ruby_setjmp(((void)0), prot_tag->buf)
  169. +#define EXEC_TAG() wipeAfterTag(EXEC_TAG_0())
  170. #define JUMP_TAG(st) do { \
  171. ruby_frame = prot_tag->frame; \
  172. @@ -1106,10 +1150,17 @@ static void scope_dup _((struct SCOPE *));
  173. } while (0)
  174. static VALUE rb_eval _((VALUE,NODE*));
  175. -static VALUE eval _((VALUE,VALUE,VALUE,char*,int));
  176. -static NODE *compile _((VALUE, char*, int));
  177. +static VALUE eval _((VALUE,VALUE,volatile VALUE,const char* volatile,int));
  178. +static NODE *compile _((VALUE, const char*, int));
  179. -static VALUE rb_yield_0 _((VALUE, VALUE, VALUE, int, int));
  180. +static VALUE rb_yield_0
  181. + _((volatile VALUE, volatile VALUE, VALUE, int, volatile int));
  182. +
  183. +#if STACK_WIPE_SITES & 0x20
  184. +#define wipeBeforeYield() rb_gc_wipe_stack()
  185. +#else
  186. +#define wipeBeforeYield() (void)0
  187. +#endif
  188. #define YIELD_LAMBDA_CALL 1
  189. #define YIELD_PROC_CALL 2
  190. @@ -1118,7 +1169,7 @@ static VALUE rb_yield_0 _((VALUE, VALUE, VALUE, int, int));
  191. #define YIELD_FUNC_SVALUE 2
  192. static VALUE rb_call _((VALUE,VALUE,ID,int,const VALUE*,int,VALUE));
  193. -static VALUE module_setup _((VALUE,NODE*));
  194. +static VALUE module_setup _((VALUE,NODE *volatile));
  195. static VALUE massign _((VALUE,NODE*,VALUE,int));
  196. static void assign _((VALUE,NODE*,VALUE,int));
  197. @@ -1150,22 +1201,6 @@ static VALUE trace_func = 0;
  198. static int tracing = 0;
  199. static void call_trace_func _((rb_event_t,NODE*,VALUE,ID,VALUE));
  200. -#if 0
  201. -#define SET_CURRENT_SOURCE() (ruby_sourcefile = ruby_current_node->nd_file, \
  202. - ruby_sourceline = nd_line(ruby_current_node))
  203. -#else
  204. -#define SET_CURRENT_SOURCE() ((void)0)
  205. -#endif
  206. -
  207. -void
  208. -ruby_set_current_source()
  209. -{
  210. - if (ruby_current_node) {
  211. - ruby_sourcefile = ruby_current_node->nd_file;
  212. - ruby_sourceline = nd_line(ruby_current_node);
  213. - }
  214. -}
  215. -
  216. static void
  217. #ifdef HAVE_STDARG_PROTOTYPES
  218. warn_printf(const char *fmt, ...)
  219. @@ -1227,20 +1262,15 @@ set_backtrace(info, bt)
  220. static void
  221. error_print()
  222. {
  223. - VALUE errat = Qnil; /* OK */
  224. + VALUE errat;
  225. volatile VALUE eclass, e;
  226. - char *einfo;
  227. + const char * einfo;
  228. long elen;
  229. if (NIL_P(ruby_errinfo)) return;
  230. PUSH_TAG(PROT_NONE);
  231. - if (EXEC_TAG() == 0) {
  232. - errat = get_backtrace(ruby_errinfo);
  233. - }
  234. - else {
  235. - errat = Qnil;
  236. - }
  237. + errat = EXEC_TAG() ? Qnil : get_backtrace(ruby_errinfo);
  238. if (EXEC_TAG()) goto error;
  239. if (NIL_P(errat)){
  240. ruby_set_current_source();
  241. @@ -1422,7 +1452,7 @@ ruby_init()
  242. }
  243. static VALUE
  244. -eval_node(self, node)
  245. +eval_tree(self, node)
  246. VALUE self;
  247. NODE *node;
  248. {
  249. @@ -1571,12 +1601,13 @@ ruby_finalize()
  250. }
  251. int
  252. -ruby_cleanup(ex)
  253. - int ex;
  254. +ruby_cleanup(exArg)
  255. + int exArg;
  256. {
  257. int state;
  258. volatile VALUE errs[2];
  259. - int nerr;
  260. + unsigned nerr;
  261. + volatile int ex = exArg;
  262. errs[1] = ruby_errinfo;
  263. ruby_safe_level = 0;
  264. @@ -1639,7 +1670,7 @@ ruby_exec_internal()
  265. /* default visibility is private at toplevel */
  266. SCOPE_SET(SCOPE_PRIVATE);
  267. if ((state = EXEC_TAG()) == 0) {
  268. - eval_node(ruby_top_self, ruby_eval_tree);
  269. + eval_tree(ruby_top_self, ruby_eval_tree);
  270. }
  271. POP_ITER();
  272. POP_TAG();
  273. @@ -1847,8 +1878,8 @@ rb_eval_cmd(cmd, arg, level)
  274. int level;
  275. {
  276. int state;
  277. - VALUE val = Qnil; /* OK */
  278. - struct SCOPE *saved_scope;
  279. + VALUE val;
  280. + struct SCOPE * volatile saved_scope;
  281. volatile int safe = ruby_safe_level;
  282. if (OBJ_TAINTED(cmd)) {
  283. @@ -1879,9 +1910,7 @@ rb_eval_cmd(cmd, arg, level)
  284. ruby_safe_level = level;
  285. PUSH_TAG(PROT_NONE);
  286. - if ((state = EXEC_TAG()) == 0) {
  287. - val = eval(ruby_top_self, cmd, Qnil, 0, 0);
  288. - }
  289. + val = (state = EXEC_TAG()) ? Qnil : eval(ruby_top_self, cmd, Qnil, 0, 0);
  290. if (ruby_scope->flags & SCOPE_DONT_RECYCLE)
  291. scope_dup(saved_scope);
  292. ruby_scope = saved_scope;
  293. @@ -1920,6 +1949,10 @@ ev_const_defined(cref, id, self)
  294. return rb_const_defined(cref->nd_clss, id);
  295. }
  296. +NOINLINE(static VALUE ev_const_get _((NODE *cref, ID id, VALUE self)));
  297. +NOINLINE(static void eval_cvar_set _((NODE *node, VALUE result, int warn)));
  298. +NOINLINE(static void eval_cdecl _((VALUE self, NODE *node, VALUE value)));
  299. +
  300. static VALUE
  301. ev_const_get(cref, id, self)
  302. NODE *cref;
  303. @@ -2032,7 +2065,7 @@ void
  304. rb_frozen_class_p(klass)
  305. VALUE klass;
  306. {
  307. - char *desc = "something(?!)";
  308. + const char *desc = "something(?!)";
  309. if (OBJ_FROZEN(klass)) {
  310. if (FL_TEST(klass, FL_SINGLETON))
  311. @@ -2070,7 +2103,7 @@ rb_undef(klass, id)
  312. }
  313. body = search_method(klass, id, &origin);
  314. if (!body || !body->nd_body) {
  315. - char *s0 = " class";
  316. + const char *s0 = " class";
  317. VALUE c = klass;
  318. if (FL_TEST(c, FL_SINGLETON)) {
  319. @@ -2161,7 +2194,7 @@ rb_alias(klass, name, def)
  320. VALUE klass;
  321. ID name, def;
  322. {
  323. - VALUE origin;
  324. + VALUE origin = 0;
  325. NODE *orig, *body, *node;
  326. VALUE singleton = 0;
  327. st_data_t data;
  328. @@ -2247,7 +2280,10 @@ rb_copy_node_scope(node, rval)
  329. NODE *node;
  330. NODE *rval;
  331. {
  332. - NODE *copy = NEW_NODE(NODE_SCOPE,0,rval,node->nd_next);
  333. + NODE *copy;
  334. +
  335. + SET_METHOD_SOURCE();
  336. + copy=NEW_NODE(NODE_SCOPE,0,rval,node->nd_next);
  337. if (node->nd_tbl) {
  338. copy->nd_tbl = ALLOC_N(ID, node->nd_tbl[0]+1);
  339. @@ -2322,9 +2358,9 @@ rb_copy_node_scope(node, rval)
  340. #define MATCH_DATA *rb_svar(node->nd_cnt)
  341. -static char* is_defined _((VALUE, NODE*, char*));
  342. +static const char* is_defined _((VALUE, NODE*, char*));
  343. -static char*
  344. +static const char*
  345. arg_defined(self, node, buf, type)
  346. VALUE self;
  347. NODE *node;
  348. @@ -2351,13 +2387,13 @@ arg_defined(self, node, buf, type)
  349. return type;
  350. }
  351. -static char*
  352. +static const char*
  353. is_defined(self, node, buf)
  354. VALUE self;
  355. - NODE *node; /* OK */
  356. + NODE *node;
  357. char *buf;
  358. {
  359. - VALUE val; /* OK */
  360. + VALUE val;
  361. int state;
  362. again:
  363. @@ -2671,27 +2707,27 @@ set_trace_func(obj, trace)
  364. return trace;
  365. }
  366. -static char *
  367. +static const char *
  368. get_event_name(rb_event_t event)
  369. {
  370. switch (event) {
  371. - case RUBY_EVENT_LINE:
  372. + case RUBY_EVENT_LINE:
  373. return "line";
  374. - case RUBY_EVENT_CLASS:
  375. + case RUBY_EVENT_CLASS:
  376. return "class";
  377. - case RUBY_EVENT_END:
  378. + case RUBY_EVENT_END:
  379. return "end";
  380. - case RUBY_EVENT_CALL:
  381. + case RUBY_EVENT_CALL:
  382. return "call";
  383. - case RUBY_EVENT_RETURN:
  384. + case RUBY_EVENT_RETURN:
  385. return "return";
  386. - case RUBY_EVENT_C_CALL:
  387. + case RUBY_EVENT_C_CALL:
  388. return "c-call";
  389. - case RUBY_EVENT_C_RETURN:
  390. + case RUBY_EVENT_C_RETURN:
  391. return "c-return";
  392. - case RUBY_EVENT_RAISE:
  393. + case RUBY_EVENT_RAISE:
  394. return "raise";
  395. - default:
  396. + default:
  397. return "unknown";
  398. }
  399. }
  400. @@ -2702,14 +2738,15 @@ call_trace_func(event, node, self, id, klass)
  401. NODE *node;
  402. VALUE self;
  403. ID id;
  404. - VALUE klass; /* OK */
  405. + VALUE klass;
  406. {
  407. - int state, raised;
  408. + int state;
  409. + volatile int raised;
  410. struct FRAME *prev;
  411. - NODE *node_save;
  412. + NODE * volatile node_save;
  413. VALUE srcfile;
  414. - char *event_name;
  415. - rb_thread_t th = curr_thread;
  416. + const char *event_name;
  417. + volatile rb_thread_t th = curr_thread;
  418. if (!trace_func) return;
  419. if (tracing) return;
  420. @@ -2918,11 +2955,11 @@ class_prefix(self, cpath)
  421. NORETURN(static void return_jump _((VALUE)));
  422. NORETURN(static void break_jump _((VALUE)));
  423. NORETURN(static void next_jump _((VALUE)));
  424. -NORETURN(static void unknown_node _((NODE * volatile)));
  425. +NORETURN(static void unknown_node _((NODE *)));
  426. static void
  427. unknown_node(node)
  428. - NODE *volatile node;
  429. + NODE *node;
  430. {
  431. ruby_current_node = 0;
  432. if (node->flags == 0) {
  433. @@ -2936,40 +2973,886 @@ unknown_node(node)
  434. }
  435. }
  436. -static VALUE
  437. -rb_eval(self, n)
  438. - VALUE self;
  439. - NODE *n;
  440. +/*
  441. + * functions factored out of rb_eval() to reduce its stack frame size
  442. + */
  443. +#define eval_node_0(n,retType, self, node) \
  444. +NOINLINE(static retType TOKEN_PASTE(eval_,n) _((self, node)));\
  445. +static retType TOKEN_PASTE(eval_,n)(self, node)
  446. +
  447. +#define eval_node(n,retType) \
  448. + eval_node_0(n,retType, VALUE self, NODE *node)
  449. +#define eval_node_volatile(n,retType) \
  450. + eval_node_0(n,retType, volatile VALUE self, NODE * volatile node)
  451. +
  452. +eval_node(match2, VALUE)
  453. +{
  454. + VALUE l = rb_eval(self,node->nd_recv);
  455. + VALUE r = rb_eval(self,node->nd_value);
  456. + return rb_reg_match(l, r);
  457. +}
  458. +
  459. +eval_node(match3, VALUE)
  460. {
  461. - NODE * volatile contnode = 0;
  462. - NODE * volatile node = n;
  463. + VALUE r = rb_eval(self,node->nd_recv);
  464. + VALUE l = rb_eval(self,node->nd_value);
  465. + return TYPE(l) == T_STRING ? rb_reg_match(r, l) : rb_funcall(l, match, 1, r);
  466. +}
  467. +
  468. +
  469. +eval_node_volatile(opt_n, void)
  470. +{
  471. + int state;
  472. + PUSH_TAG(PROT_LOOP);
  473. + switch (state = EXEC_TAG()) {
  474. + case TAG_NEXT:
  475. + state = 0;
  476. + case 0:
  477. + while (!NIL_P(rb_gets())) {
  478. + opt_n_redo:
  479. + rb_eval(self, node->nd_body);
  480. + }
  481. + break;
  482. +
  483. + case TAG_REDO:
  484. + state = 0;
  485. + goto opt_n_redo;
  486. +
  487. + case TAG_BREAK:
  488. + state = 0;
  489. + default:
  490. + break;
  491. + }
  492. + POP_TAG();
  493. + if (state) JUMP_TAG(state);
  494. +}
  495. +
  496. +
  497. +eval_node(when, NODE*)
  498. +{
  499. + do {
  500. + NODE *tag = node->nd_head;
  501. + while (tag) {
  502. + EXEC_EVENT_HOOK(RUBY_EVENT_LINE, tag, self,
  503. + ruby_frame->last_func,
  504. + ruby_frame->last_class);
  505. + if (tag->nd_head && nd_type(tag->nd_head) == NODE_WHEN) {
  506. + VALUE v = rb_eval(self, tag->nd_head->nd_head);
  507. + long i;
  508. +
  509. + if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v);
  510. + for (i=0; i<RARRAY(v)->len; i++) {
  511. + if (RTEST(RARRAY(v)->ptr[i])) return node->nd_body;
  512. + }
  513. + tag = tag->nd_next;
  514. + continue;
  515. + }
  516. + if (RTEST(rb_eval(self, tag->nd_head))) return node->nd_body;
  517. + tag = tag->nd_next;
  518. + }
  519. + } while ((node = node->nd_next) && nd_type(node) == NODE_WHEN);
  520. + return node;
  521. +}
  522. +
  523. +
  524. +eval_node(case, NODE*)
  525. +{
  526. + VALUE val = rb_eval(self, node->nd_head);
  527. + node = node->nd_body;
  528. + while (node) {
  529. + NODE *tag;
  530. +
  531. + if (nd_type(node) != NODE_WHEN) break;
  532. + tag = node->nd_head;
  533. + while (tag) {
  534. + EXEC_EVENT_HOOK(RUBY_EVENT_LINE, tag, self,
  535. + ruby_frame->last_func,
  536. + ruby_frame->last_class);
  537. + if (tag->nd_head && nd_type(tag->nd_head) == NODE_WHEN) {
  538. + VALUE v = rb_eval(self, tag->nd_head->nd_head);
  539. + long i;
  540. +
  541. + if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v);
  542. + for (i=0; i<RARRAY(v)->len; i++) {
  543. + if (RTEST(rb_funcall2(RARRAY(v)->ptr[i], eqq, 1, &val)))
  544. + return node->nd_body;
  545. + }
  546. + tag = tag->nd_next;
  547. + continue;
  548. + }
  549. + if (RTEST(rb_funcall2(rb_eval(self, tag->nd_head), eqq, 1, &val)))
  550. + return node->nd_body;
  551. + tag = tag->nd_next;
  552. + }
  553. + node = node->nd_next;
  554. + }
  555. + return node;
  556. +}
  557. +
  558. +
  559. +eval_node_volatile(while, VALUE)
  560. +{
  561. + int state;
  562. + VALUE result;
  563. + PUSH_TAG(PROT_LOOP);
  564. + switch (state = EXEC_TAG()) {
  565. + case 0:
  566. + if (!(node->nd_state) || RTEST(rb_eval(self, node->nd_cond))) {
  567. + do {
  568. + while_redo:
  569. +#if STACK_WIPE_SITES & 0x10
  570. + rb_gc_wipe_stack();
  571. +#endif
  572. + rb_eval(self, node->nd_body);
  573. + while_next:
  574. + ;
  575. + } while (RTEST(rb_eval(self, node->nd_cond)));
  576. + } /* fall thru */
  577. + default:
  578. + result=Qnil;
  579. + break;
  580. +
  581. + case TAG_REDO:
  582. + state = 0;
  583. + goto while_redo;
  584. + case TAG_NEXT:
  585. + state = 0;
  586. + goto while_next;
  587. + case TAG_BREAK:
  588. + if (TAG_DST()) {
  589. + state = 0;
  590. + result = prot_tag->retval;
  591. + }
  592. + }
  593. + POP_TAG();
  594. + if (state) JUMP_TAG(state);
  595. + return result;
  596. +}
  597. +
  598. +
  599. +eval_node_volatile(until, VALUE)
  600. +{
  601. + int state;
  602. + VALUE result;
  603. + PUSH_TAG(PROT_LOOP);
  604. + switch (state = EXEC_TAG()) {
  605. + case 0:
  606. + if (!(node->nd_state) || !RTEST(rb_eval(self, node->nd_cond))) {
  607. + do {
  608. + until_redo:
  609. + #if STACK_WIPE_SITES & 0x10
  610. + rb_gc_wipe_stack();
  611. + #endif
  612. + rb_eval(self, node->nd_body);
  613. + until_next:
  614. + ;
  615. + } while (!RTEST(rb_eval(self, node->nd_cond)));
  616. + } /* fall thru */
  617. + default:
  618. + result=Qnil;
  619. + break;
  620. +
  621. + case TAG_REDO:
  622. + state = 0;
  623. + goto until_redo;
  624. + case TAG_NEXT:
  625. + state = 0;
  626. + goto until_next;
  627. + case TAG_BREAK:
  628. + if (TAG_DST()) {
  629. + state = 0;
  630. + result = prot_tag->retval;
  631. + }
  632. + }
  633. + POP_TAG();
  634. + if (state) JUMP_TAG(state);
  635. + return result;
  636. +}
  637. +
  638. +
  639. +eval_node_volatile(iter, VALUE)
  640. +{
  641. + int state;
  642. + VALUE result;
  643. +
  644. + PUSH_TAG(PROT_LOOP);
  645. + PUSH_BLOCK(node->nd_var, node->nd_body);
  646. +
  647. + state = EXEC_TAG();
  648. + switch (state) {
  649. + case TAG_RETRY:
  650. + state = 0; /* fall thru to case 0 */
  651. + case 0:
  652. + PUSH_ITER(ITER_PRE);
  653. + if (nd_type(node) == NODE_ITER) {
  654. + result = rb_eval(self, node->nd_iter);
  655. + }
  656. + else {
  657. + _block.flags &= ~BLOCK_D_SCOPE;
  658. + BEGIN_CALLARGS;
  659. + result = rb_eval(self, node->nd_iter);
  660. + END_CALLARGS;
  661. + ruby_current_node = (NODE *)node;
  662. + SET_CURRENT_SOURCE();
  663. + result = rb_call(CLASS_OF(result),result,each,0,0,0,self);
  664. + }
  665. + POP_ITER();
  666. + break;
  667. +
  668. + case TAG_BREAK:
  669. + if (TAG_DST()) {
  670. + result = prot_tag->retval;
  671. + state = 0;
  672. + }
  673. + }
  674. + POP_BLOCK();
  675. + POP_TAG();
  676. + if (state) JUMP_TAG(state);
  677. + return result;
  678. +}
  679. +
  680. +
  681. +eval_node_volatile(rescue, VALUE)
  682. +{
  683. + volatile VALUE e_info = ruby_errinfo;
  684. + volatile int rescuing = 0;
  685. int state;
  686. - volatile VALUE result = Qnil;
  687. - st_data_t data;
  688. + VALUE result;
  689. -#define RETURN(v) do { \
  690. - result = (v); \
  691. - goto finish; \
  692. -} while (0)
  693. + PUSH_TAG(PROT_NONE);
  694. + if ((state = EXEC_TAG()) == 0) {
  695. + retry_entry:
  696. + result = rb_eval(self, node->nd_head);
  697. + }
  698. + else if (rescuing) {
  699. + if (rescuing < 0) {
  700. + /* in rescue argument, just reraise */
  701. + result = Qnil;
  702. + }
  703. + else if (state == TAG_RETRY) {
  704. + rescuing = state = 0;
  705. + ruby_errinfo = e_info;
  706. + goto retry_entry;
  707. + }
  708. + else if (state != TAG_RAISE) {
  709. + result = prot_tag->retval;
  710. + }
  711. + }
  712. + else if (state == TAG_RAISE) {
  713. + NODE *resq = node->nd_resq;
  714. +
  715. + rescuing = -1;
  716. + while (resq) {
  717. + ruby_current_node = resq;
  718. + if (handle_rescue(self, resq)) {
  719. + state = 0;
  720. + rescuing = 1;
  721. + result = rb_eval(self, resq->nd_body);
  722. + break;
  723. + }
  724. + resq = resq->nd_head; /* next rescue */
  725. + }
  726. + }
  727. + else {
  728. + result = prot_tag->retval;
  729. + }
  730. + POP_TAG();
  731. + if (state != TAG_RAISE) ruby_errinfo = e_info;
  732. + if (state) {
  733. + JUMP_TAG(state);
  734. + }
  735. + /* no exception raised */
  736. + if (!rescuing && node->nd_else) { /* else clause given */
  737. + result = Qundef; /* caller must eval this! */
  738. + }
  739. + return result;
  740. +}
  741. +
  742. +
  743. +eval_node_volatile(ensure, VALUE)
  744. +{
  745. + int state;
  746. + VALUE result;
  747. +
  748. + PUSH_TAG(PROT_NONE);
  749. + if ((state = EXEC_TAG()) == 0) {
  750. + result = rb_eval(self, node->nd_head);
  751. + }
  752. + POP_TAG();
  753. + if (node->nd_ensr && !thread_no_ensure()) {
  754. + VALUE retval = prot_tag->retval; /* save retval */
  755. + VALUE errinfo = ruby_errinfo;
  756. +
  757. + rb_eval(self, node->nd_ensr);
  758. + return_value(retval);
  759. + ruby_errinfo = errinfo;
  760. + }
  761. + if (state) JUMP_TAG(state);
  762. + return result;
  763. +}
  764. +
  765. +
  766. +eval_node(dot, VALUE)
  767. +{
  768. + VALUE beg = rb_eval(self, node->nd_beg);
  769. + VALUE end = rb_eval(self, node->nd_end);
  770. + return rb_range_new(beg, end, nd_type(node) == NODE_DOT3);
  771. +}
  772. +
  773. +
  774. +eval_node(flip2, VALUE)
  775. +{
  776. + VALUE *flip = rb_svar(node->nd_cnt);
  777. + if (!flip) rb_bug("unexpected local variable");
  778. + if (!RTEST(*flip)) {
  779. + if (!RTEST(rb_eval(self, node->nd_beg)))
  780. + return Qfalse;
  781. + *flip = RTEST(rb_eval(self, node->nd_end))?Qfalse:Qtrue;
  782. + }
  783. + else if (RTEST(rb_eval(self, node->nd_end)))
  784. + *flip = Qfalse;
  785. + return Qtrue;
  786. +}
  787. +
  788. +
  789. +eval_node(flip3, VALUE)
  790. +{
  791. + VALUE *flip = rb_svar(node->nd_cnt);
  792. + if (!flip) rb_bug("unexpected local variable");
  793. + if (!RTEST(*flip))
  794. + return *flip = (RTEST(rb_eval(self, node->nd_beg)) ? Qtrue : Qfalse);
  795. + if (RTEST(rb_eval(self, node->nd_end)))
  796. + *flip = Qfalse;
  797. + return Qtrue;
  798. +}
  799. +
  800. +
  801. +eval_node(attrasgn, VALUE)
  802. +{
  803. + VALUE recv;
  804. + int argc; VALUE *argv; /* used in SETUP_ARGS */
  805. + int scope;
  806. + TMP_PROTECT;
  807. +
  808. + BEGIN_CALLARGS;
  809. + if (node->nd_recv == (NODE *)1) {
  810. + recv = self;
  811. + scope = 1;
  812. + }
  813. + else {
  814. + recv = rb_eval(self, node->nd_recv);
  815. + scope = 0;
  816. + }
  817. + SETUP_ARGS(node->nd_args);
  818. + END_CALLARGS;
  819. +
  820. + ruby_current_node = node;
  821. + SET_CURRENT_SOURCE();
  822. + rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,scope,self);
  823. + return argv[argc-1];
  824. +}
  825. +
  826. +
  827. +eval_node(call, VALUE)
  828. +{
  829. + VALUE recv;
  830. + int argc; VALUE *argv; /* used in SETUP_ARGS */
  831. + TMP_PROTECT;
  832. +
  833. + BEGIN_CALLARGS;
  834. + recv = rb_eval(self, node->nd_recv);
  835. + SETUP_ARGS(node->nd_args);
  836. + END_CALLARGS;
  837. +
  838. + ruby_current_node = node;
  839. + SET_CURRENT_SOURCE();
  840. + return rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,0,self);
  841. +}
  842. +
  843. +
  844. +eval_node(fcall, VALUE)
  845. +{
  846. + int argc; VALUE *argv; /* used in SETUP_ARGS */
  847. + TMP_PROTECT;
  848. +
  849. + BEGIN_CALLARGS;
  850. + SETUP_ARGS(node->nd_args);
  851. + END_CALLARGS;
  852. +
  853. + ruby_current_node = node;
  854. + SET_CURRENT_SOURCE();
  855. + return rb_call(CLASS_OF(self),self,node->nd_mid,argc,argv,1,self);
  856. +}
  857. +
  858. +
  859. +eval_node(super, VALUE)
  860. +{
  861. + int argc; VALUE *argv; /* used in SETUP_ARGS */
  862. + TMP_PROTECT;
  863. +
  864. + if (ruby_frame->last_class == 0) {
  865. + if (ruby_frame->last_func) {
  866. + rb_name_error(ruby_frame->last_func,
  867. + "superclass method `%s' disabled",
  868. + rb_id2name(ruby_frame->orig_func));
  869. + }
  870. + else {
  871. + rb_raise(rb_eNoMethodError, "super called outside of method");
  872. + }
  873. + }
  874. + if (nd_type(node) == NODE_ZSUPER) {
  875. + argc = ruby_frame->argc;
  876. + if (argc && DMETHOD_P()) {
  877. + if (TYPE(RBASIC(ruby_scope)->klass) != T_ARRAY ||
  878. + RARRAY(RBASIC(ruby_scope)->klass)->len != argc) {
  879. + rb_raise(rb_eRuntimeError,
  880. + "super: specify arguments explicitly");
  881. + }
  882. + argv = RARRAY(RBASIC(ruby_scope)->klass)->ptr;
  883. + }
  884. + else if (!ruby_scope->local_vars) {
  885. + argc = 0;
  886. + argv = 0;
  887. + }
  888. + else {
  889. + argv = ruby_scope->local_vars + 2;
  890. + }
  891. + }
  892. + else {
  893. + BEGIN_CALLARGS;
  894. + SETUP_ARGS(node->nd_args);
  895. + END_CALLARGS;
  896. + ruby_current_node = node;
  897. + }
  898. +
  899. + SET_CURRENT_SOURCE();
  900. + return rb_call_super(argc, argv);
  901. +}
  902. +
  903. +
  904. +eval_node_volatile(scope, VALUE)
  905. +{
  906. + int state;
  907. + VALUE result;
  908. + struct FRAME frame;
  909. + NODE * volatile saved_cref = 0;
  910. +
  911. + frame = *ruby_frame;
  912. + frame.tmp = ruby_frame;
  913. + ruby_frame = &frame;
  914. +
  915. + PUSH_SCOPE();
  916. + PUSH_TAG(PROT_NONE);
  917. + if (node->nd_rval) {
  918. + saved_cref = ruby_cref;
  919. + ruby_cref = (NODE*)node->nd_rval;
  920. + }
  921. + if (node->nd_tbl) {
  922. + VALUE *vars = ALLOCA_N(VALUE, node->nd_tbl[0]+1);
  923. + *vars++ = (VALUE)node;
  924. + ruby_scope->local_vars = vars;
  925. + rb_mem_clear(ruby_scope->local_vars, node->nd_tbl[0]);
  926. + ruby_scope->local_tbl = node->nd_tbl;
  927. + }
  928. + else {
  929. + ruby_scope->local_vars = 0;
  930. + ruby_scope->local_tbl = 0;
  931. + }
  932. + if ((state = EXEC_TAG()) == 0) {
  933. + result = rb_eval(self, node->nd_next);
  934. + }
  935. + POP_TAG();
  936. + POP_SCOPE();
  937. + ruby_frame = frame.tmp;
  938. + if (saved_cref)
  939. + ruby_cref = saved_cref;
  940. + if (state) JUMP_TAG(state);
  941. + return result;
  942. +}
  943. +
  944. +eval_node(op_asgn1, VALUE)
  945. +{
  946. + int argc; VALUE *argv; /* used in SETUP_ARGS */
  947. + VALUE recv, val, tmp;
  948. + NODE *rval;
  949. + TMP_PROTECT;
  950. +
  951. + recv = rb_eval(self, node->nd_recv);
  952. + rval = node->nd_args->nd_head;
  953. + SETUP_ARGS0(node->nd_args->nd_body, 1);
  954. + val = rb_funcall3(recv, aref, argc, argv);
  955. + switch (node->nd_mid) {
  956. + case 0: /* OR */
  957. + if (RTEST(val)) return val;
  958. + val = rb_eval(self, rval);
  959. + break;
  960. + case 1: /* AND */
  961. + if (!RTEST(val)) return val;
  962. + val = rb_eval(self, rval);
  963. + break;
  964. + default:
  965. + tmp = rb_eval(self, rval);
  966. + val = rb_funcall3(val, node->nd_mid, 1, &tmp);
  967. + }
  968. + argv[argc] = val;
  969. + rb_funcall2(recv, aset, argc+1, argv);
  970. + return val;
  971. +}
  972. +
  973. +
  974. +eval_node(op_asgn2, VALUE)
  975. +{
  976. + ID id = node->nd_next->nd_vid;
  977. + VALUE recv, val, tmp;
  978. +
  979. + recv = rb_eval(self, node->nd_recv);
  980. + val = rb_funcall3(recv, id, 0, 0);
  981. + switch (node->nd_next->nd_mid) {
  982. + case 0: /* OR */
  983. + if (RTEST(val)) return val;
  984. + val = rb_eval(self, node->nd_value);
  985. + break;
  986. + case 1: /* AND */
  987. + if (!RTEST(val)) return val;
  988. + val = rb_eval(self, node->nd_value);
  989. + break;
  990. + default:
  991. + tmp = rb_eval(self, node->nd_value);
  992. + val = rb_funcall3(val, node->nd_next->nd_mid, 1, &tmp);
  993. + }
  994. +
  995. + rb_funcall2(recv, node->nd_next->nd_aid, 1, &val);
  996. + return val;
  997. +}
  998. +
  999. +
  1000. +eval_node(hash, VALUE)
  1001. +{
  1002. + NODE *list;
  1003. + VALUE hash = rb_hash_new();
  1004. + VALUE key, val;
  1005. +
  1006. + list = node->nd_head;
  1007. + while (list) {
  1008. + key = rb_eval(self, list->nd_head);
  1009. + list = list->nd_next;
  1010. + if (list == 0)
  1011. + rb_bug("odd number list for Hash");
  1012. + val = rb_eval(self, list->nd_head);
  1013. + list = list->nd_next;
  1014. + rb_hash_aset(hash, key, val);
  1015. + }
  1016. + return hash;
  1017. +}
  1018. +
  1019. +
  1020. +eval_node(array, VALUE)
  1021. +{
  1022. + VALUE ary;
  1023. + long i;
  1024. +
  1025. + i = node->nd_alen;
  1026. + ary = rb_ary_new2(i);
  1027. + for (i=0;node;node=node->nd_next) {
  1028. + RARRAY(ary)->ptr[i++] = rb_eval(self, node->nd_head);
  1029. + RARRAY(ary)->len = i;
  1030. + }
  1031. + return ary;
  1032. +}
  1033. +
  1034. +
  1035. +eval_node(slit, VALUE)
  1036. +{
  1037. + VALUE str, str2;
  1038. + NODE *list = node->nd_next;
  1039. +
  1040. + str = rb_str_new3(node->nd_lit);
  1041. + while (list) {
  1042. + if (list->nd_head) {
  1043. + switch (nd_type(list->nd_head)) {
  1044. + case NODE_STR:
  1045. + str2 = list->nd_head->nd_lit;
  1046. + break;
  1047. + default:
  1048. + str2 = rb_eval(self, list->nd_head);
  1049. + break;
  1050. + }
  1051. + rb_str_append(str, str2);
  1052. + OBJ_INFECT(str, str2);
  1053. + }
  1054. + list = list->nd_next;
  1055. + }
  1056. + switch (nd_type(node)) {
  1057. + case NODE_DREGX:
  1058. + str2 = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
  1059. + node->nd_cflag);
  1060. + RB_GC_GUARD(str); /* prevent tail call optimization here */
  1061. + return str2;
  1062. + case NODE_DREGX_ONCE: /* regexp expand once */
  1063. + str2 = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
  1064. + node->nd_cflag);
  1065. + nd_set_type(node, NODE_LIT);
  1066. + RB_GC_GUARD(str); /* ensure str is not GC'd in rb_reg_new */
  1067. + return node->nd_lit = str2;
  1068. + case NODE_LIT:
  1069. + /* other thread may replace NODE_DREGX_ONCE to NODE_LIT */
  1070. + return Qundef;
  1071. + case NODE_DXSTR:
  1072. + return rb_funcall(self, '`', 1, str);
  1073. + case NODE_DSYM:
  1074. + return rb_str_intern(str);
  1075. + }
  1076. + return str;
  1077. +}
  1078. +
  1079. +
  1080. +eval_node(defn, void)
  1081. +{
  1082. + NODE *body, *defn;
  1083. + VALUE origin = 0;
  1084. + int noex;
  1085. +
  1086. + if (NIL_P(ruby_class)) {
  1087. + rb_raise(rb_eTypeError, "no class/module to add method");
  1088. + }
  1089. + if (ruby_class == rb_cObject && node->nd_mid == init) {
  1090. + rb_warn("redefining Object#initialize may cause infinite loop");
  1091. + }
  1092. + if (node->nd_mid == __id__ || node->nd_mid == __send__) {
  1093. + rb_warn("redefining `%s' may cause serious problem",
  1094. + rb_id2name(node->nd_mid));
  1095. + }
  1096. + rb_frozen_class_p(ruby_class);
  1097. + body = search_method(ruby_class, node->nd_mid, &origin);
  1098. + if (body){
  1099. + if (RTEST(ruby_verbose) && ruby_class == origin && body->nd_cnt == 0 && body->nd_body) {
  1100. + rb_warning("method redefined; discarding old %s", rb_id2name(node->nd_mid));
  1101. + }
  1102. + }
  1103. +
  1104. + if (SCOPE_TEST(SCOPE_PRIVATE) || node->nd_mid == init) {
  1105. + noex = NOEX_PRIVATE;
  1106. + }
  1107. + else if (SCOPE_TEST(SCOPE_PROTECTED)) {
  1108. + noex = NOEX_PROTECTED;
  1109. + }
  1110. + else {
  1111. + noex = NOEX_PUBLIC;
  1112. + }
  1113. + if (body && origin == ruby_class && body->nd_body == 0) {
  1114. + noex |= NOEX_NOSUPER;
  1115. + }
  1116. +
  1117. + defn = rb_copy_node_scope(node->nd_defn, ruby_cref);
  1118. + rb_add_method(ruby_class, node->nd_mid, defn, noex);
  1119. + if (scope_vmode == SCOPE_MODFUNC) {
  1120. + rb_add_method(rb_singleton_class(ruby_class),
  1121. + node->nd_mid, defn, NOEX_PUBLIC);
  1122. + }
  1123. +}
  1124. +
  1125. +
  1126. +eval_node(defs, void)
  1127. +{
  1128. + VALUE recv = rb_eval(self, node->nd_recv);
  1129. + VALUE klass;
  1130. + NODE *body = 0, *defn;
  1131. + st_data_t data;
  1132. +
  1133. + if (ruby_safe_level >= 4 && !OBJ_TAINTED(recv)) {
  1134. + rb_raise(rb_eSecurityError, "Insecure: can't define singleton method");
  1135. + }
  1136. + if (FIXNUM_P(recv) || SYMBOL_P(recv)) {
  1137. + rb_raise(rb_eTypeError,
  1138. + "can't define singleton method \"%s\" for %s",
  1139. + rb_id2name(node->nd_mid),
  1140. + rb_obj_classname(recv));
  1141. + }
  1142. +
  1143. + if (OBJ_FROZEN(recv)) rb_error_frozen("object");
  1144. + klass = rb_singleton_class(recv);
  1145. + if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, &data)) {
  1146. + body = (NODE *)data;
  1147. + if (ruby_safe_level >= 4) {
  1148. + rb_raise(rb_eSecurityError, "redefining method prohibited");
  1149. + }
  1150. + if (RTEST(ruby_verbose)) {
  1151. + rb_warning("redefine %s", rb_id2name(node->nd_mid));
  1152. + }
  1153. + }
  1154. + defn = rb_copy_node_scope(node->nd_defn, ruby_cref);
  1155. + rb_add_method(klass, node->nd_mid, defn,
  1156. + NOEX_PUBLIC|(body?body->nd_noex&NOEX_UNDEF:0));
  1157. +}
  1158. +
  1159. +
  1160. +eval_node(class, VALUE)
  1161. +{
  1162. + VALUE super, klass, tmp, cbase;
  1163. + ID cname;
  1164. + int gen = Qfalse;
  1165. +
  1166. + cbase = class_prefix(self, node->nd_cpath);
  1167. + cname = node->nd_cpath->nd_mid;
  1168. +
  1169. + if (NIL_P(ruby_cbase)) {
  1170. + rb_raise(rb_eTypeError, "no outer class/module");
  1171. + }
  1172. + if (node->nd_super) {
  1173. + super = rb_eval(self, node->nd_super);
  1174. + rb_check_inheritable(super);
  1175. + }
  1176. + else {
  1177. + super = 0;
  1178. + }
  1179. +
  1180. + if (rb_const_defined_at(cbase, cname)) {
  1181. + klass = rb_const_get_at(cbase, cname);
  1182. + if (TYPE(klass) != T_CLASS) {
  1183. + rb_raise(rb_eTypeError, "%s is not a class",
  1184. + rb_id2name(cname));
  1185. + }
  1186. + if (super) {
  1187. + tmp = rb_class_real(RCLASS(klass)->super);
  1188. + if (tmp != super) {
  1189. + rb_raise(rb_eTypeError, "superclass mismatch for class %s",
  1190. + rb_id2name(cname));
  1191. + }
  1192. + super = 0;
  1193. + }
  1194. + if (ruby_safe_level >= 4) {
  1195. + rb_raise(rb_eSecurityError, "extending class prohibited");
  1196. + }
  1197. + }
  1198. + else {
  1199. + if (!super) super = rb_cObject;
  1200. + klass = rb_define_class_id(cname, super);
  1201. + rb_set_class_path(klass, cbase, rb_id2name(cname));
  1202. + rb_const_set(cbase, cname, klass);
  1203. + gen = Qtrue;
  1204. + }
  1205. + if (ruby_wrapper) {
  1206. + rb_extend_object(klass, ruby_wrapper);
  1207. + rb_include_module(klass, ruby_wrapper);
  1208. + }
  1209. + if (super && gen) {
  1210. + rb_class_inherited(super, klass);
  1211. + }
  1212. + return module_setup(klass, node);
  1213. +}
  1214. +
  1215. +
  1216. +eval_node(module, VALUE)
  1217. +{
  1218. + VALUE module, cbase;
  1219. + ID cname;
  1220. +
  1221. + if (NIL_P(ruby_cbase)) {
  1222. + rb_raise(rb_eTypeError, "no outer class/module");
  1223. + }
  1224. + cbase = class_prefix(self, node->nd_cpath);
  1225. + cname = node->nd_cpath->nd_mid;
  1226. + if (rb_const_defined_at(cbase, cname)) {
  1227. + module = rb_const_get_at(cbase, cname);
  1228. + if (TYPE(module) != T_MODULE) {
  1229. + rb_raise(rb_eTypeError, "%s is not a module",
  1230. + rb_id2name(cname));
  1231. + }
  1232. + if (ruby_safe_level >= 4) {
  1233. + rb_raise(rb_eSecurityError, "extending module prohibited");
  1234. + }
  1235. + }
  1236. + else {
  1237. + module = rb_define_module_id(cname);
  1238. + rb_set_class_path(module, cbase, rb_id2name(cname));
  1239. + rb_const_set(cbase, cname, module);
  1240. + }
  1241. + if (ruby_wrapper) {
  1242. + rb_extend_object(module, ruby_wrapper);
  1243. + rb_include_module(module, ruby_wrapper);
  1244. + }
  1245. +
  1246. + return module_setup(module, node);
  1247. +}
  1248. +
  1249. +
  1250. +eval_node(sclass, VALUE)
  1251. +{
  1252. + VALUE klass, result;
  1253. +
  1254. + result = rb_eval(self, node->nd_recv);
  1255. + if (FIXNUM_P(result) || SYMBOL_P(result)) {
  1256. + rb_raise(rb_eTypeError, "no virtual class for %s",
  1257. + rb_obj_classname(result));
  1258. + }
  1259. + if (ruby_safe_level >= 4 && !OBJ_TAINTED(result))
  1260. + rb_raise(rb_eSecurityError, "Insecure: can't extend object");
  1261. + klass = rb_singleton_class(result);
  1262. +
  1263. + if (ruby_wrapper) {
  1264. + rb_extend_object(klass, ruby_wrapper);
  1265. + rb_include_module(klass, ruby_wrapper);
  1266. + }
  1267. +
  1268. + return module_setup(klass, node);
  1269. +}
  1270. +
  1271. +
  1272. +eval_node(defined, VALUE)
  1273. +{
  1274. + char buf[20];
  1275. + const char *desc = is_defined(self, node->nd_head, buf);
  1276. + return desc ? rb_str_new2(desc) : Qnil;
  1277. +}
  1278. +
  1279. +
  1280. +static void
  1281. +eval_cvar_set(node, result, warn)
  1282. + NODE *node;
  1283. + VALUE result;
  1284. + int warn;
  1285. +{
  1286. + rb_cvar_set(cvar_cbase(), node->nd_vid, result, warn);
  1287. +}
  1288. +
  1289. +
  1290. +static void
  1291. +eval_cdecl(self, node, result)
  1292. + VALUE self, result;
  1293. + NODE *node;
  1294. +{
  1295. + if (node->nd_vid == 0)
  1296. + rb_const_set(class_prefix(self, node->nd_else),
  1297. + node->nd_else->nd_mid, result);
  1298. + else
  1299. + rb_const_set(ruby_cbase, node->nd_vid, result);
  1300. +}
  1301. - again:
  1302. - if (!node) RETURN(Qnil);
  1303. +static VALUE
  1304. +rb_eval(self, node)
  1305. + VALUE self;
  1306. + NODE * node;
  1307. +{
  1308. + VALUE result;
  1309. +
  1310. +again:
  1311. + CHECK_INTS;
  1312. + result = Qnil;
  1313. + if (node) {
  1314. ruby_current_node = node;
  1315. switch (nd_type(node)) {
  1316. case NODE_BLOCK:
  1317. - if (contnode) {
  1318. - result = rb_eval(self, node);
  1319. - break;
  1320. + while (node->nd_next) {
  1321. + rb_eval(self, node->nd_head);
  1322. + node = node->nd_next;
  1323. }
  1324. - contnode = node->nd_next;
  1325. node = node->nd_head;
  1326. goto again;
  1327. case NODE_POSTEXE:
  1328. rb_f_END();
  1329. nd_set_type(node, NODE_NIL); /* exec just once */
  1330. - result = Qnil;
  1331. break;
  1332. /* begin .. end without clauses */
  1333. @@ -2984,223 +3867,57 @@ rb_eval(self, n)
  1334. /* nodes for speed-up(literal match) */
  1335. case NODE_MATCH2:
  1336. - {
  1337. - VALUE l = rb_eval(self,node->nd_recv);
  1338. - VALUE r = rb_eval(self,node->nd_value);
  1339. - result = rb_reg_match(l, r);
  1340. - }
  1341. + result = eval_match2(self, node);
  1342. break;
  1343. /* nodes for speed-up(literal match) */
  1344. case NODE_MATCH3:
  1345. - {
  1346. - VALUE r = rb_eval(self,node->nd_recv);
  1347. - VALUE l = rb_eval(self,node->nd_value);
  1348. - if (TYPE(l) == T_STRING) {
  1349. - result = rb_reg_match(r, l);
  1350. - }
  1351. - else {
  1352. - result = rb_funcall(l, match, 1, r);
  1353. - }
  1354. - }
  1355. + result = eval_match3(self,node);
  1356. break;
  1357. /* node for speed-up(top-level loop for -n/-p) */
  1358. case NODE_OPT_N:
  1359. - PUSH_TAG(PROT_LOOP);
  1360. - switch (state = EXEC_TAG()) {
  1361. - case 0:
  1362. - opt_n_next:
  1363. - while (!NIL_P(rb_gets())) {
  1364. - opt_n_redo:
  1365. - rb_eval(self, node->nd_body);
  1366. - }
  1367. - break;
  1368. -
  1369. - case TAG_REDO:
  1370. - state = 0;
  1371. - goto opt_n_redo;
  1372. - case TAG_NEXT:
  1373. - state = 0;
  1374. - goto opt_n_next;
  1375. - case TAG_BREAK:
  1376. - state = 0;
  1377. - default:
  1378. - break;
  1379. - }
  1380. - POP_TAG();
  1381. - if (state) JUMP_TAG(state);
  1382. - RETURN(Qnil);
  1383. + eval_opt_n(self, node);
  1384. + break;
  1385. case NODE_SELF:
  1386. - RETURN(self);
  1387. + result = self;
  1388. + break;
  1389. case NODE_NIL:
  1390. - RETURN(Qnil);
  1391. + break;
  1392. case NODE_TRUE:
  1393. - RETURN(Qtrue);
  1394. + result = Qtrue;
  1395. + break;
  1396. case NODE_FALSE:
  1397. - RETURN(Qfalse);
  1398. + result = Qfalse;
  1399. + break;
  1400. case NODE_IF:
  1401. EXEC_EVENT_HOOK(RUBY_EVENT_LINE, node, self,
  1402. ruby_frame->last_func,
  1403. ruby_frame->last_class);
  1404. - if (RTEST(rb_eval(self, node->nd_cond))) {
  1405. - node = node->nd_body;
  1406. - }
  1407. - else {
  1408. - node = node->nd_else;
  1409. - }
  1410. + node = RTEST(rb_eval(self, node->nd_cond)) ?
  1411. + node->nd_body : node->nd_else;
  1412. goto again;
  1413. case NODE_WHEN:
  1414. - while (node) {
  1415. - NODE *tag;
  1416. -
  1417. - if (nd_type(node) != NODE_WHEN) goto again;
  1418. - tag = node->nd_head;
  1419. - while (tag) {
  1420. - EXEC_EVENT_HOOK(RUBY_EVENT_LINE, tag, self,
  1421. - ruby_frame->last_func,
  1422. - ruby_frame->last_class);
  1423. - if (tag->nd_head && nd_type(tag->nd_head) == NODE_WHEN) {
  1424. - VALUE v = rb_eval(self, tag->nd_head->nd_head);
  1425. - long i;
  1426. -
  1427. - if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v);
  1428. - for (i=0; i<RARRAY(v)->len; i++) {
  1429. - if (RTEST(RARRAY(v)->ptr[i])) {
  1430. - node = node->nd_body;
  1431. - goto again;
  1432. - }
  1433. - }
  1434. - tag = tag->nd_next;
  1435. - continue;
  1436. - }
  1437. - if (RTEST(rb_eval(self, tag->nd_head))) {
  1438. - node = node->nd_body;
  1439. - goto again;
  1440. - }
  1441. - tag = tag->nd_next;
  1442. - }
  1443. - node = node->nd_next;
  1444. - }
  1445. - RETURN(Qnil);
  1446. + if (node = eval_when(self, node)) goto again;
  1447. + break;
  1448. case NODE_CASE:
  1449. - {
  1450. - VALUE val;
  1451. -
  1452. - val = rb_eval(self, node->nd_head);
  1453. - node = node->nd_body;
  1454. - while (node) {
  1455. - NODE *tag;
  1456. -
  1457. - if (nd_type(node) != NODE_WHEN) {
  1458. - goto again;
  1459. - }
  1460. - tag = node->nd_head;
  1461. - while (tag) {
  1462. - EXEC_EVENT_HOOK(RUBY_EVENT_LINE, tag, self,
  1463. - ruby_frame->last_func,
  1464. - ruby_frame->last_class);
  1465. - if (tag->nd_head && nd_type(tag->nd_head) == NODE_WHEN) {
  1466. - VALUE v = rb_eval(self, tag->nd_head->nd_head);
  1467. - long i;
  1468. -
  1469. - if (TYPE(v) != T_ARRAY) v = rb_ary_to_ary(v);
  1470. - for (i=0; i<RARRAY(v)->len; i++) {
  1471. - if (RTEST(rb_funcall2(RARRAY(v)->ptr[i], eqq, 1, &val))){
  1472. - node = node->nd_body;
  1473. - goto again;
  1474. - }
  1475. - }
  1476. - tag = tag->nd_next;
  1477. - continue;
  1478. - }
  1479. - if (RTEST(rb_funcall2(rb_eval(self, tag->nd_head), eqq, 1, &val))) {
  1480. - node = node->nd_body;
  1481. - goto again;
  1482. - }
  1483. - tag = tag->nd_next;
  1484. - }
  1485. - node = node->nd_next;
  1486. - }
  1487. - }
  1488. - RETURN(Qnil);
  1489. + if (node = eval_case(self, node)) goto again;
  1490. + break;
  1491. case NODE_WHILE:
  1492. - PUSH_TAG(PROT_LOOP);
  1493. - result = Qnil;
  1494. - switch (state = EXEC_TAG()) {
  1495. - case 0:
  1496. - if (node->nd_state && !RTEST(rb_eval(self, node->nd_cond)))
  1497. - goto while_out;
  1498. - do {
  1499. - while_redo:
  1500. - rb_eval(self, node->nd_body);
  1501. - while_next:
  1502. - ;
  1503. - } while (RTEST(rb_eval(self, node->nd_cond)));
  1504. - break;
  1505. -
  1506. - case TAG_REDO:
  1507. - state = 0;
  1508. - goto while_redo;
  1509. - case TAG_NEXT:
  1510. - state = 0;
  1511. - goto while_next;
  1512. - case TAG_BREAK:
  1513. - if (TAG_DST()) {
  1514. - state = 0;
  1515. - result = prot_tag->retval;
  1516. - }
  1517. - /* fall through */
  1518. - default:
  1519. - break;
  1520. - }
  1521. - while_out:
  1522. - POP_TAG();
  1523. - if (state) JUMP_TAG(state);
  1524. - RETURN(result);
  1525. + result = eval_while(self,node);
  1526. + break;
  1527. case NODE_UNTIL:
  1528. - PUSH_TAG(PROT_LOOP);
  1529. - result = Qnil;
  1530. - switch (state = EXEC_TAG()) {
  1531. - case 0:
  1532. - if (node->nd_state && RTEST(rb_eval(self, node->nd_cond)))
  1533. - goto until_out;
  1534. - do {
  1535. - until_redo:
  1536. - rb_eval(self, node->nd_body);
  1537. - until_next:
  1538. - ;
  1539. - } while (!RTEST(rb_eval(self, node->nd_cond)));
  1540. - break;
  1541. -
  1542. - case TAG_REDO:
  1543. - state = 0;
  1544. - goto until_redo;
  1545. - case TAG_NEXT:
  1546. - state = 0;
  1547. - goto until_next;
  1548. - case TAG_BREAK:
  1549. - if (TAG_DST()) {
  1550. - state = 0;
  1551. - result = prot_tag->retval;
  1552. - }
  1553. - /* fall through */
  1554. - default:
  1555. - break;
  1556. - }
  1557. - until_out:
  1558. - POP_TAG();
  1559. - if (state) JUMP_TAG(state);
  1560. - RETURN(result);
  1561. + result = eval_until(self,node);
  1562. + break;
  1563. case NODE_BLOCK_PASS:
  1564. result = block_pass(self, node);
  1565. @@ -3208,47 +3925,7 @@ rb_eval(self, n)
  1566. case NODE_ITER:
  1567. case NODE_FOR:
  1568. - {
  1569. - PUSH_TAG(PROT_LOOP);
  1570. - PUSH_BLOCK(node->nd_var, node->nd_body);
  1571. -
  1572. - state = EXEC_TAG();
  1573. - if (state == 0) {
  1574. - iter_retry:
  1575. - PUSH_ITER(ITER_PRE);
  1576. - if (nd_type(node) == NODE_ITER) {
  1577. - result = rb_eval(self, node->nd_iter);
  1578. - }
  1579. - else {
  1580. - VALUE recv;
  1581. -
  1582. - _block.flags &= ~BLOCK_D_SCOPE;
  1583. - BEGIN_CALLARGS;
  1584. - recv = rb_eval(self, node->nd_iter);
  1585. - END_CALLARGS;
  1586. - ruby_current_node = node;
  1587. - SET_CURRENT_SOURCE();
  1588. - result = rb_call(CLASS_OF(recv),recv,each,0,0,0,self);
  1589. - }
  1590. - POP_ITER();
  1591. - }
  1592. - else if (state == TAG_BREAK && TAG_DST()) {
  1593. - result = prot_tag->retval;
  1594. - state = 0;
  1595. - }
  1596. - else if (state == TAG_RETRY) {
  1597. - state = 0;
  1598. - goto iter_retry;
  1599. - }
  1600. - POP_BLOCK();
  1601. - POP_TAG();
  1602. - switch (state) {
  1603. - case 0:
  1604. - break;
  1605. - default:
  1606. - JUMP_TAG(state);
  1607. - }
  1608. - }
  1609. + result = eval_iter(self, node);
  1610. break;
  1611. case NODE_BREAK:
  1612. @@ -3256,17 +3933,14 @@ rb_eval(self, n)
  1613. break;
  1614. case NODE_NEXT:
  1615. - CHECK_INTS;
  1616. next_jump(rb_eval(self, node->nd_stts));
  1617. break;
  1618. case NODE_REDO:
  1619. - CHECK_INTS;
  1620. JUMP_TAG(TAG_REDO);
  1621. break;
  1622. case NODE_RETRY:
  1623. - CHECK_INTS;
  1624. JUMP_TAG(TAG_RETRY);
  1625. break;
  1626. @@ -3296,73 +3970,15 @@ rb_eval(self, n)
  1627. break;
  1628. case NODE_RESCUE:
  1629. - {
  1630. - volatile VALUE e_info = ruby_errinfo;
  1631. - volatile int rescuing = 0;
  1632. -
  1633. - PUSH_TAG(PROT_NONE);
  1634. - if ((state = EXEC_TAG()) == 0) {
  1635. - retry_entry:
  1636. - result = rb_eval(self, node->nd_head);
  1637. - }
  1638. - else if (rescuing) {
  1639. - if (rescuing < 0) {
  1640. - /* in rescue argument, just reraise */
  1641. - }
  1642. - else if (state == TAG_RETRY) {
  1643. - rescuing = state = 0;
  1644. - ruby_errinfo = e_info;
  1645. - goto retry_entry;
  1646. - }
  1647. - else if (state != TAG_RAISE) {
  1648. - result = prot_tag->retval;
  1649. - }
  1650. - }
  1651. - else if (state == TAG_RAISE) {
  1652. - NODE *resq = node->nd_resq;
  1653. -
  1654. - rescuing = -1;
  1655. - while (resq) {
  1656. - ruby_current_node = resq;
  1657. - if (handle_rescue(self, resq)) {
  1658. - state = 0;
  1659. - rescuing = 1;
  1660. - result = rb_eval(self, resq->nd_body);
  1661. - break;
  1662. - }
  1663. - resq = resq->nd_head; /* next rescue */
  1664. - }
  1665. - }
  1666. - else {
  1667. - result = prot_tag->retval;
  1668. - }
  1669. - POP_TAG();
  1670. - if (state != TAG_RAISE) ruby_errinfo = e_info;
  1671. - if (state) {
  1672. - JUMP_TAG(state);
  1673. - }
  1674. - /* no exception raised */
  1675. - if (!rescuing && (node = node->nd_else)) { /* else clause given */
  1676. - goto again;
  1677. - }
  1678. - }
  1679. + result = eval_rescue(self,node);
  1680. + if (result == Qundef) { /* handle else clause w/o recursion */
  1681. + node = node->nd_else;
  1682. + goto again;
  1683. + }
  1684. break;
  1685. case NODE_ENSURE:
  1686. - PUSH_TAG(PROT_NONE);
  1687. - if ((state = EXEC_TAG()) == 0) {
  1688. - result = rb_eval(self, node->nd_head);
  1689. - }
  1690. - POP_TAG();
  1691. - if (node->nd_ensr && !thread_no_ensure()) {
  1692. - VALUE retval = prot_tag->retval; /* save retval */
  1693. - VALUE errinfo = ruby_errinfo;
  1694. -
  1695. - rb_eval(self, node->nd_ensr);
  1696. - return_value(retval);
  1697. - ruby_errinfo = errinfo;
  1698. - }
  1699. - if (state) JUMP_TAG(state);
  1700. + result = eval_ensure(self,node);
  1701. break;
  1702. case NODE_AND:
  1703. @@ -3378,56 +3994,20 @@ rb_eval(self, n)
  1704. goto again;
  1705. case NODE_NOT:
  1706. - if (RTEST(rb_eval(self, node->nd_body))) result = Qfalse;
  1707. - else result = Qtrue;
  1708. + result = RTEST(rb_eval(self, node->nd_body)) ? Qfalse : Qtrue;
  1709. break;
  1710. case NODE_DOT2:
  1711. case NODE_DOT3:
  1712. - {
  1713. - VALUE beg = rb_eval(self, node->nd_beg);
  1714. - VALUE end = rb_eval(self, node->nd_end);
  1715. - result = rb_range_new(beg, end, nd_type(node) == NODE_DOT3);
  1716. - }
  1717. - break;
  1718. + result = eval_dot(self,node);
  1719. + break;
  1720. case NODE_FLIP2: /* like AWK */
  1721. - {
  1722. - VALUE *flip = rb_svar(node->nd_cnt);
  1723. - if (!flip) rb_bug("unexpected local variable");
  1724. - if (!RTEST(*flip)) {
  1725. - if (RTEST(rb_eval(self, node->nd_beg))) {
  1726. - *flip = RTEST(rb_eval(self, node->nd_end))?Qfalse:Qtrue;
  1727. - result = Qtrue;
  1728. - }
  1729. - else {
  1730. - result = Qfalse;
  1731. - }
  1732. - }
  1733. - else {
  1734. - if (RTEST(rb_eval(self, node->nd_end))) {
  1735. - *flip = Qfalse;
  1736. - }
  1737. - result = Qtrue;
  1738. - }
  1739. - }
  1740. + result = eval_flip2(self,node);
  1741. break;
  1742. case NODE_FLIP3: /* like SED */
  1743. - {
  1744. - VALUE *flip = rb_svar(node->nd_cnt);
  1745. - if (!flip) rb_bug("unexpected local variable");
  1746. - if (!RTEST(*flip)) {
  1747. - result = RTEST(rb_eval(self, node->nd_beg)) ? Qtrue : Qfalse;
  1748. - *flip = result;
  1749. - }
  1750. - else {
  1751. - if (RTEST(rb_eval(self, node->nd_end))) {
  1752. - *flip = Qfalse;
  1753. - }
  1754. - result = Qtrue;
  1755. - }
  1756. - }
  1757. + result = eval_flip3(self,node);
  1758. break;
  1759. case NODE_RETURN:
  1760. @@ -3435,75 +4015,25 @@ rb_eval(self, n)
  1761. break;
  1762. case NODE_ARGSCAT:
  1763. - {
  1764. - VALUE args = rb_eval(self, node->nd_head);
  1765. - result = rb_ary_concat(args, splat_value(rb_eval(self, node->nd_body)));
  1766. - }
  1767. + result = rb_eval(self, node->nd_head);
  1768. + result = rb_ary_concat(result, splat_value(rb_eval(self, node->nd_body)));
  1769. break;
  1770. case NODE_ARGSPUSH:
  1771. - {
  1772. - VALUE args = rb_ary_dup(rb_eval(self, node->nd_head));
  1773. - result = rb_ary_push(args, rb_eval(self, node->nd_body));
  1774. - }
  1775. + result = rb_ary_dup(rb_eval(self, node->nd_head));
  1776. + result = rb_ary_push(result, rb_eval(self, node->nd_body));
  1777. break;
  1778. case NODE_ATTRASGN:
  1779. - {
  1780. - VALUE recv;
  1781. - int argc; VALUE *argv; /* used in SETUP_ARGS */
  1782. - int scope;
  1783. - TMP_PROTECT;
  1784. -
  1785. - BEGIN_CALLARGS;
  1786. - if (node->nd_recv == (NODE *)1) {
  1787. - recv = self;
  1788. - scope = 1;
  1789. - }
  1790. - else {
  1791. - recv = rb_eval(self, node->nd_recv);
  1792. - scope = 0;
  1793. - }
  1794. - SETUP_ARGS(node->nd_args);
  1795. - END_CALLARGS;
  1796. -
  1797. - ruby_current_node = node;
  1798. - SET_CURRENT_SOURCE();
  1799. - rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,scope,self);
  1800. - result = argv[argc-1];
  1801. - }
  1802. - break;
  1803. + result = eval_attrasgn(self,node);
  1804. + break;
  1805. case NODE_CALL:
  1806. - {
  1807. - VALUE recv;
  1808. - int argc; VALUE *argv; /* used in SETUP_ARGS */
  1809. - TMP_PROTECT;
  1810. -
  1811. - BEGIN_CALLARGS;
  1812. - recv = rb_eval(self, node->nd_recv);
  1813. - SETUP_ARGS(node->nd_args);
  1814. - END_CALLARGS;
  1815. -
  1816. - ruby_current_node = node;
  1817. - SET_CURRENT_SOURCE();
  1818. - result = rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,0,self);
  1819. - }
  1820. + result = eval_call(self,node);
  1821. break;
  1822. case NODE_FCALL:
  1823. - {
  1824. - int argc; VALUE *argv; /* used in SETUP_ARGS */
  1825. - TMP_PROTECT;
  1826. -
  1827. - BEGIN_CALLARGS;
  1828. - SETUP_ARGS(node->nd_args);
  1829. - END_CALLARGS;
  1830. -
  1831. - ruby_current_node = node;
  1832. - SET_CURRENT_SOURCE();
  1833. - result = rb_call(CLASS_OF(self),self,node->nd_mid,argc,argv,1,self);
  1834. - }
  1835. + result = eval_fcall(self,node);
  1836. break;
  1837. case NODE_VCALL:
  1838. @@ -3513,142 +4043,19 @@ rb_eval(self, n)
  1839. case NODE_SUPER:
  1840. case NODE_ZSUPER:
  1841. - {
  1842. - int argc; VALUE *argv; /* used in SETUP_ARGS */
  1843. - TMP_PROTECT;
  1844. -
  1845. - if (ruby_frame->last_class == 0) {
  1846. - if (ruby_frame->last_func) {
  1847. - rb_name_error(ruby_frame->last_func,
  1848. - "superclass method `%s' disabled",
  1849. - rb_id2name(ruby_frame->orig_func));
  1850. - }
  1851. - else {
  1852. - rb_raise(rb_eNoMethodError, "super called outside of method");
  1853. - }
  1854. - }
  1855. - if (nd_type(node) == NODE_ZSUPER) {
  1856. - argc = ruby_frame->argc;
  1857. - if (argc && DMETHOD_P()) {
  1858. - if (TYPE(RBASIC(ruby_scope)->klass) != T_ARRAY ||
  1859. - RARRAY(RBASIC(ruby_scope)->klass)->len != argc) {
  1860. - rb_raise(rb_eRuntimeError,
  1861. - "super: specify arguments explicitly");
  1862. - }
  1863. - argv = RARRAY(RBASIC(ruby_scope)->klass)->ptr;
  1864. - }
  1865. - else if (!ruby_scope->local_vars) {
  1866. - argc = 0;
  1867. - argv = 0;
  1868. - }
  1869. - else {
  1870. - argv = ruby_scope->local_vars + 2;
  1871. - }
  1872. - }
  1873. - else {
  1874. - BEGIN_CALLARGS;
  1875. - SETUP_ARGS(node->nd_args);
  1876. - END_CALLARGS;
  1877. - ruby_current_node = node;
  1878. - }
  1879. -
  1880. - SET_CURRENT_SOURCE();
  1881. - result = rb_call_super(argc, argv);
  1882. - }
  1883. + result = eval_super(self,node);
  1884. break;
  1885. case NODE_SCOPE:
  1886. - {
  1887. - struct FRAME frame;
  1888. - NODE *saved_cref = 0;
  1889. -
  1890. - frame = *ruby_frame;
  1891. - frame.tmp = ruby_frame;
  1892. - ruby_frame = &frame;
  1893. -
  1894. - PUSH_SCOPE();
  1895. - PUSH_TAG(PROT_NONE);
  1896. - if (node->nd_rval) {
  1897. - saved_cref = ruby_cref;
  1898. - ruby_cref = (NODE*)node->nd_rval;
  1899. - }
  1900. - if (node->nd_tbl) {
  1901. - VALUE *vars = ALLOCA_N(VALUE, node->nd_tbl[0]+1);
  1902. - *vars++ = (VALUE)node;
  1903. - ruby_scope->local_vars = vars;
  1904. - rb_mem_clear(ruby_scope->local_vars, node->nd_tbl[0]);
  1905. - ruby_scope->local_tbl = node->nd_tbl;
  1906. - }
  1907. - else {
  1908. - ruby_scope->local_vars = 0;
  1909. - ruby_scope->local_tbl = 0;
  1910. - }
  1911. - if ((state = EXEC_TAG()) == 0) {
  1912. - result = rb_eval(self, node->nd_next);
  1913. - }
  1914. - POP_TAG();
  1915. - POP_SCOPE();
  1916. - ruby_frame = frame.tmp;
  1917. - if (saved_cref)
  1918. - ruby_cref = saved_cref;
  1919. - if (state) JUMP_TAG(state);
  1920. - }
  1921. - break;
  1922. + result = eval_scope(self,node);
  1923. + break;
  1924. case NODE_OP_ASGN1:
  1925. - {
  1926. - int argc; VALUE *argv; /* used in SETUP_ARGS */
  1927. - VALUE recv, val, tmp;
  1928. - NODE *rval;
  1929. - TMP_PROTECT;
  1930. -
  1931. - recv = rb_eval(self, node->nd_recv);
  1932. - rval = node->nd_args->nd_head;
  1933. - SETUP_ARGS0(node->nd_args->nd_body, 1);
  1934. - val = rb_funcall3(recv, aref, argc, argv);
  1935. - switch (node->nd_mid) {
  1936. - case 0: /* OR */
  1937. - if (RTEST(val)) RETURN(val);
  1938. - val = rb_eval(self, rval);
  1939. - break;
  1940. - case 1: /* AND */
  1941. - if (!RTEST(val)) RETURN(val);
  1942. - val = rb_eval(self, rval);
  1943. - break;
  1944. - default:
  1945. - tmp = rb_eval(self, rval);
  1946. - val = rb_funcall3(val, node->nd_mid, 1, &tmp);
  1947. - }
  1948. - argv[argc] = val;
  1949. - rb_funcall2(recv, aset, argc+1, argv);
  1950. - result = val;
  1951. - }
  1952. + result = eval_op_asgn1(self,node);
  1953. break;
  1954. case NODE_OP_ASGN2:
  1955. - {
  1956. - ID id = node->nd_next->nd_vid;
  1957. - VALUE recv, val, tmp;
  1958. -
  1959. - recv = rb_eval(self, node->nd_recv);
  1960. - val = rb_funcall3(recv, id, 0, 0);
  1961. - switch (node->nd_next->nd_mid) {
  1962. - case 0: /* OR */
  1963. - if (RTEST(val)) RETURN(val);
  1964. - val = rb_eval(self, node->nd_value);
  1965. - break;
  1966. - case 1: /* AND */
  1967. - if (!RTEST(val)) RETURN(val);
  1968. - val = rb_eval(self, node->nd_value);
  1969. - break;
  1970. - default:
  1971. - tmp = rb_eval(self, node->nd_value);
  1972. - val = rb_funcall3(val, node->nd_next->nd_mid, 1, &tmp);
  1973. - }
  1974. -
  1975. - rb_funcall2(recv, node->nd_next->nd_aid, 1, &val);
  1976. - result = val;
  1977. - }
  1978. + result = eval_op_asgn2(self,node);
  1979. break;
  1980. case NODE_OP_ASGN_AND:
  1981. @@ -3697,26 +4104,21 @@ rb_eval(self, n)
  1982. break;
  1983. case NODE_CDECL:
  1984. - result = rb_eval(self, node->nd_value);
  1985. - if (node->nd_vid == 0) {
  1986. - rb_const_set(class_prefix(self, node->nd_else), node->nd_else->nd_mid, result);
  1987. - }
  1988. - else {
  1989. - rb_const_set(ruby_cbase, node->nd_vid, result);
  1990. - }
  1991. + result = rb_eval(self, node->nd_value);
  1992. + eval_cdecl(self, node, result);
  1993. break;
  1994. case NODE_CVDECL:
  1995. if (NIL_P(ruby_cbase)) {
  1996. rb_raise(rb_eTypeError, "no class/module to define class variable");
  1997. }
  1998. - result = rb_eval(self, node->nd_value);
  1999. - rb_cvar_set(cvar_cbase(), node->nd_vid, result, Qtrue);
  2000. + result = rb_eval(self, node->nd_value);
  2001. + eval_cvar_set(node, result, Qtrue);
  2002. break;
  2003. case NODE_CVASGN:
  2004. - result = rb_eval(self, node->nd_value);
  2005. - rb_cvar_set(cvar_cbase(), node->nd_vid, result, Qfalse);
  2006. + result = rb_eval(self, node->nd_value);
  2007. + eval_cvar_set(node, result, Qfalse);
  2008. break;
  2009. case NODE_LVAR:
  2010. @@ -3759,26 +4161,21 @@ rb_eval(self, n)
  2011. break;
  2012. case NODE_COLON2:
  2013. - {
  2014. - VALUE klass;
  2015. -
  2016. - klass = rb_eval(self, node->nd_head);
  2017. - if (rb_is_const_id(node->nd_mid)) {
  2018. - switch (TYPE(klass)) {
  2019. - case T_CLASS:
  2020. - case T_MODULE:
  2021. - result = rb_const_get_from(klass, node->nd_mid);
  2022. - break;
  2023. - default:
  2024. - rb_raise(rb_eTypeError, "%s is not a class/module",
  2025. - RSTRING(rb_obj_as_string(klass))->ptr);
  2026. - break;
  2027. - }
  2028. - }
  2029. - else {
  2030. - result = rb_funcall(klass, node->nd_mid, 0, 0);
  2031. + result = rb_eval(self, node->nd_head);
  2032. + if (rb_is_const_id(node->nd_mid)) {
  2033. + switch (TYPE(result)) {
  2034. + case T_CLASS:
  2035. + case T_MODULE:
  2036. + result = rb_const_get_from(result, node->nd_mid);
  2037. + break;
  2038. + default:
  2039. + rb_raise(rb_eTypeError, "%s is not a class/module",
  2040. + RSTRING(rb_obj_as_string(result))->ptr);
  2041. + break;
  2042. }
  2043. }
  2044. + else
  2045. + result = rb_funcall(result, node->nd_mid, 0, 0);
  2046. break;
  2047. case NODE_COLON3:
  2048. @@ -3809,23 +4206,7 @@ rb_eval(self, n)
  2049. break;
  2050. case NODE_HASH:
  2051. - {
  2052. - NODE *list;
  2053. - VALUE hash = rb_hash_new();
  2054. - VALUE key, val;
  2055. -
  2056. - list = node->nd_head;
  2057. - while (list) {
  2058. - key = rb_eval(self, list->nd_head);
  2059. - list = list->nd_next;
  2060. - if (list == 0)
  2061. - rb_bug("odd number list for Hash");
  2062. - val = rb_eval(self, list->nd_head);
  2063. - list = list->nd_next;
  2064. - rb_hash_aset(hash, key, val);
  2065. - }
  2066. - result = hash;
  2067. - }
  2068. + result = eval_hash(self,node);
  2069. break;
  2070. case NODE_ZARRAY: /* zero length list */
  2071. @@ -3833,19 +4214,7 @@ rb_eval(self, n)
  2072. break;
  2073. case NODE_ARRAY:
  2074. - {
  2075. - VALUE ary;
  2076. - long i;
  2077. -
  2078. - i = node->nd_alen;
  2079. - ary = rb_ary_new2(i);
  2080. - for (i=0;node;node=node->nd_next) {
  2081. - RARRAY(ary)->ptr[i++] = rb_eval(self, node->nd_head);
  2082. - RARRAY(ary)->len = i;
  2083. - }
  2084. -
  2085. - result = ary;
  2086. - }
  2087. + result = eval_array(self,node);
  2088. break;
  2089. case NODE_STR:
  2090. @@ -3861,51 +4230,8 @@ rb_eval(self, n)
  2091. case NODE_DREGX:
  2092. case NODE_DREGX_ONCE:
  2093. case NODE_DSYM:
  2094. - {
  2095. - VALUE str, str2;
  2096. - NODE *list = node->nd_next;
  2097. -
  2098. - str = rb_str_new3(node->nd_lit);
  2099. - while (list) {
  2100. - if (list->nd_head) {
  2101. - switch (nd_type(list->nd_head)) {
  2102. - case NODE_STR:
  2103. - str2 = list->nd_head->nd_lit;
  2104. - break;
  2105. - default:
  2106. - str2 = rb_eval(self, list->nd_head);
  2107. - break;
  2108. - }
  2109. - rb_str_append(str, str2);
  2110. - OBJ_INFECT(str, str2);
  2111. - }
  2112. - list = list->nd_next;
  2113. - }
  2114. - switch (nd_type(node)) {
  2115. - case NODE_DREGX:
  2116. - result = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
  2117. - node->nd_cflag);
  2118. - break;
  2119. - case NODE_DREGX_ONCE: /* regexp expand once */
  2120. - result = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
  2121. - node->nd_cflag);
  2122. - nd_set_type(node, NODE_LIT);
  2123. - node->nd_lit = result;
  2124. - break;
  2125. - case NODE_LIT:
  2126. - /* other thread may replace NODE_DREGX_ONCE to NODE_LIT */
  2127. - goto again;
  2128. - case NODE_DXSTR:
  2129. - result = rb_funcall(self, '`', 1, str);
  2130. - break;
  2131. - case NODE_DSYM:
  2132. - result = rb_str_intern(str);
  2133. - break;
  2134. - default:
  2135. - result = str;
  2136. - break;
  2137. - }
  2138. - }
  2139. + result = eval_slit(self, node);
  2140. + if (result == Qundef) goto again;
  2141. break;
  2142. case NODE_XSTR:
  2143. @@ -3917,84 +4243,13 @@ rb_eval(self, n)
  2144. break;
  2145. case NODE_DEFN:
  2146. - if (node->nd_defn) {
  2147. - NODE *body, *defn;
  2148. - VALUE origin;
  2149. - int noex;
  2150. -
  2151. - if (NIL_P(ruby_class)) {
  2152. - rb_raise(rb_eTypeError, "no class/module to add method");
  2153. - }
  2154. - if (ruby_class == rb_cObject && node->nd_mid == init) {
  2155. - rb_warn("redefining Object#initialize may cause infinite loop");
  2156. - }
  2157. - if (node->nd_mid == __id__ || node->nd_mid == __send__) {
  2158. - rb_warn("redefining `%s' may cause serious problem",
  2159. - rb_id2name(node->nd_mid));
  2160. - }
  2161. - rb_frozen_class_p(ruby_class);
  2162. - body = search_method(ruby_class, node->nd_mid, &origin);
  2163. - if (body){
  2164. - if (RTEST(ruby_verbose) && ruby_class == origin && body->nd_cnt == 0 && body->nd_body) {
  2165. - rb_warning("method redefined; discarding old %s", rb_id2name(node->nd_mid));
  2166. - }
  2167. - }
  2168. -
  2169. - if (SCOPE_TEST(SCOPE_PRIVATE) || node->nd_mid == init) {
  2170. - noex = NOEX_PRIVATE;
  2171. - }
  2172. - else if (SCOPE_TEST(SCOPE_PROTECTED)) {
  2173. - noex = NOEX_PROTECTED;
  2174. - }
  2175. - else {
  2176. - noex = NOEX_PUBLIC;
  2177. - }
  2178. - if (body && origin == ruby_class && body->nd_body == 0) {
  2179. - noex |= NOEX_NOSUPER;
  2180. - }
  2181. -
  2182. - defn = rb_copy_node_scope(node->nd_defn, ruby_cref);
  2183. - rb_add_method(ruby_class, node->nd_mid, defn, noex);
  2184. - if (scope_vmode == SCOPE_MODFUNC) {
  2185. - rb_add_method(rb_singleton_class(ruby_class),
  2186. - node->nd_mid, defn, NOEX_PUBLIC);
  2187. - }
  2188. - result = Qnil;
  2189. - }
  2190. + if (node->nd_defn)
  2191. + eval_defn(self,node);
  2192. break;
  2193. case NODE_DEFS:
  2194. - if (node->nd_defn) {
  2195. - VALUE recv = rb_eval(self, node->nd_recv);
  2196. - VALUE klass;
  2197. - NODE *body = 0, *defn;
  2198. -
  2199. - if (ruby_safe_level >= 4 && !OBJ_TAINTED(recv)) {
  2200. - rb_raise(rb_eSecurityError, "Insecure: can't define singleton method");
  2201. - }
  2202. - if (FIXNUM_P(recv) || SYMBOL_P(recv)) {
  2203. - rb_raise(rb_eTypeError,
  2204. - "can't define singleton method \"%s\" for %s",
  2205. - rb_id2name(node->nd_mid),
  2206. - rb_obj_classname(recv));
  2207. - }
  2208. -
  2209. - if (OBJ_FROZEN(recv)) rb_error_frozen("object");
  2210. - klass = rb_singleton_class(recv);
  2211. - if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, &data)) {
  2212. - body = (NODE *)data;
  2213. - if (ruby_safe_level >= 4) {
  2214. - rb_raise(rb_eSecurityError, "redefining method prohibited");
  2215. - }
  2216. - if (RTEST(ruby_verbose)) {
  2217. - rb_warning("redefine %s", rb_id2name(node->nd_mid));
  2218. - }
  2219. - }
  2220. - defn = rb_copy_node_scope(node->nd_defn, ruby_cref);
  2221. - rb_add_method(klass, node->nd_mid, defn,
  2222. - NOEX_PUBLIC|(body?body->nd_noex&NOEX_UNDEF:0));
  2223. - result = Qnil;
  2224. - }
  2225. + if (node->nd_defn)
  2226. + eval_defs(self,node);
  2227. break;
  2228. case NODE_UNDEF:
  2229. @@ -4002,7 +4257,6 @@ rb_eval(self, n)
  2230. rb_raise(rb_eTypeError, "no class to undef method");
  2231. }
  2232. rb_undef(ruby_class, rb_to_id(rb_eval(self, node->u2.node)));
  2233. - result = Qnil;
  2234. break;
  2235. case NODE_ALIAS:
  2236. @@ -4011,134 +4265,26 @@ rb_eval(self, n)
  2237. }
  2238. rb_alias(ruby_class, rb_to_id(rb_eval(self, node->u1.node)),
  2239. rb_to_id(rb_eval(self, node->u2.node)));
  2240. - result = Qnil;
  2241. break;
  2242. case NODE_VALIAS:
  2243. rb_alias_variable(node->u1.id, node->u2.id);
  2244. - result = Qnil;
  2245. break;
  2246. case NODE_CLASS:
  2247. - {
  2248. - VALUE super, klass, tmp, cbase;
  2249. - ID cname;
  2250. - int gen = Qfalse;
  2251. -
  2252. - cbase = class_prefix(self, node->nd_cpath);
  2253. - cname = node->nd_cpath->nd_mid;
  2254. -
  2255. - if (NIL_P(ruby_cbase)) {
  2256. - rb_raise(rb_eTypeError, "no outer class/module");
  2257. - }
  2258. - if (node->nd_super) {
  2259. - super = rb_eval(self, node->nd_super);
  2260. - rb_check_inheritable(super);
  2261. - }
  2262. - else {
  2263. - super = 0;
  2264. - }
  2265. -
  2266. - if (rb_const_defined_at(cbase, cname)) {
  2267. - klass = rb_const_get_at(cbase, cname);
  2268. - if (TYPE(klass) != T_CLASS) {
  2269. - rb_raise(rb_eTypeError, "%s is not a class",
  2270. - rb_id2name(cname));
  2271. - }
  2272. - if (super) {
  2273. - tmp = rb_class_real(RCLASS(klass)->super);
  2274. - if (tmp != super) {
  2275. - rb_raise(rb_eTypeError, "superclass mismatch for class %s",
  2276. - rb_id2name(cname));
  2277. - }
  2278. - super = 0;
  2279. - }
  2280. - if (ruby_safe_level >= 4) {
  2281. - rb_raise(rb_eSecurityError, "extending class prohibited");
  2282. - }
  2283. - }
  2284. - else {
  2285. - if (!super) super = rb_cObject;
  2286. - klass = rb_define_class_id(cname, super);
  2287. - rb_set_class_path(klass, cbase, rb_id2name(cname));
  2288. - rb_const_set(cbase, cname, klass);
  2289. - gen = Qtrue;
  2290. - }
  2291. - if (ruby_wrapper) {
  2292. - rb_extend_object(klass, ruby_wrapper);
  2293. - rb_include_module(klass, ruby_wrapper);
  2294. - }
  2295. - if (super && gen) {
  2296. - rb_class_inherited(super, klass);
  2297. - }
  2298. - result = module_setup(klass, node);
  2299. - }
  2300. + result = eval_class(self,node);
  2301. break;
  2302. case NODE_MODULE:
  2303. - {
  2304. - VALUE module, cbase;
  2305. - ID cname;
  2306. -
  2307. - if (NIL_P(ruby_cbase)) {
  2308. - rb_raise(rb_eTypeError, "no outer class/module");
  2309. - }
  2310. - cbase = class_prefix(self, node->nd_cpath);
  2311. - cname = node->nd_cpath->nd_mid;
  2312. - if (rb_const_defined_at(cbase, cname)) {
  2313. - module = rb_const_get_at(cbase, cname);
  2314. - if (TYPE(module) != T_MODULE) {
  2315. - rb_raise(rb_eTypeError, "%s is not a module",
  2316. - rb_id2name(cname));
  2317. - }
  2318. - if (ruby_safe_level >= 4) {
  2319. - rb_raise(rb_eSecurityError, "extending module prohibited");
  2320. - }
  2321. - }
  2322. - else {
  2323. - module = rb_define_module_id(cname);
  2324. - rb_set_class_path(module, cbase, rb_id2name(cname));
  2325. - rb_const_set(cbase, cname, module);
  2326. - }
  2327. - if (ruby_wrapper) {
  2328. - rb_extend_object(module, ruby_wrapper);
  2329. - rb_include_module(module, ruby_wrapper);
  2330. - }
  2331. -
  2332. - result = module_setup(module, node);
  2333. - }
  2334. + result = eval_module(self,node);
  2335. break;
  2336. case NODE_SCLASS:
  2337. - {
  2338. - VALUE klass;
  2339. -
  2340. - result = rb_eval(self, node->nd_recv);
  2341. - if (FIXNUM_P(result) || SYMBOL_P(result)) {
  2342. - rb_raise(rb_eTypeError, "no virtual class for %s",
  2343. - rb_obj_classname(result));
  2344. - }
  2345. - if (ruby_safe_level >= 4 && !OBJ_TAINTED(result))
  2346. - rb_raise(rb_eSecurityError, "Insecure: can't extend object");
  2347. - klass = rb_singleton_class(result);
  2348. -
  2349. - if (ruby_wrapper) {
  2350. - rb_extend_object(klass, ruby_wrapper);
  2351. - rb_include_module(klass, ruby_wrapper);
  2352. - }
  2353. -
  2354. - result = module_setup(klass, node);
  2355. - }
  2356. + result = eval_sclass(self,node);
  2357. break;
  2358. case NODE_DEFINED:
  2359. - {
  2360. - char buf[20];
  2361. - char *desc = is_defined(self, node->nd_head, buf);
  2362. -
  2363. - if (desc) result = rb_str_new2(desc);
  2364. - else result = Qnil;
  2365. - }
  2366. + result = eval_defined(self,node);
  2367. break;
  2368. case NODE_NEWLINE:
  2369. @@ -4151,25 +4297,19 @@ rb_eval(self, n)
  2370. default:
  2371. unknown_node(node);
  2372. }
  2373. - finish:
  2374. - CHECK_INTS;
  2375. - if (contnode) {
  2376. - node = contnode;
  2377. - contnode = 0;
  2378. - goto again;
  2379. - }
  2380. - return result;
  2381. + }
  2382. + return result;
  2383. }
  2384. static VALUE
  2385. module_setup(module, n)
  2386. VALUE module;
  2387. - NODE *n;
  2388. + NODE * volatile n;
  2389. {
  2390. - NODE * volatile node = n->nd_body;
  2391. + NODE *node = n->nd_body;
  2392. int state;
  2393. struct FRAME frame;
  2394. - VALUE result = Qnil; /* OK */
  2395. + VALUE result;
  2396. TMP_PROTECT;
  2397. frame = *ruby_frame;
  2398. @@ -4560,16 +4700,16 @@ rb_iter_break()
  2399. break_jump(Qnil);
  2400. }
  2401. -NORETURN(static void rb_longjmp _((int, VALUE)));
  2402. +NORETURN(static void rb_longjmp _((volatile int, volatile VALUE)));
  2403. static VALUE make_backtrace _((void));
  2404. static void
  2405. rb_longjmp(tag, mesg)
  2406. - int tag;
  2407. - VALUE mesg;
  2408. + volatile int tag;
  2409. + volatile VALUE mesg;
  2410. {
  2411. VALUE at;
  2412. - rb_thread_t th = curr_thread;
  2413. + volatile rb_thread_t th = curr_thread;
  2414. if (rb_thread_set_raised(th)) {
  2415. ruby_errinfo = exception_error;
  2416. @@ -4658,7 +4798,8 @@ rb_exc_fatal(mesg)
  2417. void
  2418. rb_interrupt()
  2419. {
  2420. - rb_raise(rb_eInterrupt, "");
  2421. + static const char fmt[1] = {'\0'};
  2422. + rb_raise(rb_eInterrupt, fmt);
  2423. }
  2424. /*
  2425. @@ -4814,7 +4955,7 @@ proc_jump_error(state, result)
  2426. VALUE result;
  2427. {
  2428. char mesg[32];
  2429. - char *statement;
  2430. + const char *statement;
  2431. switch (state) {
  2432. case TAG_BREAK:
  2433. @@ -4925,19 +5066,21 @@ rb_need_block()
  2434. static VALUE
  2435. rb_yield_0(val, self, klass, flags, avalue)
  2436. - VALUE val, self, klass; /* OK */
  2437. - int flags, avalue;
  2438. + volatile VALUE val, self;
  2439. + VALUE klass;
  2440. + int flags;
  2441. + volatile int avalue;
  2442. {
  2443. - NODE *node;
  2444. + NODE *volatile node;
  2445. volatile VALUE result = Qnil;
  2446. volatile VALUE old_cref;
  2447. volatile VALUE old_wrapper;
  2448. struct BLOCK * volatile block;
  2449. struct SCOPE * volatile old_scope;
  2450. - int old_vmode;
  2451. + volatile int old_vmode;
  2452. struct FRAME frame;
  2453. NODE *cnode = ruby_current_node;
  2454. - int lambda = flags & YIELD_LAMBDA_CALL;
  2455. + volatile int lambda = flags & YIELD_LAMBDA_CALL;
  2456. int state;
  2457. rb_need_block();
  2458. @@ -5116,6 +5259,7 @@ rb_yield_0(val, self, klass, flags, avalue)
  2459. tt->retval = result;
  2460. JUMP_TAG(TAG_BREAK);
  2461. }
  2462. + if (tt->tag == PROT_THREAD) break;
  2463. tt = tt->prev;
  2464. }
  2465. proc_jump_error(TAG_BREAK, result);
  2466. @@ -5133,6 +5277,7 @@ VALUE
  2467. rb_yield(val)
  2468. VALUE val;
  2469. {
  2470. + wipeBeforeYield();
  2471. return rb_yield_0(val, 0, 0, 0, Qfalse);
  2472. }
  2473. @@ -5195,6 +5340,7 @@ static VALUE
  2474. rb_f_loop()
  2475. {
  2476. for (;;) {
  2477. + wipeBeforeYield();
  2478. rb_yield_0(Qundef, 0, 0, 0, Qfalse);
  2479. CHECK_INTS;
  2480. }
  2481. @@ -5348,11 +5494,12 @@ assign(self, lhs, val, pcall)
  2482. VALUE
  2483. rb_iterate(it_proc, data1, bl_proc, data2)
  2484. - VALUE (*it_proc) _((VALUE)), (*bl_proc)(ANYARGS);
  2485. - VALUE data1, data2;
  2486. + VALUE (* volatile it_proc) _((VALUE)), (*bl_proc)(ANYARGS);
  2487. + volatile VALUE data1;
  2488. + VALUE data2;
  2489. {
  2490. int state;
  2491. - volatile VALUE retval = Qnil;
  2492. + VALUE retval = Qnil;
  2493. NODE *node = NEW_IFUNC(bl_proc, data2);
  2494. VALUE self = ruby_top_self;
  2495. @@ -5376,12 +5523,7 @@ rb_iterate(it_proc, data1, bl_proc, data2)
  2496. POP_BLOCK();
  2497. POP_TAG();
  2498. - switch (state) {
  2499. - case 0:
  2500. - break;
  2501. - default:
  2502. - JUMP_TAG(state);
  2503. - }
  2504. + if (state) JUMP_TAG(state);
  2505. return retval;
  2506. }
  2507. @@ -5413,16 +5555,17 @@ handle_rescue(self, node)
  2508. VALUE
  2509. #ifdef HAVE_STDARG_PROTOTYPES
  2510. -rb_rescue2(VALUE (*b_proc)(ANYARGS), VALUE data1, VALUE (*r_proc)(ANYARGS), VALUE data2, ...)
  2511. +rb_rescue2(VALUE (* volatile b_proc)(ANYARGS), volatile VALUE data1,
  2512. + VALUE (* volatile r_proc)(ANYARGS), volatile VALUE data2, ...)
  2513. #else
  2514. rb_rescue2(b_proc, data1, r_proc, data2, va_alist)
  2515. - VALUE (*b_proc)(ANYARGS), (*r_proc)(ANYARGS);
  2516. - VALUE data1, data2;
  2517. + VALUE (* volatile b_proc)(ANYARGS), (* volatile r_proc)(ANYARGS);
  2518. + volatile VALUE data1, data2;
  2519. va_dcl
  2520. #endif
  2521. {
  2522. int state;
  2523. - volatile VALUE result;
  2524. + VALUE result;
  2525. volatile VALUE e_info = ruby_errinfo;
  2526. volatile int handle = Qfalse;
  2527. VALUE eclass;
  2528. @@ -5481,9 +5624,9 @@ VALUE
  2529. rb_protect(proc, data, state)
  2530. VALUE (*proc) _((VALUE));
  2531. VALUE data;
  2532. - int *state;
  2533. + int * volatile state;
  2534. {
  2535. - VALUE result = Qnil; /* OK */
  2536. + VALUE result;
  2537. int status;
  2538. PUSH_TAG(PROT_NONE);
  2539. @@ -5496,22 +5639,18 @@ rb_protect(proc, data, state)
  2540. if (state) {
  2541. *state = status;
  2542. }
  2543. - if (status != 0) {
  2544. - return Qnil;
  2545. - }
  2546. -
  2547. - return result;
  2548. + return status ? Qnil : result;
  2549. }
  2550. VALUE
  2551. rb_ensure(b_proc, data1, e_proc, data2)
  2552. VALUE (*b_proc)();
  2553. VALUE data1;
  2554. - VALUE (*e_proc)();
  2555. - VALUE data2;
  2556. + VALUE (* volatile e_proc)();
  2557. + volatile VALUE data2;
  2558. {
  2559. int state;
  2560. - volatile VALUE result = Qnil;
  2561. + VALUE result;
  2562. VALUE retval;
  2563. PUSH_TAG(PROT_NONE);
  2564. @@ -5519,7 +5658,7 @@ rb_ensure(b_proc, data1, e_proc, data2)
  2565. result = (*b_proc)(data1);
  2566. }
  2567. POP_TAG();
  2568. - retval = prot_tag ? prot_tag->retval : Qnil; /* save retval */
  2569. + if (prot_tag) retval = prot_tag->retval; /* save retval */
  2570. if (!thread_no_ensure()) {
  2571. (*e_proc)(data2);
  2572. }
  2573. @@ -5533,7 +5672,7 @@ rb_with_disable_interrupt(proc, data)
  2574. VALUE (*proc)();
  2575. VALUE data;
  2576. {
  2577. - VALUE result = Qnil; /* OK */
  2578. + VALUE result;
  2579. int status;
  2580. DEFER_INTS;
  2581. @@ -5610,7 +5749,7 @@ rb_method_missing(argc, argv, obj)
  2582. {
  2583. ID id;
  2584. VALUE exc = rb_eNoMethodError;
  2585. - char *format = 0;
  2586. + const char *format = 0;
  2587. NODE *cnode = ruby_current_node;
  2588. if (argc == 0 || !SYMBOL_P(argv[0])) {
  2589. @@ -5785,16 +5924,16 @@ call_cfunc(func, recv, len, argc, argv)
  2590. static VALUE
  2591. rb_call0(klass, recv, id, oid, argc, argv, body, flags)
  2592. - VALUE klass, recv;
  2593. - ID id;
  2594. + volatile VALUE klass, recv;
  2595. + volatile ID id;
  2596. ID oid;
  2597. - int argc; /* OK */
  2598. - VALUE *argv; /* OK */
  2599. - NODE * volatile body;
  2600. + int argc;
  2601. + VALUE *argv;
  2602. + NODE *volatile body;
  2603. int flags;
  2604. {
  2605. NODE *b2; /* OK */
  2606. - volatile VALUE result = Qnil;
  2607. + VALUE result;
  2608. int itr;
  2609. static int tick;
  2610. TMP_PROTECT;
  2611. @@ -5911,7 +6050,7 @@ rb_call0(klass, recv, id, oid, argc, argv, body, flags)
  2612. {
  2613. int state;
  2614. VALUE *local_vars; /* OK */
  2615. - NODE *saved_cref = 0;
  2616. + NODE * volatile saved_cref = 0;
  2617. PUSH_SCOPE();
  2618. if (body->nd_rval) {
  2619. @@ -6054,7 +6193,6 @@ rb_call0(klass, recv, id, oid, argc, argv, body, flags)
  2620. default:
  2621. unknown_node(body);
  2622. - break;
  2623. }
  2624. POP_FRAME();
  2625. POP_ITER();
  2626. @@ -6222,7 +6360,7 @@ rb_funcall_rescue(recv, mid, n, va_alist)
  2627. va_dcl
  2628. #endif
  2629. {
  2630. - VALUE result = Qnil; /* OK */
  2631. + VALUE result;
  2632. int status;
  2633. va_list ar;
  2634. @@ -6413,7 +6551,7 @@ rb_frame_last_func()
  2635. static NODE*
  2636. compile(src, file, line)
  2637. VALUE src;
  2638. - char *file;
  2639. + const char *file;
  2640. int line;
  2641. {
  2642. NODE *node;
  2643. @@ -6432,12 +6570,13 @@ compile(src, file, line)
  2644. static VALUE
  2645. eval(self, src, scope, file, line)
  2646. - VALUE self, src, scope;
  2647. - char *file;
  2648. + VALUE self, src;
  2649. + volatile VALUE scope;
  2650. + const char * volatile file;
  2651. int line;
  2652. {
  2653. struct BLOCK *data = NULL;
  2654. - volatile VALUE result = Qnil;
  2655. + VALUE result;
  2656. struct SCOPE * volatile old_scope;
  2657. struct BLOCK * volatile old_block;
  2658. struct RVarmap * volatile old_dyna_vars;
  2659. @@ -6445,7 +6584,7 @@ eval(self, src, scope, file, line)
  2660. int volatile old_vmode;
  2661. volatile VALUE old_wrapper;
  2662. struct FRAME frame;
  2663. - NODE *nodesave = ruby_current_node;
  2664. + NODE * volatile nodesave = ruby_current_node;
  2665. volatile int iter = ruby_frame->iter;
  2666. volatile int safe = ruby_safe_level;
  2667. int state;
  2668. @@ -6510,7 +6649,7 @@ eval(self, src, scope, file, line)
  2669. compile_error(0);
  2670. }
  2671. if (!NIL_P(result)) ruby_errinfo = result;
  2672. - result = eval_node(self, node);
  2673. + result = eval_tree(self, node);
  2674. }
  2675. POP_TAG();
  2676. POP_CLASS();
  2677. @@ -6532,6 +6671,7 @@ eval(self, src, scope, file, line)
  2678. scope_dup(ruby_scope);
  2679. for (tag=prot_tag; tag; tag=tag->prev) {
  2680. + if (tag->tag == PROT_THREAD) break;
  2681. scope_dup(tag->scope);
  2682. }
  2683. for (vars = ruby_dyna_vars; vars; vars = vars->next) {
  2684. @@ -6594,7 +6734,7 @@ rb_f_eval(argc, argv, self)
  2685. VALUE self;
  2686. {
  2687. VALUE src, scope, vfile, vline;
  2688. - char *file = "(eval)";
  2689. + const char *file = "(eval)";
  2690. int line = 1;
  2691. rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
  2692. @@ -6635,12 +6775,13 @@ rb_f_eval(argc, argv, self)
  2693. static VALUE
  2694. exec_under(func, under, cbase, args)
  2695. VALUE (*func)();
  2696. - VALUE under, cbase;
  2697. + VALUE under;
  2698. + volatile VALUE cbase;
  2699. void *args;
  2700. {
  2701. - VALUE val = Qnil; /* OK */
  2702. + VALUE val;
  2703. int state;
  2704. - int mode;
  2705. + volatile int mode;
  2706. struct FRAME *f = ruby_frame;
  2707. PUSH_CLASS(under);
  2708. @@ -6732,7 +6873,7 @@ specific_eval(argc, argv, klass, self)
  2709. return yield_under(klass, self);
  2710. }
  2711. else {
  2712. - char *file = "(eval)";
  2713. + const char *file = "(eval)";
  2714. int line = 1;
  2715. if (argc == 0) {
  2716. @@ -6846,9 +6987,9 @@ rb_load(fname, wrap)
  2717. volatile int prohibit_int = rb_prohibit_interrupt;
  2718. volatile ID last_func;
  2719. volatile VALUE wrapper = ruby_wrapper;
  2720. - volatile VALUE self = ruby_top_self;
  2721. + VALUE self = ruby_top_self;
  2722. NODE *volatile last_node;
  2723. - NODE *saved_cref = ruby_cref;
  2724. + NODE *volatile saved_cref = ruby_cref;
  2725. if (wrap && ruby_safe_level >= 4) {
  2726. StringValue(fname);
  2727. @@ -6897,7 +7038,7 @@ rb_load(fname, wrap)
  2728. ruby_current_node = 0;
  2729. if (state == 0) {
  2730. NODE *node;
  2731. - volatile int critical;
  2732. + int critical;
  2733. DEFER_INTS;
  2734. ruby_in_eval++;
  2735. @@ -6909,7 +7050,7 @@ rb_load(fname, wrap)
  2736. rb_thread_critical = critical;
  2737. ALLOW_INTS;
  2738. if (ruby_nerrs == 0) {
  2739. - eval_node(self, node);
  2740. + eval_tree(self, node);
  2741. }
  2742. }
  2743. ruby_frame->last_func = last_func;
  2744. @@ -6942,7 +7083,7 @@ void
  2745. rb_load_protect(fname, wrap, state)
  2746. VALUE fname;
  2747. int wrap;
  2748. - int *state;
  2749. + int * volatile state;
  2750. {
  2751. int status;
  2752. @@ -7231,7 +7372,7 @@ rb_require_safe(fname, safe)
  2753. VALUE fname;
  2754. int safe;
  2755. {
  2756. - VALUE result = Qnil;
  2757. + VALUE result;
  2758. volatile VALUE errinfo = ruby_errinfo;
  2759. int state;
  2760. struct {
  2761. @@ -7282,7 +7423,8 @@ rb_require_safe(fname, safe)
  2762. rb_provide_feature(feature);
  2763. result = Qtrue;
  2764. }
  2765. - }
  2766. + }else
  2767. + result = Qnil;
  2768. }
  2769. POP_TAG();
  2770. ruby_current_node = saved.node;
  2771. @@ -7950,7 +8092,7 @@ rb_f_at_exit()
  2772. void
  2773. rb_exec_end_proc()
  2774. {
  2775. - struct end_proc_data *link, *tmp;
  2776. + struct end_proc_data *tmp, *volatile link;
  2777. int status;
  2778. volatile int safe = ruby_safe_level;
  2779. @@ -8595,8 +8737,9 @@ proc_invoke(proc, args, self, klass)
  2780. int state;
  2781. volatile int safe = ruby_safe_level;
  2782. volatile VALUE old_wrapper = ruby_wrapper;
  2783. - volatile int pcall, avalue = Qtrue;
  2784. - volatile VALUE tmp = args;
  2785. + volatile int pcall;
  2786. + int avalue = Qtrue;
  2787. + VALUE tmp = args;
  2788. if (rb_block_given_p() && ruby_frame->last_func) {
  2789. if (klass != ruby_frame->last_class)
  2790. @@ -8814,7 +8957,7 @@ proc_to_s(self)
  2791. {
  2792. struct BLOCK *data;
  2793. NODE *node;
  2794. - char *cname = rb_obj_classname(self);
  2795. + const char *cname = rb_obj_classname(self);
  2796. const int w = (sizeof(VALUE) * CHAR_BIT) / 4;
  2797. long len = strlen(cname)+6+w; /* 6:tags 16:addr */
  2798. VALUE str;
  2799. @@ -8895,15 +9038,15 @@ proc_binding(proc)
  2800. static VALUE
  2801. block_pass(self, node)
  2802. - VALUE self;
  2803. - NODE *node;
  2804. + volatile VALUE self;
  2805. + NODE *volatile node;
  2806. {
  2807. - VALUE proc = rb_eval(self, node->nd_body); /* OK */
  2808. + volatile VALUE proc = rb_eval(self, node->nd_body);
  2809. VALUE b;
  2810. struct BLOCK * volatile old_block;
  2811. struct BLOCK _block;
  2812. struct BLOCK *data;
  2813. - volatile VALUE result = Qnil;
  2814. + VALUE result;
  2815. int state;
  2816. volatile int orphan;
  2817. volatile int safe = ruby_safe_level;
  2818. @@ -8950,20 +9093,24 @@ block_pass(self, node)
  2819. PUSH_TAG(PROT_LOOP);
  2820. state = EXEC_TAG();
  2821. - if (state == 0) {
  2822. - retry:
  2823. + switch (state) {
  2824. + case TAG_RETRY:
  2825. + state = 0;
  2826. + case 0:
  2827. proc_set_safe_level(proc);
  2828. if (safe > ruby_safe_level)
  2829. ruby_safe_level = safe;
  2830. result = rb_eval(self, node->nd_iter);
  2831. - }
  2832. - else if (state == TAG_BREAK && TAG_DST()) {
  2833. - result = prot_tag->retval;
  2834. - state = 0;
  2835. - }
  2836. - else if (state == TAG_RETRY) {
  2837. - state = 0;
  2838. - goto retry;
  2839. + break;
  2840. + case TAG_BREAK:
  2841. + result = Qnil;
  2842. + if (TAG_DST()) {
  2843. + result = prot_tag->retval;
  2844. + state = 0;
  2845. + }
  2846. + break;
  2847. + default:
  2848. + result = Qnil;
  2849. }
  2850. POP_TAG();
  2851. POP_ITER();
  2852. @@ -8980,7 +9127,6 @@ block_pass(self, node)
  2853. default:
  2854. JUMP_TAG(state);
  2855. }
  2856. -
  2857. return result;
  2858. }
  2859. @@ -9239,7 +9385,7 @@ method_call(argc, argv, method)
  2860. VALUE *argv;
  2861. VALUE method;
  2862. {
  2863. - VALUE result = Qnil; /* OK */
  2864. + VALUE result;
  2865. struct METHOD *data;
  2866. int safe;
  2867. @@ -9473,7 +9619,7 @@ method_inspect(method)
  2868. struct METHOD *data;
  2869. VALUE str;
  2870. const char *s;
  2871. - char *sharp = "#";
  2872. + const char *sharp = "#";
  2873. Data_Get_Struct(method, struct METHOD, data);
  2874. str = rb_str_buf_new2("#<");
  2875. @@ -9538,12 +9684,9 @@ static VALUE
  2876. bmcall(args, method)
  2877. VALUE args, method;
  2878. {
  2879. - volatile VALUE a;
  2880. - VALUE ret;
  2881. -
  2882. - a = svalue_to_avalue(args);
  2883. - ret = method_call(RARRAY(a)->len, RARRAY(a)->ptr, method);
  2884. - a = Qnil; /* prevent tail call */
  2885. + VALUE a = svalue_to_avalue(args);
  2886. + VALUE ret = method_call(RARRAY(a)->len, RARRAY(a)->ptr, method);
  2887. + RB_GC_GUARD(a); /* ensure a is not GC'd during method_call */
  2888. return ret;
  2889. }
  2890. @@ -9658,6 +9801,7 @@ rb_mod_define_method(argc, argv, mod)
  2891. else {
  2892. rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
  2893. }
  2894. + SET_METHOD_SOURCE();
  2895. if (RDATA(body)->dmark == (RUBY_DATA_FUNC)bm_mark) {
  2896. node = NEW_DMETHOD(method_unbind(body));
  2897. }
  2898. @@ -9689,6 +9833,112 @@ rb_mod_define_method(argc, argv, mod)
  2899. return body;
  2900. }
  2901. +
  2902. +#ifdef MBARI_API
  2903. +/*
  2904. + * call-seq:
  2905. + * meth.__file__ => String
  2906. + *
  2907. + * returns the filename containing this method's definition
  2908. + *
  2909. + * raises ArgumentError if method has no associated ruby source code
  2910. + *
  2911. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  2912. + */
  2913. +
  2914. +static VALUE
  2915. +method_source_file_name(VALUE method)
  2916. +{
  2917. + struct METHOD *data;
  2918. + NODE *node;
  2919. +
  2920. + Data_Get_Struct(method, struct METHOD, data);
  2921. + if (node = data->body) {
  2922. + const char *filename = node->nd_file;
  2923. + if (filename)
  2924. + return rb_str_new2(filename);
  2925. + }
  2926. + rb_raise(rb_eArgError, "native Method");
  2927. +}
  2928. +
  2929. +/*
  2930. + * call-seq:
  2931. + * meth.__line__ => Fixnum
  2932. + *
  2933. + * returns the starting line number of this method
  2934. + *
  2935. + * raises ArgumentError if method has no associated ruby source code
  2936. + *
  2937. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  2938. + */
  2939. +
  2940. +static VALUE
  2941. +method_source_line(VALUE method)
  2942. +{
  2943. + struct METHOD *data;
  2944. + NODE *node;
  2945. +
  2946. + Data_Get_Struct(method, struct METHOD, data);
  2947. + if (node = data->body) {
  2948. + int lineno = nd_line(node);
  2949. + if (lineno)
  2950. + return INT2FIX(nd_line(node));
  2951. + }
  2952. + rb_raise(rb_eArgError, "native Method");
  2953. +}
  2954. +
  2955. +
  2956. +/*
  2957. + * call-seq:
  2958. + * prc.__file__ => String
  2959. + *
  2960. + * returns the filename where this proc is defined
  2961. + *
  2962. + * raises ArgumentError if proc has no associated ruby source
  2963. + *
  2964. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  2965. + */
  2966. +
  2967. +static VALUE
  2968. +proc_source_file_name(VALUE block)
  2969. +{
  2970. + struct BLOCK *data;
  2971. + const char *filename;
  2972. + NODE *node;
  2973. +
  2974. + Data_Get_Struct(block, struct BLOCK, data);
  2975. + if ((node = data->frame.node) || (node = data->body))
  2976. + return rb_str_new2(node->nd_file);
  2977. + rb_raise(rb_eArgError, "native Proc");
  2978. +}
  2979. +
  2980. +
  2981. +/*
  2982. + * call-seq:
  2983. + * prc.__line__ => Fixnum
  2984. + *
  2985. + * returns the starting line number of this proc
  2986. + *
  2987. + * raises ArgumentError if proc has no associated ruby source code
  2988. + *
  2989. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  2990. + */
  2991. +
  2992. +static VALUE
  2993. +proc_source_line(VALUE block)
  2994. +{
  2995. + struct BLOCK *data;
  2996. + NODE *node;
  2997. +
  2998. + Data_Get_Struct(block, struct BLOCK, data);
  2999. + if ((node = data->frame.node) || (node = data->body))
  3000. + return INT2FIX( nd_line(node) );
  3001. + rb_raise(rb_eArgError, "native Proc");
  3002. +}
  3003. +
  3004. +#endif /* MBARI_API */
  3005. +
  3006. +
  3007. /*
  3008. * <code>Proc</code> objects are blocks of code that have been bound to
  3009. * a set of local variables. Once bound, the code may be called in
  3010. @@ -9768,6 +10018,15 @@ Init_Proc()
  3011. rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0);
  3012. rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
  3013. rb_define_method(rb_cModule, "instance_method", rb_mod_method, 1);
  3014. +
  3015. +#ifdef MBARI_API
  3016. + rb_define_method(rb_cUnboundMethod, "__file__", method_source_file_name, 0);
  3017. + rb_define_method(rb_cUnboundMethod, "__line__", method_source_line, 0);
  3018. + rb_define_method(rb_cProc, "__file__", proc_source_file_name, 0);
  3019. + rb_define_method(rb_cProc, "__line__", proc_source_line, 0);
  3020. + rb_define_method(rb_cMethod, "__file__", method_source_file_name, 0);
  3021. + rb_define_method(rb_cMethod, "__line__", method_source_line, 0);
  3022. +#endif
  3023. }
  3024. /*
  3025. @@ -10084,14 +10343,19 @@ timeofday()
  3026. return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
  3027. }
  3028. -#define STACK(addr) (th->stk_pos<(VALUE*)(addr) && (VALUE*)(addr)<th->stk_pos+th->stk_len)
  3029. -#define ADJ(addr) (void*)(STACK(addr)?(((VALUE*)(addr)-th->stk_pos)+th->stk_ptr):(VALUE*)(addr))
  3030. +
  3031. +#define ADJ(addr) \
  3032. + if ((size_t)((void *)addr - stkBase) < stkSize) addr=(void *)addr + stkShift
  3033. +
  3034. static void
  3035. thread_mark(th)
  3036. rb_thread_t th;
  3037. {
  3038. struct FRAME *frame;
  3039. struct BLOCK *block;
  3040. + void *stkBase;
  3041. + ptrdiff_t stkShift;
  3042. + size_t stkSize;
  3043. rb_gc_mark(th->result);
  3044. rb_gc_mark(th->thread);
  3045. @@ -10126,15 +10390,26 @@ thread_mark(th)
  3046. }
  3047. #endif
  3048. }
  3049. +
  3050. + stkBase = (void *)th->stk_start;
  3051. + stkSize = th->stk_len * sizeof(VALUE);
  3052. +#if STACK_GROW_DIRECTION == 0
  3053. + if (rb_gc_stack_grow_direction < 0)
  3054. +#endif
  3055. +#if STACK_GROW_DIRECTION <= 0
  3056. + stkBase -= stkSize;
  3057. +#endif
  3058. + stkShift = (void *)th->stk_ptr - stkBase;
  3059. +
  3060. frame = th->frame;
  3061. while (frame && frame != top_frame) {
  3062. - frame = ADJ(frame);
  3063. + ADJ(frame);
  3064. rb_gc_mark_frame(frame);
  3065. if (frame->tmp) {
  3066. struct FRAME *tmp = frame->tmp;
  3067. while (tmp && tmp != top_frame) {
  3068. - tmp = ADJ(tmp);
  3069. + ADJ(tmp);
  3070. rb_gc_mark_frame(tmp);
  3071. tmp = tmp->prev;
  3072. }
  3073. @@ -10143,7 +10418,7 @@ thread_mark(th)
  3074. }
  3075. block = th->block;
  3076. while (block) {
  3077. - block = ADJ(block);
  3078. + ADJ(block);
  3079. rb_gc_mark_frame(&block->frame);
  3080. block = block->prev;
  3081. }
  3082. @@ -10202,16 +10477,57 @@ rb_gc_abort_threads()
  3083. } END_FOREACH_FROM(main_thread, th);
  3084. }
  3085. -static void
  3086. -thread_free(th)
  3087. +
  3088. +static inline void
  3089. +stack_free(th)
  3090. rb_thread_t th;
  3091. {
  3092. - if (th->stk_ptr) free(th->stk_ptr);
  3093. - th->stk_ptr = 0;
  3094. + if (th->stk_ptr) {
  3095. + free(th->stk_ptr);
  3096. + th->stk_ptr = 0;
  3097. + }
  3098. #ifdef __ia64
  3099. - if (th->bstr_ptr) free(th->bstr_ptr);
  3100. - th->bstr_ptr = 0;
  3101. + if (th->bstr_ptr) {
  3102. + free(th->bstr_ptr);
  3103. + th->bstr_ptr = 0;
  3104. + }
  3105. #endif
  3106. +}
  3107. +
  3108. +static void
  3109. +rb_thread_die(th)
  3110. + rb_thread_t th;
  3111. +{
  3112. + th->thgroup = 0;
  3113. + th->status = THREAD_KILLED;
  3114. + stack_free(th);
  3115. +}
  3116. +
  3117. +#define THREAD_DATA(threadObject) ((rb_thread_t)RDATA(threadObject)->data)
  3118. +
  3119. +static inline void
  3120. +cc_purge(cc)
  3121. + rb_thread_t cc;
  3122. +{ /* free continuation's stack if it has just died */
  3123. + if (cc->thread != Qnil && THREAD_DATA(cc->thread)->status == THREAD_KILLED) {
  3124. + cc->thread = Qnil;
  3125. + rb_thread_die(cc); /* can't possibly activate this stack */
  3126. + }
  3127. +}
  3128. +
  3129. +static void
  3130. +cc_mark(cc)
  3131. + rb_thread_t cc;
  3132. +{ /* mark this continuation's stack only if its parent thread is still alive */
  3133. + cc_purge(cc);
  3134. + thread_mark(cc);
  3135. +}
  3136. +
  3137. +static void
  3138. +thread_free(th)
  3139. + rb_thread_t th;
  3140. +{
  3141. + stack_free(th);
  3142. if (th->locals) st_free_table(th->locals);
  3143. if (th->status != THREAD_KILLED) {
  3144. if (th->prev) th->prev->next = th->next;
  3145. @@ -10228,7 +10544,7 @@ rb_thread_check(data)
  3146. rb_raise(rb_eTypeError, "wrong argument type %s (expected Thread)",
  3147. rb_obj_classname(data));
  3148. }
  3149. - return (rb_thread_t)RDATA(data)->data;
  3150. + return THREAD_DATA(data);
  3151. }
  3152. static VALUE rb_thread_raise _((int, VALUE*, rb_thread_t));
  3153. @@ -10255,13 +10571,10 @@ static void
  3154. rb_thread_save_context(th)
  3155. rb_thread_t th;
  3156. {
  3157. - VALUE *pos;
  3158. - int len;
  3159. + size_t len;
  3160. static VALUE tval;
  3161. - len = ruby_stack_length(&pos);
  3162. - th->stk_len = 0;
  3163. - th->stk_pos = pos;
  3164. + len = ruby_stack_length(th->stk_start,&th->stk_pos);
  3165. if (len > th->stk_max) {
  3166. VALUE *ptr = realloc(th->stk_ptr, sizeof(VALUE) * len);
  3167. if (!ptr) rb_memerror();
  3168. @@ -10323,6 +10636,9 @@ static int
  3169. rb_thread_switch(n)
  3170. int n;
  3171. {
  3172. +#if STACK_WIPE_SITES & 1
  3173. + rb_gc_wipe_stack();
  3174. +#endif
  3175. rb_trap_immediate = (curr_thread->flags&0x100)?1:0;
  3176. switch (n) {
  3177. case 0:
  3178. @@ -10359,15 +10675,14 @@ rb_thread_switch(n)
  3179. return 1;
  3180. }
  3181. -#define THREAD_SAVE_CONTEXT(th) \
  3182. - (rb_thread_switch((FLUSH_REGISTER_WINDOWS, ruby_setjmp(rb_thread_save_context(th), (th)->context))))
  3183. +#define THREAD_SAVE_CONTEXT(th) (rb_thread_switch( wipeAfter(\
  3184. + ruby_setjmp(rb_thread_save_context(th), (th)->context))))
  3185. NORETURN(static void rb_thread_restore_context _((rb_thread_t,int)));
  3186. -NORETURN(NOINLINE(static void rb_thread_restore_context_0(rb_thread_t,int,void*)));
  3187. -NORETURN(NOINLINE(static void stack_extend(rb_thread_t, int, VALUE *)));
  3188. +NORETURN(NOINLINE(static void rb_thread_restore_context_0(rb_thread_t,int)));
  3189. static void
  3190. -rb_thread_restore_context_0(rb_thread_t th, int exit, void *vp)
  3191. +rb_thread_restore_context_0(rb_thread_t th, int exit)
  3192. {
  3193. static rb_thread_t tmp;
  3194. static int ex;
  3195. @@ -10424,9 +10739,9 @@ static volatile int C(f), C(g), C(h), C(i), C(j);
  3196. static volatile int C(k), C(l), C(m), C(n), C(o);
  3197. static volatile int C(p), C(q), C(r), C(s), C(t);
  3198. int rb_dummy_false = 0;
  3199. -NORETURN(NOINLINE(static void register_stack_extend(rb_thread_t, int, void *, VALUE *)));
  3200. +NORETURN(NOINLINE(static void register_stack_extend(rb_thread_t, int, VALUE *)));
  3201. static void
  3202. -register_stack_extend(rb_thread_t th, int exit, void *vp, VALUE *curr_bsp)
  3203. +register_stack_extend(rb_thread_t th, int exit, VALUE *curr_bsp)
  3204. {
  3205. if (rb_dummy_false) {
  3206. /* use registers as much as possible */
  3207. @@ -10440,52 +10755,69 @@ register_stack_extend(rb_thread_t th, int exit, void *vp, VALUE *curr_bsp)
  3208. E(p) = E(q) = E(r) = E(s) = E(t) = 0;
  3209. }
  3210. if (curr_bsp < th->bstr_pos+th->bstr_len) {
  3211. - register_stack_extend(th, exit, &exit, (VALUE*)rb_ia64_bsp());
  3212. + register_stack_extend(th, exit, (VALUE*)rb_ia64_bsp());
  3213. }
  3214. - rb_thread_restore_context_0(th, exit, &exit);
  3215. + rb_thread_restore_context_0(th, exit);
  3216. }
  3217. #undef C
  3218. #undef E
  3219. #endif
  3220. -# if defined(_MSC_VER) && _MSC_VER >= 1300
  3221. -__declspec(noinline) static void stack_extend(rb_thread_t, int, VALUE*);
  3222. -# endif
  3223. -static void
  3224. -stack_extend(rb_thread_t th, int exit, VALUE *addr_in_prev_frame)
  3225. -{
  3226. -#define STACK_PAD_SIZE 1024
  3227. - VALUE space[STACK_PAD_SIZE];
  3228. -
  3229. -#if STACK_GROW_DIRECTION < 0
  3230. - if (addr_in_prev_frame > th->stk_pos) stack_extend(th, exit, &space[0]);
  3231. -#elif STACK_GROW_DIRECTION > 0
  3232. - if (addr_in_prev_frame < th->stk_pos + th->stk_len) stack_extend(th, exit, &space[STACK_PAD_SIZE-1]);
  3233. -#else
  3234. - if (addr_in_prev_frame < rb_gc_stack_start) {
  3235. - /* Stack grows downward */
  3236. - if (addr_in_prev_frame > th->stk_pos) stack_extend(th, exit, &space[0]);
  3237. - }
  3238. - else {
  3239. - /* Stack grows upward */
  3240. - if (addr_in_prev_frame < th->stk_pos + th->stk_len) stack_extend(th, exit, &space[STACK_PAD_SIZE-1]);
  3241. - }
  3242. -#endif
  3243. -#ifdef __ia64
  3244. - register_stack_extend(th, exit, space, (VALUE*)rb_ia64_bsp());
  3245. -#else
  3246. - rb_thread_restore_context_0(th, exit, space);
  3247. -#endif
  3248. -}
  3249. static void
  3250. rb_thread_restore_context(th, exit)
  3251. rb_thread_t th;
  3252. int exit;
  3253. {
  3254. + VALUE *pos = th->stk_start;
  3255. +
  3256. +#if HAVE_ALLOCA /* use alloca to grow stack in O(1) time */
  3257. VALUE v;
  3258. +
  3259. + if (!th->stk_ptr) rb_bug("unsaved context");
  3260. +# if !STACK_GROW_DIRECTION /* unknown at compile time */
  3261. + if (rb_gc_stack_grow_direction < 0) {
  3262. +# endif
  3263. +# if STACK_GROW_DIRECTION <= 0
  3264. + pos -= th->stk_len;
  3265. + if (&v > pos)
  3266. + (volatile void *)ALLOCA_N(VALUE, &v-pos);
  3267. +# endif
  3268. +# if !STACK_GROW_DIRECTION
  3269. + }else
  3270. +# endif
  3271. +#if STACK_GROW_DIRECTION >= 0 /* stack grows upward */
  3272. + if (&v < pos + th->stk_len)
  3273. + (volatile void *)ALLOCA_N(VALUE, pos+th->stk_len - &v);
  3274. +# endif
  3275. +
  3276. +#else /* recursive O(n/1024) if extending stack > 1024 VALUEs */
  3277. +
  3278. + volatile VALUE v[1023];
  3279. +
  3280. +# if !STACK_GROW_DIRECTION /* unknown at compile time */
  3281. + if (rb_gc_stack_grow_direction < 0) {
  3282. +# endif
  3283. +# if STACK_GROW_DIRECTION <= 0
  3284. + pos -= th->stk_len;
  3285. + if (v > pos) rb_thread_restore_context(th, exit);
  3286. +# endif
  3287. +# if !STACK_GROW_DIRECTION
  3288. + }else
  3289. +# endif
  3290. +# if STACK_GROW_DIRECTION >= 0 /* stack grows upward */
  3291. + if (v < pos + th->stk_len) rb_thread_restore_context(th, exit);
  3292. +# endif
  3293. if (!th->stk_ptr) rb_bug("unsaved context");
  3294. - stack_extend(th, exit, &v);
  3295. +
  3296. +#endif /* stack now extended */
  3297. +
  3298. +
  3299. +#ifdef __ia64
  3300. + register_stack_extend(th, exit, (VALUE*)rb_ia64_bsp());
  3301. +#else
  3302. + rb_thread_restore_context_0(th, exit);
  3303. +#endif
  3304. }
  3305. static void
  3306. @@ -10499,16 +10831,6 @@ rb_thread_ready(th)
  3307. }
  3308. static void
  3309. -rb_thread_die(th)
  3310. - rb_thread_t th;
  3311. -{
  3312. - th->thgroup = 0;
  3313. - th->status = THREAD_KILLED;
  3314. - if (th->stk_ptr) free(th->stk_ptr);
  3315. - th->stk_ptr = 0;
  3316. -}
  3317. -
  3318. -static void
  3319. rb_thread_remove(th)
  3320. rb_thread_t th;
  3321. {
  3322. @@ -11077,8 +11399,6 @@ rb_thread_select(max, read, write, except, timeout)
  3323. return curr_thread->select_value;
  3324. }
  3325. -static int rb_thread_join _((rb_thread_t, double));
  3326. -
  3327. static int
  3328. rb_thread_join(th, limit)
  3329. rb_thread_t th;
  3330. @@ -11171,7 +11491,7 @@ rb_thread_join_m(argc, argv, thread)
  3331. {
  3332. VALUE limit;
  3333. double delay = DELAY_INFTY;
  3334. - rb_thread_t th = rb_thread_check(thread);
  3335. + rb_thread_t th = THREAD_DATA(thread);
  3336. rb_scan_args(argc, argv, "01", &limit);
  3337. if (!NIL_P(limit)) delay = rb_num2dbl(limit);
  3338. @@ -11283,7 +11603,7 @@ VALUE
  3339. rb_thread_wakeup_alive(thread)
  3340. VALUE thread;
  3341. {
  3342. - rb_thread_t th = rb_thread_check(thread);
  3343. + rb_thread_t th = THREAD_DATA(thread);
  3344. if (th->status == THREAD_KILLED)
  3345. return Qnil;
  3346. @@ -11358,7 +11678,7 @@ VALUE
  3347. rb_thread_kill(thread)
  3348. VALUE thread;
  3349. {
  3350. - rb_thread_t th = rb_thread_check(thread);
  3351. + rb_thread_t th = THREAD_DATA(thread);
  3352. rb_kill_thread(th, 0);
  3353. return thread;
  3354. @@ -11382,7 +11702,7 @@ static VALUE
  3355. rb_thread_kill_bang(thread)
  3356. VALUE thread;
  3357. {
  3358. - rb_thread_t th = rb_thread_check(thread);
  3359. + rb_thread_t th = THREAD_DATA(thread);
  3360. rb_kill_thread(th, THREAD_NO_ENSURE);
  3361. return thread;
  3362. }
  3363. @@ -11554,7 +11874,7 @@ static VALUE
  3364. rb_thread_priority(thread)
  3365. VALUE thread;
  3366. {
  3367. - return INT2NUM(rb_thread_check(thread)->priority);
  3368. + return INT2NUM(THREAD_DATA(thread)->priority);
  3369. }
  3370. @@ -11588,7 +11908,7 @@ rb_thread_priority_set(thread, prio)
  3371. rb_thread_t th;
  3372. rb_secure(4);
  3373. - th = rb_thread_check(thread);
  3374. + th = THREAD_DATA(thread);
  3375. th->priority = NUM2INT(prio);
  3376. rb_thread_schedule();
  3377. @@ -11614,7 +11934,7 @@ rb_thread_safe_level(thread)
  3378. {
  3379. rb_thread_t th;
  3380. - th = rb_thread_check(thread);
  3381. + th = THREAD_DATA(thread);
  3382. if (th == curr_thread) {
  3383. return INT2NUM(ruby_safe_level);
  3384. }
  3385. @@ -11691,7 +12011,7 @@ static VALUE
  3386. rb_thread_abort_exc(thread)
  3387. VALUE thread;
  3388. {
  3389. - return rb_thread_check(thread)->abort?Qtrue:Qfalse;
  3390. + return THREAD_DATA(thread)->abort?Qtrue:Qfalse;
  3391. }
  3392. @@ -11709,7 +12029,7 @@ rb_thread_abort_exc_set(thread, val)
  3393. VALUE thread, val;
  3394. {
  3395. rb_secure(4);
  3396. - rb_thread_check(thread)->abort = RTEST(val);
  3397. + THREAD_DATA(thread)->abort = RTEST(val);
  3398. return val;
  3399. }
  3400. @@ -11718,7 +12038,7 @@ enum rb_thread_status
  3401. rb_thread_status(thread)
  3402. VALUE thread;
  3403. {
  3404. - rb_thread_t th = rb_thread_check(thread);
  3405. + rb_thread_t th = THREAD_DATA(thread);
  3406. return th->status;
  3407. }
  3408. @@ -11737,7 +12057,7 @@ VALUE
  3409. rb_thread_group(thread)
  3410. VALUE thread;
  3411. {
  3412. - VALUE group = rb_thread_check(thread)->thgroup;
  3413. + VALUE group = THREAD_DATA(thread)->thgroup;
  3414. if (!group) {
  3415. group = Qnil;
  3416. }
  3417. @@ -11760,6 +12080,7 @@ rb_thread_group(thread)
  3418. th->result = 0;\
  3419. th->flags = 0;\
  3420. \
  3421. + th->stk_start = rb_gc_stack_start;\
  3422. th->stk_ptr = 0;\
  3423. th->stk_len = 0;\
  3424. th->stk_max = 0;\
  3425. @@ -11971,6 +12292,16 @@ rb_thread_start_0(fn, arg, th)
  3426. "can't start a new thread (frozen ThreadGroup)");
  3427. }
  3428. +
  3429. + th->stk_start = /* establish start of new thread's stack */
  3430. +#if STACK_GROW_DIRECTION > 0
  3431. + (VALUE *)ruby_frame;
  3432. +#elif STACK_GROW_DIRECTION < 0
  3433. + (VALUE *)(ruby_frame+1);
  3434. +#else
  3435. + (VALUE *)(ruby_frame+((VALUE *)(&arg)<rb_gc_stack_start))
  3436. +#endif
  3437. +
  3438. if (!thread_init) {
  3439. thread_init = 1;
  3440. #if defined(HAVE_SETITIMER) || defined(_THREAD_SAFE)
  3441. @@ -12016,6 +12347,8 @@ rb_thread_start_0(fn, arg, th)
  3442. PUSH_TAG(PROT_THREAD);
  3443. if ((state = EXEC_TAG()) == 0) {
  3444. if (THREAD_SAVE_CONTEXT(th) == 0) {
  3445. + ruby_frame->prev = top_frame; /* hide parent thread's frames */
  3446. + ruby_frame->tmp = 0;
  3447. curr_thread = th;
  3448. th->result = (*fn)(arg, th);
  3449. }
  3450. @@ -12127,9 +12460,6 @@ rb_thread_s_new(argc, argv, klass)
  3451. VALUE klass;
  3452. {
  3453. rb_thread_t th = rb_thread_alloc(klass);
  3454. - volatile VALUE *pos;
  3455. -
  3456. - pos = th->stk_pos;
  3457. rb_obj_call_init(th->thread, argc, argv);
  3458. if (th->stk_pos == 0) {
  3459. rb_raise(rb_eThreadError, "uninitialized thread - check `%s#initialize'",
  3460. @@ -12167,7 +12497,7 @@ rb_thread_initialize(thread, args)
  3461. if (!rb_block_given_p()) {
  3462. rb_raise(rb_eThreadError, "must be called with a block");
  3463. }
  3464. - th = rb_thread_check(thread);
  3465. + th = THREAD_DATA(thread);
  3466. if (th->stk_max) {
  3467. NODE *node = th->node;
  3468. if (!node) {
  3469. @@ -12216,7 +12546,7 @@ static VALUE
  3470. rb_thread_value(thread)
  3471. VALUE thread;
  3472. {
  3473. - rb_thread_t th = rb_thread_check(thread);
  3474. + rb_thread_t th = THREAD_DATA(thread);
  3475. while (!rb_thread_join(th, DELAY_INFTY));
  3476. @@ -12251,7 +12581,7 @@ static VALUE
  3477. rb_thread_status_name(thread)
  3478. VALUE thread;
  3479. {
  3480. - rb_thread_t th = rb_thread_check(thread);
  3481. + rb_thread_t th = THREAD_DATA(thread);
  3482. if (rb_thread_dead(th)) {
  3483. if (!NIL_P(th->errinfo) && (th->flags & RAISED_EXCEPTION))
  3484. @@ -12279,7 +12609,7 @@ VALUE
  3485. rb_thread_alive_p(thread)
  3486. VALUE thread;
  3487. {
  3488. - rb_thread_t th = rb_thread_check(thread);
  3489. + rb_thread_t th = THREAD_DATA(thread);
  3490. if (rb_thread_dead(th)) return Qfalse;
  3491. return Qtrue;
  3492. @@ -12302,7 +12632,7 @@ static VALUE
  3493. rb_thread_stop_p(thread)
  3494. VALUE thread;
  3495. {
  3496. - rb_thread_t th = rb_thread_check(thread);
  3497. + rb_thread_t th = THREAD_DATA(thread);
  3498. if (rb_thread_dead(th)) return Qtrue;
  3499. if (th->status == THREAD_STOPPED) return Qtrue;
  3500. @@ -12536,7 +12866,7 @@ rb_thread_raise_m(argc, argv, thread)
  3501. VALUE *argv;
  3502. VALUE thread;
  3503. {
  3504. - rb_thread_t th = rb_thread_check(thread);
  3505. + rb_thread_t th = THREAD_DATA(thread);
  3506. if (ruby_safe_level > th->safe) {
  3507. rb_secure(4);
  3508. @@ -12553,7 +12883,7 @@ rb_thread_local_aref(thread, id)
  3509. rb_thread_t th;
  3510. VALUE val;
  3511. - th = rb_thread_check(thread);
  3512. + th = THREAD_DATA(thread);
  3513. if (ruby_safe_level >= 4 && th != curr_thread) {
  3514. rb_raise(rb_eSecurityError, "Insecure: thread locals");
  3515. }
  3516. @@ -12599,7 +12929,7 @@ rb_thread_local_aset(thread, id, val)
  3517. ID id;
  3518. VALUE val;
  3519. {
  3520. - rb_thread_t th = rb_thread_check(thread);
  3521. + rb_thread_t th = THREAD_DATA(thread);
  3522. if (ruby_safe_level >= 4 && th != curr_thread) {
  3523. rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
  3524. @@ -12652,7 +12982,7 @@ static VALUE
  3525. rb_thread_key_p(thread, id)
  3526. VALUE thread, id;
  3527. {
  3528. - rb_thread_t th = rb_thread_check(thread);
  3529. + rb_thread_t th = THREAD_DATA(thread);
  3530. if (!th->locals) return Qfalse;
  3531. if (st_lookup(th->locals, rb_to_id(id), 0))
  3532. @@ -12688,7 +13018,7 @@ static VALUE
  3533. rb_thread_keys(thread)
  3534. VALUE thread;
  3535. {
  3536. - rb_thread_t th = rb_thread_check(thread);
  3537. + rb_thread_t th = THREAD_DATA(thread);
  3538. VALUE ary = rb_ary_new();
  3539. if (th->locals) {
  3540. @@ -12709,7 +13039,7 @@ rb_thread_inspect(thread)
  3541. VALUE thread;
  3542. {
  3543. char *cname = rb_obj_classname(thread);
  3544. - rb_thread_t th = rb_thread_check(thread);
  3545. + rb_thread_t th = THREAD_DATA(thread);
  3546. const char *status = thread_status_name(th->status);
  3547. VALUE str;
  3548. size_t len = strlen(cname)+7+16+9+1;
  3549. @@ -12789,6 +13119,32 @@ rb_thread_atfork()
  3550. VALUE rb_cCont;
  3551. +
  3552. +static rb_thread_t prep4callcc(void)
  3553. +{
  3554. + rb_thread_t th;
  3555. + struct tag *tag;
  3556. + struct RVarmap *vars;
  3557. +
  3558. + THREAD_ALLOC(th);
  3559. + /* must finish th initialization before any possible gc */
  3560. + th->thread = curr_thread->thread; /* brent@mbari.org */
  3561. + th->thgroup = cont_protect;
  3562. +
  3563. + scope_dup(ruby_scope);
  3564. + for (tag=prot_tag; tag; tag=tag->prev) {
  3565. + if (tag->tag == PROT_THREAD) break;
  3566. + scope_dup(tag->scope);
  3567. + }
  3568. +
  3569. + for (vars = ruby_dyna_vars; vars; vars = vars->next) {
  3570. + if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
  3571. + FL_SET(vars, DVAR_DONT_RECYCLE);
  3572. + }
  3573. + return th;
  3574. +}
  3575. +
  3576. +
  3577. /*
  3578. * call-seq:
  3579. * callcc {|cont| block } => obj
  3580. @@ -12807,35 +13163,14 @@ static VALUE
  3581. rb_callcc(self)
  3582. VALUE self;
  3583. {
  3584. - volatile VALUE cont;
  3585. - rb_thread_t th;
  3586. - volatile rb_thread_t th_save;
  3587. - struct tag *tag;
  3588. - struct RVarmap *vars;
  3589. -
  3590. - THREAD_ALLOC(th);
  3591. - cont = Data_Wrap_Struct(rb_cCont, thread_mark, thread_free, th);
  3592. -
  3593. - scope_dup(ruby_scope);
  3594. - for (tag=prot_tag; tag; tag=tag->prev) {
  3595. - scope_dup(tag->scope);
  3596. - }
  3597. - th->thread = curr_thread->thread;
  3598. - th->thgroup = cont_protect;
  3599. -
  3600. - for (vars = ruby_dyna_vars; vars; vars = vars->next) {
  3601. - if (FL_TEST(vars, DVAR_DONT_RECYCLE)) break;
  3602. - FL_SET(vars, DVAR_DONT_RECYCLE);
  3603. - }
  3604. - th_save = th;
  3605. - if (THREAD_SAVE_CONTEXT(th)) {
  3606. - return th_save->result;
  3607. - }
  3608. - else {
  3609. - return rb_yield(cont);
  3610. - }
  3611. + volatile rb_thread_t th = prep4callcc();
  3612. + return THREAD_SAVE_CONTEXT(th) ?
  3613. + th->result
  3614. + :
  3615. + rb_yield(Data_Wrap_Struct(rb_cCont, cc_mark, thread_free, th));
  3616. }
  3617. +
  3618. /*
  3619. * call-seq:
  3620. * cont.call(args, ...)
  3621. @@ -12858,7 +13193,7 @@ rb_cont_call(argc, argv, cont)
  3622. VALUE *argv;
  3623. VALUE cont;
  3624. {
  3625. - rb_thread_t th = rb_thread_check(cont);
  3626. + rb_thread_t th = THREAD_DATA(cont);
  3627. if (th->thread != curr_thread->thread) {
  3628. rb_raise(rb_eRuntimeError, "continuation called across threads");
  3629. @@ -12882,6 +13217,34 @@ rb_cont_call(argc, argv, cont)
  3630. return Qnil;
  3631. }
  3632. +
  3633. +#ifdef MBARI_API
  3634. +/*
  3635. + * call-seq:
  3636. + * cont.thread
  3637. + *
  3638. + * Returns the thread on which this continuation can be called
  3639. + * or nil if that thread has died
  3640. + *
  3641. + * t = Thread.new {callcc{|c| $x=c}; sleep 5}
  3642. + * sleep 1
  3643. + * $x.thread #=> t
  3644. + * sleep 10
  3645. + * $x.thread #=> nil
  3646. + *
  3647. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  3648. + */
  3649. +static VALUE
  3650. +rb_cont_thread(cont)
  3651. + VALUE cont;
  3652. +{
  3653. + rb_thread_t th = THREAD_DATA(cont);
  3654. + cc_purge(th);
  3655. + return th->thread;
  3656. +}
  3657. +#endif
  3658. +
  3659. +
  3660. struct thgroup {
  3661. int enclosed;
  3662. VALUE group;
  3663. @@ -13034,10 +13397,6 @@ thgroup_add(group, thread)
  3664. rb_secure(4);
  3665. th = rb_thread_check(thread);
  3666. - if (!th->next || !th->prev) {
  3667. - rb_raise(rb_eTypeError, "wrong argument type %s (expected Thread)",
  3668. - rb_obj_classname(thread));
  3669. - }
  3670. if (OBJ_FROZEN(group)) {
  3671. rb_raise(rb_eThreadError, "can't move to the frozen thread group");
  3672. @@ -13135,6 +13494,9 @@ Init_Thread()
  3673. rb_undef_method(CLASS_OF(rb_cCont), "new");
  3674. rb_define_method(rb_cCont, "call", rb_cont_call, -1);
  3675. rb_define_method(rb_cCont, "[]", rb_cont_call, -1);
  3676. +#ifdef MBARI_API
  3677. + rb_define_method(rb_cCont, "thread", rb_cont_thread, 0);
  3678. +#endif
  3679. rb_define_global_function("callcc", rb_callcc, 0);
  3680. rb_global_variable(&cont_protect);
  3681. @@ -13186,14 +13548,15 @@ Init_Thread()
  3682. static VALUE
  3683. rb_f_catch(dmy, tag)
  3684. - VALUE dmy, tag;
  3685. + VALUE dmy;
  3686. + volatile VALUE tag;
  3687. {
  3688. int state;
  3689. - VALUE val = Qnil; /* OK */
  3690. + VALUE val;
  3691. tag = ID2SYM(rb_to_id(tag));
  3692. PUSH_TAG(tag);
  3693. - if ((state = EXEC_TAG()) == 0) {
  3694. + if ((state = wipeAfter(EXEC_TAG_0())) == 0) {
  3695. val = rb_yield_0(tag, 0, 0, 0, Qfalse);
  3696. }
  3697. else if (state == TAG_THROW && tag == prot_tag->dst) {
  3698. @@ -13261,6 +13624,9 @@ rb_f_throw(argc, argv)
  3699. if (!tt) {
  3700. rb_name_error(SYM2ID(tag), "uncaught throw `%s'", rb_id2name(SYM2ID(tag)));
  3701. }
  3702. +#if STACK_WIPE_SITES & 0x800
  3703. + rb_gc_update_stack_extent();
  3704. +#endif
  3705. rb_trap_restore_mask();
  3706. JUMP_TAG(TAG_THROW);
  3707. #ifndef __GNUC__
  3708. diff --git a/gc.c b/gc.c
  3709. index 33f035e..d89ee09 100644
  3710. --- a/gc.c
  3711. +++ b/gc.c
  3712. @@ -22,10 +22,6 @@
  3713. #include <setjmp.h>
  3714. #include <sys/types.h>
  3715. -#ifdef HAVE_SYS_TIME_H
  3716. -#include <sys/time.h>
  3717. -#endif
  3718. -
  3719. #ifdef HAVE_SYS_RESOURCE_H
  3720. #include <sys/resource.h>
  3721. #endif
  3722. @@ -41,39 +37,150 @@ void rb_io_fptr_finalize _((struct OpenFile*));
  3723. #define setjmp(env) _setjmp(env)
  3724. #endif
  3725. -/* Make alloca work the best possible way. */
  3726. -#ifdef __GNUC__
  3727. -# ifndef atarist
  3728. -# ifndef alloca
  3729. -# define alloca __builtin_alloca
  3730. -# endif
  3731. -# endif /* atarist */
  3732. -#else
  3733. -# ifdef HAVE_ALLOCA_H
  3734. -# include <alloca.h>
  3735. -# else
  3736. -# ifndef _AIX
  3737. -# ifndef alloca /* predefined by HP cc +Olibcalls */
  3738. -void *alloca ();
  3739. -# endif
  3740. -# endif /* AIX */
  3741. -# endif /* HAVE_ALLOCA_H */
  3742. -#endif /* __GNUC__ */
  3743. -
  3744. #ifndef GC_MALLOC_LIMIT
  3745. #if defined(MSDOS) || defined(__human68k__)
  3746. #define GC_MALLOC_LIMIT 200000
  3747. #else
  3748. -#define GC_MALLOC_LIMIT 8000000
  3749. +#define GC_MALLOC_LIMIT (2000000*sizeof(VALUE))
  3750. #endif
  3751. #endif
  3752. -static unsigned long malloc_increase = 0;
  3753. -static unsigned long malloc_limit = GC_MALLOC_LIMIT;
  3754. +#ifndef GC_LEVEL_MAX /*maximum # of VALUEs on 'C' stack during GC*/
  3755. +#define GC_LEVEL_MAX 8000
  3756. +#endif
  3757. +#ifndef GC_STACK_PAD
  3758. +#define GC_STACK_PAD 200 /* extra padding VALUEs for GC stack */
  3759. +#endif
  3760. +#define GC_STACK_MAX (GC_LEVEL_MAX+GC_STACK_PAD)
  3761. +
  3762. +static VALUE *stack_limit, *gc_stack_limit;
  3763. +
  3764. +static size_t malloc_increase = 0;
  3765. +static size_t malloc_limit = GC_MALLOC_LIMIT;
  3766. +
  3767. +#ifdef MBARI_API
  3768. +/*
  3769. + * call-seq:
  3770. + * GC.limit => increase limit in bytes
  3771. + *
  3772. + * Get the # of bytes that may be allocated before triggering
  3773. + * a mark and sweep by the garbarge collector to reclaim unused storage.
  3774. + *
  3775. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  3776. + */
  3777. +static VALUE gc_getlimit(VALUE mod)
  3778. +{
  3779. + return ULONG2NUM(malloc_limit);
  3780. +}
  3781. +
  3782. +/*
  3783. + * call-seq:
  3784. + * GC.limit= => updated increase limit in bytes
  3785. + *
  3786. + * Set the # of bytes that may be allocated before triggering
  3787. + * a mark and sweep by the garbarge collector to reclaim unused storage.
  3788. + * Attempts to set the GC.limit= less than 0 will be ignored.
  3789. + *
  3790. + * GC.limit=5000000 #=> 5000000
  3791. + * GC.limit #=> 5000000
  3792. + * GC.limit=-50 #=> 5000000
  3793. + * GC.limit=0 #=> 0 #functionally equivalent to GC.stress=true
  3794. + *
  3795. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  3796. + */
  3797. +static VALUE gc_setlimit(VALUE mod, VALUE newLimit)
  3798. +{
  3799. + long limit = NUM2LONG(newLimit);
  3800. + rb_secure(2);
  3801. + if (limit < 0) return gc_getlimit(mod);
  3802. + malloc_limit = limit;
  3803. + return newLimit;
  3804. +}
  3805. +
  3806. +
  3807. +/*
  3808. + * call-seq:
  3809. + * GC.growth
  3810. + *
  3811. + * Get # of bytes that have been allocated since the last mark & sweep
  3812. + *
  3813. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  3814. + */
  3815. +static VALUE gc_growth(VALUE mod)
  3816. +{
  3817. + return ULONG2NUM(malloc_increase);
  3818. +}
  3819. +
  3820. +
  3821. +/*
  3822. + * call-seq:
  3823. + * GC.exorcise
  3824. + *
  3825. + * Purge ghost references from recently freed stack space
  3826. + *
  3827. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  3828. + */
  3829. +static VALUE gc_exorcise(VALUE mod)
  3830. +{
  3831. + rb_gc_wipe_stack();
  3832. + return Qnil;
  3833. +}
  3834. +
  3835. +#else /* no api changes */
  3836. +
  3837. +static size_t unstressed_malloc_limit = GC_MALLOC_LIMIT;
  3838. +
  3839. +/*
  3840. + * call-seq:
  3841. + * GC.stress => true or false
  3842. + *
  3843. + * returns current status of GC stress mode.
  3844. + *
  3845. + * <i>Only available when MBARI_API extentions are disabled at build time</i>
  3846. + */
  3847. +
  3848. +static VALUE
  3849. +gc_stress_get(self)
  3850. + VALUE self;
  3851. +{
  3852. + return malloc_limit ? Qfalse : Qtrue;
  3853. +}
  3854. +
  3855. +/*
  3856. + * call-seq:
  3857. + * GC.stress = bool => bool
  3858. + *
  3859. + * updates GC stress mode.
  3860. + *
  3861. + * When GC.stress = true, GC is invoked for all GC opportunity:
  3862. + * all memory and object allocation.
  3863. + *
  3864. + * Since it makes Ruby very slow, it is only for debugging.
  3865. + *
  3866. + * <i>Only available when MBARI_API extentions are enabled at build time</i>
  3867. + */
  3868. +
  3869. +static VALUE
  3870. +gc_stress_set(self, bool)
  3871. + VALUE self, bool;
  3872. +{
  3873. + rb_secure(2);
  3874. + if (!RTEST(bool))
  3875. + malloc_limit = unstressed_malloc_limit;
  3876. + else if (malloc_limit > 0) {
  3877. + unstressed_malloc_limit = malloc_limit;
  3878. + malloc_limit = 0;
  3879. + }
  3880. + return bool;
  3881. +}
  3882. +
  3883. +#endif /* MBARI_API */
  3884. +
  3885. static void run_final();
  3886. static VALUE nomem_error;
  3887. static void garbage_collect();
  3888. +
  3889. NORETURN(void rb_exc_jump _((VALUE)));
  3890. void
  3891. @@ -93,6 +200,7 @@ rb_memerror()
  3892. rb_exc_raise(nomem_error);
  3893. }
  3894. +
  3895. void *
  3896. ruby_xmalloc(size)
  3897. long size;
  3898. @@ -104,8 +212,9 @@ ruby_xmalloc(size)
  3899. }
  3900. if (size == 0) size = 1;
  3901. - if ((malloc_increase+size) > malloc_limit) {
  3902. + if ((malloc_increase+=size) > malloc_limit) {
  3903. garbage_collect();
  3904. + malloc_increase = size;
  3905. }
  3906. RUBY_CRITICAL(mem = malloc(size));
  3907. if (!mem) {
  3908. @@ -115,8 +224,9 @@ ruby_xmalloc(size)
  3909. rb_memerror();
  3910. }
  3911. }
  3912. - malloc_increase += size;
  3913. -
  3914. +#if STACK_WIPE_SITES & 0x100
  3915. + rb_gc_update_stack_extent();
  3916. +#endif
  3917. return mem;
  3918. }
  3919. @@ -144,6 +254,10 @@ ruby_xrealloc(ptr, size)
  3920. }
  3921. if (!ptr) return xmalloc(size);
  3922. if (size == 0) size = 1;
  3923. + if ((malloc_increase+=size) > malloc_limit) {
  3924. + garbage_collect();
  3925. + malloc_increase = size;
  3926. + }
  3927. RUBY_CRITICAL(mem = realloc(ptr, size));
  3928. if (!mem) {
  3929. garbage_collect();
  3930. @@ -152,8 +266,9 @@ ruby_xrealloc(ptr, size)
  3931. rb_memerror();
  3932. }
  3933. }
  3934. - malloc_increase += size;
  3935. -
  3936. +#if STACK_WIPE_SITES & 0x200
  3937. + rb_gc_update_stack_extent();
  3938. +#endif
  3939. return mem;
  3940. }
  3941. @@ -393,7 +508,7 @@ rb_newobj()
  3942. if (during_gc)
  3943. rb_bug("object allocation during garbage collection phase");
  3944. - if (!freelist) garbage_collect();
  3945. + if (!malloc_limit || !freelist) garbage_collect();
  3946. obj = (VALUE)freelist;
  3947. freelist = freelist->as.free.next;
  3948. @@ -428,6 +543,9 @@ VALUE *rb_gc_stack_start = 0;
  3949. VALUE *rb_gc_register_stack_start = 0;
  3950. #endif
  3951. +VALUE *rb_gc_stack_end = (VALUE *)STACK_GROW_DIRECTION;
  3952. +
  3953. +
  3954. #ifdef DJGPP
  3955. /* set stack size (http://www.delorie.com/djgpp/v2faq/faq15_9.html) */
  3956. unsigned int _stklen = 0x180000; /* 1.5 kB */
  3957. @@ -445,77 +563,90 @@ static unsigned int STACK_LEVEL_MAX = 655300;
  3958. # define STACK_LEVEL_MAX 655300
  3959. #endif
  3960. -#ifdef C_ALLOCA
  3961. -# define SET_STACK_END VALUE stack_end; alloca(0);
  3962. +#ifndef nativeAllocA
  3963. + /* portable way to return an approximate stack pointer */
  3964. +NOINLINE(VALUE *__sp(void));
  3965. +VALUE *__sp(void) {
  3966. + VALUE tos;
  3967. + return &tos;
  3968. +}
  3969. +# define SET_STACK_END VALUE stack_end
  3970. # define STACK_END (&stack_end)
  3971. #else
  3972. -# if defined(__GNUC__) && defined(USE_BUILTIN_FRAME_ADDRESS) && !defined(__ia64)
  3973. -# if ( __GNUC__ == 3 && __GNUC_MINOR__ > 0 ) || __GNUC__ > 3
  3974. -__attribute__ ((noinline))
  3975. -# endif
  3976. -static void
  3977. -stack_end_address(VALUE **stack_end_p)
  3978. -{
  3979. - VALUE stack_end;
  3980. - *stack_end_p = &stack_end;
  3981. -}
  3982. -# define SET_STACK_END VALUE *stack_end; stack_end_address(&stack_end)
  3983. -# else
  3984. -# define SET_STACK_END VALUE *stack_end = alloca(1)
  3985. -# endif
  3986. -# define STACK_END (stack_end)
  3987. +# define SET_STACK_END ((void)0)
  3988. +# define STACK_END __sp()
  3989. #endif
  3990. +
  3991. #if STACK_GROW_DIRECTION < 0
  3992. -# define STACK_LENGTH (rb_gc_stack_start - STACK_END)
  3993. +# define STACK_LENGTH(start) ((start) - STACK_END)
  3994. #elif STACK_GROW_DIRECTION > 0
  3995. -# define STACK_LENGTH (STACK_END - rb_gc_stack_start + 1)
  3996. +# define STACK_LENGTH(start) (STACK_END - (start) + 1)
  3997. #else
  3998. -# define STACK_LENGTH ((STACK_END < rb_gc_stack_start) ? rb_gc_stack_start - STACK_END\
  3999. - : STACK_END - rb_gc_stack_start + 1)
  4000. +# define STACK_LENGTH(start) ((STACK_END < (start)) ? \
  4001. + (start) - STACK_END : STACK_END - (start) + 1)
  4002. #endif
  4003. +
  4004. #if STACK_GROW_DIRECTION > 0
  4005. -# define STACK_UPPER(x, a, b) a
  4006. +# define STACK_UPPER(a, b) a
  4007. #elif STACK_GROW_DIRECTION < 0
  4008. -# define STACK_UPPER(x, a, b) b
  4009. +# define STACK_UPPER(a, b) b
  4010. #else
  4011. -static int grow_direction;
  4012. +int rb_gc_stack_grow_direction;
  4013. static int
  4014. stack_grow_direction(addr)
  4015. VALUE *addr;
  4016. {
  4017. SET_STACK_END;
  4018. -
  4019. - if (STACK_END > addr) return grow_direction = 1;
  4020. - return grow_direction = -1;
  4021. + return rb_gc_stack_grow_direction = STACK_END > addr ? 1 : -1;
  4022. }
  4023. -# define stack_growup_p(x) ((grow_direction ? grow_direction : stack_grow_direction(x)) > 0)
  4024. -# define STACK_UPPER(x, a, b) (stack_growup_p(x) ? a : b)
  4025. +# define STACK_UPPER(a, b) (rb_gc_stack_grow_direction > 0 ? a : b)
  4026. #endif
  4027. -#define GC_WATER_MARK 512
  4028. -
  4029. -#define CHECK_STACK(ret) do {\
  4030. - SET_STACK_END;\
  4031. - (ret) = (STACK_LENGTH > STACK_LEVEL_MAX + GC_WATER_MARK);\
  4032. -} while (0)
  4033. -
  4034. -int
  4035. -ruby_stack_length(p)
  4036. - VALUE **p;
  4037. +size_t
  4038. +ruby_stack_length(start, base)
  4039. + VALUE *start, **base;
  4040. {
  4041. SET_STACK_END;
  4042. - if (p) *p = STACK_UPPER(STACK_END, rb_gc_stack_start, STACK_END);
  4043. - return STACK_LENGTH;
  4044. + if (base) *base = STACK_UPPER(start, STACK_END);
  4045. + return STACK_LENGTH(start);
  4046. }
  4047. int
  4048. ruby_stack_check()
  4049. {
  4050. - int ret;
  4051. + SET_STACK_END;
  4052. + return __stack_past(stack_limit, STACK_END);
  4053. +}
  4054. - CHECK_STACK(ret);
  4055. - return ret;
  4056. +/*
  4057. + Zero memory that was (recently) part of the stack, but is no longer.
  4058. + Invoke when stack is deep to mark its extent and when it's shallow to wipe it.
  4059. +*/
  4060. +#if STACK_WIPE_METHOD != 4
  4061. +#if STACK_WIPE_METHOD
  4062. +void rb_gc_wipe_stack(void)
  4063. +{
  4064. + VALUE *stack_end = rb_gc_stack_end;
  4065. + VALUE *sp = __sp();
  4066. + rb_gc_stack_end = sp;
  4067. +#if STACK_WIPE_METHOD == 1
  4068. +#warning clearing of "ghost references" from the call stack has been disabled
  4069. +#elif STACK_WIPE_METHOD == 2 /* alloca ghost stack before clearing it */
  4070. + if (__stack_past(sp, stack_end)) {
  4071. + size_t bytes = __stack_depth((char *)stack_end, (char *)sp);
  4072. + STACK_UPPER(sp = nativeAllocA(bytes), stack_end = nativeAllocA(bytes));
  4073. + __stack_zero(stack_end, sp);
  4074. + }
  4075. +#elif STACK_WIPE_METHOD == 3 /* clear unallocated area past stack pointer */
  4076. + __stack_zero(stack_end, sp); /* will crash if compiler pushes a temp. here */
  4077. +#else
  4078. +#error unsupported method of clearing ghost references from the stack
  4079. +#endif
  4080. }
  4081. +#else
  4082. +#warning clearing of "ghost references" from the call stack completely disabled
  4083. +#endif
  4084. +#endif
  4085. #define MARK_STACK_MAX 1024
  4086. static VALUE mark_stack[MARK_STACK_MAX];
  4087. @@ -530,7 +661,18 @@ init_mark_stack()
  4088. }
  4089. #define MARK_STACK_EMPTY (mark_stack_ptr == mark_stack)
  4090. -
  4091. +
  4092. +static inline void
  4093. +push_mark_stack(VALUE ptr)
  4094. +{
  4095. + if (!mark_stack_overflow) {
  4096. + if (mark_stack_ptr - mark_stack < MARK_STACK_MAX)
  4097. + *mark_stack_ptr++ = ptr;
  4098. + else
  4099. + mark_stack_overflow = 1;
  4100. + }
  4101. +}
  4102. +
  4103. static st_table *source_filenames;
  4104. char *
  4105. @@ -574,22 +716,22 @@ sweep_source_filename(key, value)
  4106. }
  4107. }
  4108. -static void gc_mark _((VALUE ptr, int lev));
  4109. -static void gc_mark_children _((VALUE ptr, int lev));
  4110. +#define gc_mark(ptr) rb_gc_mark(ptr)
  4111. +static void gc_mark_children _((VALUE ptr));
  4112. static void
  4113. gc_mark_all()
  4114. {
  4115. RVALUE *p, *pend;
  4116. - int i;
  4117. + struct heaps_slot *heap = heaps+heaps_used;
  4118. init_mark_stack();
  4119. - for (i = 0; i < heaps_used; i++) {
  4120. - p = heaps[i].slot; pend = p + heaps[i].limit;
  4121. + while (--heap >= heaps) {
  4122. + p = heap->slot; pend = p + heap->limit;
  4123. while (p < pend) {
  4124. if ((p->as.basic.flags & FL_MARK) &&
  4125. (p->as.basic.flags != FL_MARK)) {
  4126. - gc_mark_children((VALUE)p, 0);
  4127. + gc_mark_children((VALUE)p);
  4128. }
  4129. p++;
  4130. }
  4131. @@ -599,169 +741,129 @@ gc_mark_all()
  4132. static void
  4133. gc_mark_rest()
  4134. {
  4135. + size_t stackLen = mark_stack_ptr - mark_stack;
  4136. +#ifdef nativeAllocA
  4137. + VALUE *tmp_arry = nativeAllocA(stackLen*sizeof(VALUE));
  4138. +#else
  4139. VALUE tmp_arry[MARK_STACK_MAX];
  4140. - VALUE *p;
  4141. -
  4142. - p = (mark_stack_ptr - mark_stack) + tmp_arry;
  4143. - MEMCPY(tmp_arry, mark_stack, VALUE, MARK_STACK_MAX);
  4144. +#endif
  4145. + VALUE *p = tmp_arry + stackLen;
  4146. +
  4147. + MEMCPY(tmp_arry, mark_stack, VALUE, stackLen);
  4148. init_mark_stack();
  4149. - while(p != tmp_arry){
  4150. - p--;
  4151. - gc_mark_children(*p, 0);
  4152. - }
  4153. + while(--p >= tmp_arry) gc_mark_children(*p);
  4154. }
  4155. static inline int
  4156. is_pointer_to_heap(ptr)
  4157. void *ptr;
  4158. {
  4159. - register RVALUE *p = RANY(ptr);
  4160. - register RVALUE *heap_org;
  4161. - register long i;
  4162. + RVALUE *p = RANY(ptr);
  4163. + struct heaps_slot *heap;
  4164. - if (p < lomem || p > himem) return Qfalse;
  4165. - if ((VALUE)p % sizeof(RVALUE) != 0) return Qfalse;
  4166. + if (p < lomem || p > himem || (VALUE)p % sizeof(RVALUE)) return Qfalse;
  4167. /* check if p looks like a pointer */
  4168. - for (i=0; i < heaps_used; i++) {
  4169. - heap_org = heaps[i].slot;
  4170. - if (heap_org <= p && p < heap_org + heaps[i].limit)
  4171. - return Qtrue;
  4172. - }
  4173. + heap = heaps+heaps_used;
  4174. + while (--heap >= heaps)
  4175. + if (p >= heap->slot && p < heap->slot + heap->limit)
  4176. + return Qtrue;
  4177. return Qfalse;
  4178. }
  4179. static void
  4180. mark_locations_array(x, n)
  4181. - register VALUE *x;
  4182. - register long n;
  4183. + VALUE *x;
  4184. + size_t n;
  4185. {
  4186. VALUE v;
  4187. while (n--) {
  4188. v = *x;
  4189. if (is_pointer_to_heap((void *)v)) {
  4190. - gc_mark(v, 0);
  4191. + gc_mark(v);
  4192. }
  4193. x++;
  4194. }
  4195. }
  4196. -void
  4197. +inline void
  4198. rb_gc_mark_locations(start, end)
  4199. VALUE *start, *end;
  4200. {
  4201. - long n;
  4202. -
  4203. - n = end - start;
  4204. - mark_locations_array(start,n);
  4205. + mark_locations_array(start,end - start);
  4206. }
  4207. static int
  4208. -mark_entry(key, value, lev)
  4209. +mark_entry(key, value)
  4210. ID key;
  4211. VALUE value;
  4212. - int lev;
  4213. {
  4214. - gc_mark(value, lev);
  4215. + gc_mark(value);
  4216. return ST_CONTINUE;
  4217. }
  4218. -static void
  4219. -mark_tbl(tbl, lev)
  4220. - st_table *tbl;
  4221. - int lev;
  4222. -{
  4223. - if (!tbl) return;
  4224. - st_foreach(tbl, mark_entry, lev);
  4225. -}
  4226. -
  4227. void
  4228. rb_mark_tbl(tbl)
  4229. st_table *tbl;
  4230. {
  4231. - mark_tbl(tbl, 0);
  4232. + if (!tbl) return;
  4233. + st_foreach(tbl, mark_entry, 0);
  4234. }
  4235. +#define mark_tbl(tbl) rb_mark_tbl(tbl)
  4236. static int
  4237. -mark_keyvalue(key, value, lev)
  4238. +mark_keyvalue(key, value)
  4239. VALUE key;
  4240. VALUE value;
  4241. - int lev;
  4242. {
  4243. - gc_mark(key, lev);
  4244. - gc_mark(value, lev);
  4245. + gc_mark(key);
  4246. + gc_mark(value);
  4247. return ST_CONTINUE;
  4248. }
  4249. -static void
  4250. -mark_hash(tbl, lev)
  4251. - st_table *tbl;
  4252. - int lev;
  4253. -{
  4254. - if (!tbl) return;
  4255. - st_foreach(tbl, mark_keyvalue, lev);
  4256. -}
  4257. -
  4258. void
  4259. rb_mark_hash(tbl)
  4260. st_table *tbl;
  4261. {
  4262. - mark_hash(tbl, 0);
  4263. + if (!tbl) return;
  4264. + st_foreach(tbl, mark_keyvalue, 0);
  4265. }
  4266. +#define mark_hash(tbl) rb_mark_hash(tbl)
  4267. void
  4268. rb_gc_mark_maybe(obj)
  4269. VALUE obj;
  4270. {
  4271. if (is_pointer_to_heap((void *)obj)) {
  4272. - gc_mark(obj, 0);
  4273. + gc_mark(obj);
  4274. }
  4275. }
  4276. -#define GC_LEVEL_MAX 250
  4277. -
  4278. -static void
  4279. -gc_mark(ptr, lev)
  4280. +void
  4281. +rb_gc_mark(ptr)
  4282. VALUE ptr;
  4283. - int lev;
  4284. {
  4285. - register RVALUE *obj;
  4286. -
  4287. - obj = RANY(ptr);
  4288. + RVALUE *obj = RANY(ptr);
  4289. + SET_STACK_END;
  4290. +
  4291. if (rb_special_const_p(ptr)) return; /* special const not marked */
  4292. if (obj->as.basic.flags == 0) return; /* free cell */
  4293. if (obj->as.basic.flags & FL_MARK) return; /* already marked */
  4294. obj->as.basic.flags |= FL_MARK;
  4295. - if (lev > GC_LEVEL_MAX || (lev == 0 && ruby_stack_check())) {
  4296. - if (!mark_stack_overflow) {
  4297. - if (mark_stack_ptr - mark_stack < MARK_STACK_MAX) {
  4298. - *mark_stack_ptr = ptr;
  4299. - mark_stack_ptr++;
  4300. - }
  4301. - else {
  4302. - mark_stack_overflow = 1;
  4303. - }
  4304. - }
  4305. - return;
  4306. + if (__stack_past(gc_stack_limit, STACK_END))
  4307. + push_mark_stack(ptr);
  4308. + else{
  4309. + gc_mark_children(ptr);
  4310. }
  4311. - gc_mark_children(ptr, lev+1);
  4312. -}
  4313. -
  4314. -void
  4315. -rb_gc_mark(ptr)
  4316. - VALUE ptr;
  4317. -{
  4318. - gc_mark(ptr, 0);
  4319. }
  4320. static void
  4321. -gc_mark_children(ptr, lev)
  4322. +gc_mark_children(ptr)
  4323. VALUE ptr;
  4324. - int lev;
  4325. {
  4326. - register RVALUE *obj = RANY(ptr);
  4327. + RVALUE *obj = RANY(ptr);
  4328. goto marking; /* skip */
  4329. @@ -795,7 +897,7 @@ gc_mark_children(ptr, lev)
  4330. case NODE_RESCUE:
  4331. case NODE_RESBODY:
  4332. case NODE_CLASS:
  4333. - gc_mark((VALUE)obj->as.node.u2.node, lev);
  4334. + gc_mark((VALUE)obj->as.node.u2.node);
  4335. /* fall through */
  4336. case NODE_BLOCK: /* 1,3 */
  4337. case NODE_ARRAY:
  4338. @@ -808,7 +910,7 @@ gc_mark_children(ptr, lev)
  4339. case NODE_CALL:
  4340. case NODE_DEFS:
  4341. case NODE_OP_ASGN1:
  4342. - gc_mark((VALUE)obj->as.node.u1.node, lev);
  4343. + gc_mark((VALUE)obj->as.node.u1.node);
  4344. /* fall through */
  4345. case NODE_SUPER: /* 3 */
  4346. case NODE_FCALL:
  4347. @@ -835,7 +937,7 @@ gc_mark_children(ptr, lev)
  4348. case NODE_ALIAS:
  4349. case NODE_VALIAS:
  4350. case NODE_ARGS:
  4351. - gc_mark((VALUE)obj->as.node.u1.node, lev);
  4352. + gc_mark((VALUE)obj->as.node.u1.node);
  4353. /* fall through */
  4354. case NODE_METHOD: /* 2 */
  4355. case NODE_NOT:
  4356. @@ -873,7 +975,7 @@ gc_mark_children(ptr, lev)
  4357. case NODE_SCOPE: /* 2,3 */
  4358. case NODE_BLOCK_PASS:
  4359. case NODE_CDECL:
  4360. - gc_mark((VALUE)obj->as.node.u3.node, lev);
  4361. + gc_mark((VALUE)obj->as.node.u3.node);
  4362. ptr = (VALUE)obj->as.node.u2.node;
  4363. goto again;
  4364. @@ -906,25 +1008,26 @@ gc_mark_children(ptr, lev)
  4365. default: /* unlisted NODE */
  4366. if (is_pointer_to_heap(obj->as.node.u1.node)) {
  4367. - gc_mark((VALUE)obj->as.node.u1.node, lev);
  4368. + gc_mark((VALUE)obj->as.node.u1.node);
  4369. }
  4370. if (is_pointer_to_heap(obj->as.node.u2.node)) {
  4371. - gc_mark((VALUE)obj->as.node.u2.node, lev);
  4372. + gc_mark((VALUE)obj->as.node.u2.node);
  4373. }
  4374. if (is_pointer_to_heap(obj->as.node.u3.node)) {
  4375. - gc_mark((VALUE)obj->as.node.u3.node, lev);
  4376. + ptr = (VALUE)obj->as.node.u3.node;
  4377. + goto again;
  4378. }
  4379. }
  4380. - return; /* no need to mark class. */
  4381. + return; /* no need to mark class. */
  4382. }
  4383. - gc_mark(obj->as.basic.klass, lev);
  4384. + gc_mark(obj->as.basic.klass);
  4385. switch (obj->as.basic.flags & T_MASK) {
  4386. case T_ICLASS:
  4387. case T_CLASS:
  4388. case T_MODULE:
  4389. - mark_tbl(obj->as.klass.m_tbl, lev);
  4390. - mark_tbl(obj->as.klass.iv_tbl, lev);
  4391. + mark_tbl(obj->as.klass.m_tbl);
  4392. + mark_tbl(obj->as.klass.iv_tbl);
  4393. ptr = obj->as.klass.super;
  4394. goto again;
  4395. @@ -934,17 +1037,16 @@ gc_mark_children(ptr, lev)
  4396. goto again;
  4397. }
  4398. else {
  4399. - long i, len = obj->as.array.len;
  4400. VALUE *ptr = obj->as.array.ptr;
  4401. -
  4402. - for (i=0; i < len; i++) {
  4403. - gc_mark(*ptr++, lev);
  4404. + VALUE *pend = ptr + obj->as.array.len;
  4405. + while (ptr < pend) {
  4406. + gc_mark(*ptr++);
  4407. }
  4408. }
  4409. break;
  4410. case T_HASH:
  4411. - mark_hash(obj->as.hash.tbl, lev);
  4412. + mark_hash(obj->as.hash.tbl);
  4413. ptr = obj->as.hash.ifnone;
  4414. goto again;
  4415. @@ -961,7 +1063,7 @@ gc_mark_children(ptr, lev)
  4416. break;
  4417. case T_OBJECT:
  4418. - mark_tbl(obj->as.object.iv_tbl, lev);
  4419. + mark_tbl(obj->as.object.iv_tbl);
  4420. break;
  4421. case T_FILE:
  4422. @@ -979,7 +1081,7 @@ gc_mark_children(ptr, lev)
  4423. break;
  4424. case T_VARMAP:
  4425. - gc_mark(obj->as.varmap.val, lev);
  4426. + gc_mark(obj->as.varmap.val);
  4427. ptr = (VALUE)obj->as.varmap.next;
  4428. goto again;
  4429. @@ -989,19 +1091,17 @@ gc_mark_children(ptr, lev)
  4430. VALUE *vars = &obj->as.scope.local_vars[-1];
  4431. while (n--) {
  4432. - gc_mark(*vars++, lev);
  4433. + gc_mark(*vars++);
  4434. }
  4435. }
  4436. break;
  4437. case T_STRUCT:
  4438. {
  4439. - long len = obj->as.rstruct.len;
  4440. VALUE *ptr = obj->as.rstruct.ptr;
  4441. -
  4442. - while (len--) {
  4443. - gc_mark(*ptr++, lev);
  4444. - }
  4445. + VALUE *pend = ptr + obj->as.rstruct.len;
  4446. + while (ptr < pend)
  4447. + gc_mark(*ptr++);
  4448. }
  4449. break;
  4450. @@ -1057,13 +1157,12 @@ gc_sweep()
  4451. RVALUE *p, *pend, *final_list;
  4452. int freed = 0;
  4453. int i;
  4454. - unsigned long live = 0;
  4455. - unsigned long free_min = 0;
  4456. + long free_min = 0;
  4457. for (i = 0; i < heaps_used; i++) {
  4458. free_min += heaps[i].limit;
  4459. }
  4460. - free_min = free_min * 0.2;
  4461. + free_min /= 5;
  4462. if (free_min < FREE_MIN)
  4463. free_min = FREE_MIN;
  4464. @@ -1074,7 +1173,7 @@ gc_sweep()
  4465. p = heaps[i].slot; pend = p + heaps[i].limit;
  4466. while (p < pend) {
  4467. if (!(p->as.basic.flags&FL_MARK) && BUILTIN_TYPE(p) == T_NODE)
  4468. - gc_mark((VALUE)p, 0);
  4469. + gc_mark((VALUE)p);
  4470. p++;
  4471. }
  4472. }
  4473. @@ -1117,7 +1216,6 @@ gc_sweep()
  4474. }
  4475. else {
  4476. RBASIC(p)->flags &= ~FL_MARK;
  4477. - live++;
  4478. }
  4479. p++;
  4480. }
  4481. @@ -1134,10 +1232,6 @@ gc_sweep()
  4482. freed += n;
  4483. }
  4484. }
  4485. - if (malloc_increase > malloc_limit) {
  4486. - malloc_limit += (malloc_increase - malloc_limit) * (double)live / (live + freed);
  4487. - if (malloc_limit < GC_MALLOC_LIMIT) malloc_limit = GC_MALLOC_LIMIT;
  4488. - }
  4489. malloc_increase = 0;
  4490. if (freed < free_min) {
  4491. add_heap();
  4492. @@ -1291,7 +1385,7 @@ void
  4493. rb_gc_mark_frame(frame)
  4494. struct FRAME *frame;
  4495. {
  4496. - gc_mark((VALUE)frame->node, 0);
  4497. + gc_mark((VALUE)frame->node);
  4498. }
  4499. #ifdef __GNUC__
  4500. @@ -1332,12 +1426,13 @@ int rb_setjmp (rb_jmp_buf);
  4501. #endif /* __human68k__ or DJGPP */
  4502. #endif /* __GNUC__ */
  4503. +
  4504. +
  4505. static void
  4506. -garbage_collect()
  4507. +garbage_collect_0(VALUE *top_frame)
  4508. {
  4509. struct gc_list *list;
  4510. - struct FRAME * volatile frame; /* gcc 2.7.2.3 -O2 bug?? */
  4511. - jmp_buf save_regs_gc_mark;
  4512. + struct FRAME * frame;
  4513. SET_STACK_END;
  4514. #ifdef HAVE_NATIVETHREAD
  4515. @@ -1354,9 +1449,10 @@ garbage_collect()
  4516. if (during_gc) return;
  4517. during_gc++;
  4518. + gc_stack_limit = __stack_grow(STACK_END, GC_LEVEL_MAX);
  4519. init_mark_stack();
  4520. - gc_mark((VALUE)ruby_current_node, 0);
  4521. + gc_mark((VALUE)ruby_current_node);
  4522. /* mark frame stack */
  4523. for (frame = ruby_frame; frame; frame = frame->prev) {
  4524. @@ -1369,25 +1465,21 @@ garbage_collect()
  4525. }
  4526. }
  4527. }
  4528. - gc_mark((VALUE)ruby_scope, 0);
  4529. - gc_mark((VALUE)ruby_dyna_vars, 0);
  4530. + gc_mark((VALUE)ruby_scope);
  4531. + gc_mark((VALUE)ruby_dyna_vars);
  4532. if (finalizer_table) {
  4533. - mark_tbl(finalizer_table, 0);
  4534. + mark_tbl(finalizer_table);
  4535. }
  4536. - FLUSH_REGISTER_WINDOWS;
  4537. - /* This assumes that all registers are saved into the jmp_buf (and stack) */
  4538. - setjmp(save_regs_gc_mark);
  4539. - mark_locations_array((VALUE*)save_regs_gc_mark, sizeof(save_regs_gc_mark) / sizeof(VALUE *));
  4540. #if STACK_GROW_DIRECTION < 0
  4541. - rb_gc_mark_locations((VALUE*)STACK_END, rb_gc_stack_start);
  4542. + rb_gc_mark_locations(top_frame, rb_curr_thread->stk_start);
  4543. #elif STACK_GROW_DIRECTION > 0
  4544. - rb_gc_mark_locations(rb_gc_stack_start, (VALUE*)STACK_END + 1);
  4545. + rb_gc_mark_locations(rb_curr_thread->stk_start, top_frame + 1);
  4546. #else
  4547. - if ((VALUE*)STACK_END < rb_gc_stack_start)
  4548. - rb_gc_mark_locations((VALUE*)STACK_END, rb_gc_stack_start);
  4549. + if (rb_gc_stack_grow_direction < 0)
  4550. + rb_gc_mark_locations(top_frame, rb_curr_thread->stk_start);
  4551. else
  4552. - rb_gc_mark_locations(rb_gc_stack_start, (VALUE*)STACK_END + 1);
  4553. + rb_gc_mark_locations(rb_curr_thread->stk_start, top_frame + 1);
  4554. #endif
  4555. #ifdef __ia64
  4556. /* mark backing store (flushed register window on the stack) */
  4557. @@ -1396,7 +1488,7 @@ garbage_collect()
  4558. #endif
  4559. #if defined(__human68k__) || defined(__mc68000__)
  4560. rb_gc_mark_locations((VALUE*)((char*)STACK_END + 2),
  4561. - (VALUE*)((char*)rb_gc_stack_start + 2));
  4562. + (VALUE*)((char*)rb_curr_thread->stk_start + 2));
  4563. #endif
  4564. rb_gc_mark_threads();
  4565. @@ -1427,10 +1519,39 @@ garbage_collect()
  4566. }
  4567. rb_gc_abort_threads();
  4568. } while (!MARK_STACK_EMPTY);
  4569. -
  4570. gc_sweep();
  4571. }
  4572. +static void
  4573. +garbage_collect()
  4574. +{
  4575. + jmp_buf save_regs_gc_mark;
  4576. + VALUE *top = __sp();
  4577. + FLUSH_REGISTER_WINDOWS;
  4578. + /* This assumes that all registers are saved into the jmp_buf (and stack) */
  4579. + setjmp(save_regs_gc_mark);
  4580. +
  4581. +#if STACK_WIPE_SITES & 0x400
  4582. +# ifdef nativeAllocA
  4583. + if (__stack_past (top, stack_limit)) {
  4584. + /* allocate a large frame to ensure app stack cannot grow into GC stack */
  4585. + (volatile void*) nativeAllocA(__stack_depth((void*)stack_limit,(void*)top));
  4586. + }
  4587. + garbage_collect_0(top);
  4588. +# else /* no native alloca() available */
  4589. + garbage_collect_0(top);
  4590. + {
  4591. + VALUE *paddedLimit = __stack_grow(gc_stack_limit, GC_STACK_PAD);
  4592. + if (__stack_past(rb_gc_stack_end, paddedLimit))
  4593. + rb_gc_stack_end = paddedLimit;
  4594. + }
  4595. + rb_gc_wipe_stack(); /* wipe the whole stack area reserved for this gc */
  4596. +# endif
  4597. +#else
  4598. + garbage_collect_0(top);
  4599. +#endif
  4600. +}
  4601. +
  4602. void
  4603. rb_gc()
  4604. {
  4605. @@ -1455,6 +1576,7 @@ rb_gc_start()
  4606. return Qnil;
  4607. }
  4608. +
  4609. void
  4610. ruby_set_stack_size(size)
  4611. size_t size;
  4612. @@ -1462,6 +1584,29 @@ ruby_set_stack_size(size)
  4613. #ifndef STACK_LEVEL_MAX
  4614. STACK_LEVEL_MAX = size / sizeof(VALUE);
  4615. #endif
  4616. + stack_limit = __stack_grow(rb_gc_stack_start, STACK_LEVEL_MAX-GC_STACK_MAX);
  4617. +}
  4618. +
  4619. +static void
  4620. +set_stack_size(void)
  4621. +{
  4622. +#ifdef HAVE_GETRLIMIT
  4623. + struct rlimit rlim;
  4624. + if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
  4625. + if (rlim.rlim_cur > 0 && rlim.rlim_cur != RLIM_INFINITY) {
  4626. + size_t maxStackBytes = rlim.rlim_cur;
  4627. + if (rlim.rlim_cur != maxStackBytes)
  4628. + maxStackBytes = -1;
  4629. + {
  4630. + size_t space = maxStackBytes/5;
  4631. + if (space > 1024*1024) space = 1024*1024;
  4632. + ruby_set_stack_size(maxStackBytes - space);
  4633. + return;
  4634. + }
  4635. + }
  4636. + }
  4637. +#endif
  4638. + ruby_set_stack_size(STACK_LEVEL_MAX*sizeof(VALUE));
  4639. }
  4640. void
  4641. @@ -1495,7 +1640,7 @@ Init_stack(addr)
  4642. memset(&m, 0, sizeof(m));
  4643. VirtualQuery(&m, &m, sizeof(m));
  4644. rb_gc_stack_start =
  4645. - STACK_UPPER((VALUE *)&m, (VALUE *)m.BaseAddress,
  4646. + STACK_UPPER((VALUE *)m.BaseAddress,
  4647. (VALUE *)((char *)m.BaseAddress + m.RegionSize) - 1);
  4648. #elif defined(STACK_END_ADDRESS)
  4649. {
  4650. @@ -1504,28 +1649,16 @@ Init_stack(addr)
  4651. }
  4652. #else
  4653. if (!addr) addr = (void *)&addr;
  4654. - STACK_UPPER(&addr, addr, ++addr);
  4655. + STACK_UPPER(addr, ++addr);
  4656. if (rb_gc_stack_start) {
  4657. - if (STACK_UPPER(&addr,
  4658. - rb_gc_stack_start > addr,
  4659. + if (STACK_UPPER(rb_gc_stack_start > addr,
  4660. rb_gc_stack_start < addr))
  4661. rb_gc_stack_start = addr;
  4662. return;
  4663. }
  4664. rb_gc_stack_start = addr;
  4665. #endif
  4666. -#ifdef HAVE_GETRLIMIT
  4667. - {
  4668. - struct rlimit rlim;
  4669. -
  4670. - if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
  4671. - unsigned int space = rlim.rlim_cur/5;
  4672. -
  4673. - if (space > 1024*1024) space = 1024*1024;
  4674. - STACK_LEVEL_MAX = (rlim.rlim_cur - space) / sizeof(VALUE);
  4675. - }
  4676. - }
  4677. -#endif
  4678. + set_stack_size();
  4679. }
  4680. void ruby_init_stack(VALUE *addr
  4681. @@ -1535,8 +1668,7 @@ void ruby_init_stack(VALUE *addr
  4682. )
  4683. {
  4684. if (!rb_gc_stack_start ||
  4685. - STACK_UPPER(&addr,
  4686. - rb_gc_stack_start > addr,
  4687. + STACK_UPPER(rb_gc_stack_start > addr,
  4688. rb_gc_stack_start < addr)) {
  4689. rb_gc_stack_start = addr;
  4690. }
  4691. @@ -1547,16 +1679,7 @@ void ruby_init_stack(VALUE *addr
  4692. }
  4693. #endif
  4694. #ifdef HAVE_GETRLIMIT
  4695. - {
  4696. - struct rlimit rlim;
  4697. -
  4698. - if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
  4699. - unsigned int space = rlim.rlim_cur/5;
  4700. -
  4701. - if (space > 1024*1024) space = 1024*1024;
  4702. - STACK_LEVEL_MAX = (rlim.rlim_cur - space) / sizeof(VALUE);
  4703. - }
  4704. - }
  4705. + set_stack_size();
  4706. #elif defined _WIN32
  4707. {
  4708. MEMORY_BASIC_INFORMATION mi;
  4709. @@ -1567,7 +1690,7 @@ void ruby_init_stack(VALUE *addr
  4710. size = (char *)mi.BaseAddress - (char *)mi.AllocationBase;
  4711. space = size / 5;
  4712. if (space > 1024*1024) space = 1024*1024;
  4713. - STACK_LEVEL_MAX = (size - space) / sizeof(VALUE);
  4714. + ruby_set_stack_size(size - space);
  4715. }
  4716. }
  4717. #endif
  4718. @@ -2042,10 +2165,22 @@ Init_GC()
  4719. {
  4720. VALUE rb_mObSpace;
  4721. +#if !STACK_GROW_DIRECTION
  4722. + rb_gc_stack_end = stack_grow_direction(&rb_mObSpace);
  4723. +#endif
  4724. rb_mGC = rb_define_module("GC");
  4725. rb_define_singleton_method(rb_mGC, "start", rb_gc_start, 0);
  4726. rb_define_singleton_method(rb_mGC, "enable", rb_gc_enable, 0);
  4727. rb_define_singleton_method(rb_mGC, "disable", rb_gc_disable, 0);
  4728. +#ifdef MBARI_API
  4729. + rb_define_singleton_method(rb_mGC, "limit", gc_getlimit, 0);
  4730. + rb_define_singleton_method(rb_mGC, "limit=", gc_setlimit, 1);
  4731. + rb_define_singleton_method(rb_mGC, "growth", gc_growth, 0);
  4732. + rb_define_singleton_method(rb_mGC, "exorcise", gc_exorcise, 0);
  4733. +#else
  4734. + rb_define_singleton_method(rb_mGC, "stress", gc_stress_get, 0);
  4735. + rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set, 1);
  4736. +#endif
  4737. rb_define_method(rb_mGC, "garbage_collect", rb_gc_start, 0);
  4738. rb_mObSpace = rb_define_module("ObjectSpace");
  4739. diff --git a/intern.h b/intern.h
  4740. index b251a83..70a7656 100644
  4741. --- a/intern.h
  4742. +++ b/intern.h
  4743. @@ -238,7 +238,7 @@ VALUE rb_file_directory_p _((VALUE,VALUE));
  4744. /* gc.c */
  4745. NORETURN(void rb_memerror __((void)));
  4746. int ruby_stack_check _((void));
  4747. -int ruby_stack_length _((VALUE**));
  4748. +size_t ruby_stack_length _((VALUE *,VALUE**));
  4749. int rb_during_gc _((void));
  4750. char *rb_source_filename _((const char*));
  4751. void rb_gc_mark_locations _((VALUE*, VALUE*));
  4752. diff --git a/missing/alloca.c b/missing/alloca.c
  4753. index 39697f1..3facdfe 100644
  4754. --- a/missing/alloca.c
  4755. +++ b/missing/alloca.c
  4756. @@ -29,6 +29,7 @@
  4757. static char SCCSid[] = "@(#)alloca.c 1.1"; /* for the "what" utility */
  4758. #endif
  4759. +#include <sys/types.h>
  4760. #include "config.h"
  4761. #ifdef emacs
  4762. #ifdef static
  4763. @@ -44,11 +45,7 @@ lose
  4764. #endif /* static */
  4765. #endif /* emacs */
  4766. -#ifdef X3J11
  4767. typedef void *pointer; /* generic pointer type */
  4768. -#else
  4769. -typedef char *pointer; /* generic pointer type */
  4770. -#endif /* X3J11 */
  4771. #define NULL 0 /* null pointer constant */
  4772. @@ -140,8 +137,7 @@ typedef union hdr
  4773. static header *last_alloca_header = NULL; /* -> last alloca header */
  4774. pointer
  4775. -alloca (size) /* returns pointer to storage */
  4776. - unsigned size; /* # bytes to allocate */
  4777. +alloca (size_t size) /* returns pointer to storage */
  4778. {
  4779. auto char probe; /* probes stack depth: */
  4780. register char *depth = &probe;
  4781. diff --git a/node.h b/node.h
  4782. index 7d3f756..4a1db92 100644
  4783. --- a/node.h
  4784. +++ b/node.h
  4785. @@ -409,15 +409,11 @@ struct rb_thread {
  4786. VALUE result;
  4787. - long stk_len;
  4788. - long stk_max;
  4789. - VALUE *stk_ptr;
  4790. - VALUE *stk_pos;
  4791. + size_t stk_len, stk_max;
  4792. + VALUE *stk_ptr, *stk_pos, *stk_start;
  4793. #ifdef __ia64
  4794. - long bstr_len;
  4795. - long bstr_max;
  4796. - VALUE *bstr_ptr;
  4797. - VALUE *bstr_pos;
  4798. + size_t bstr_len, bstr_max;
  4799. + VALUE *bstr_ptr, *bstr_pos;
  4800. #endif
  4801. struct FRAME *frame;
  4802. diff --git a/re.c b/re.c
  4803. index 5553d28..129b621 100644
  4804. --- a/re.c
  4805. +++ b/re.c
  4806. @@ -1419,7 +1419,7 @@ VALUE
  4807. rb_reg_regcomp(str)
  4808. VALUE str;
  4809. {
  4810. - volatile VALUE save_str = str;
  4811. + VALUE save_str = str;
  4812. if (reg_cache && RREGEXP(reg_cache)->len == RSTRING(str)->len
  4813. && case_cache == ruby_ignorecase
  4814. && kcode_cache == reg_kcode
  4815. @@ -1428,8 +1428,9 @@ rb_reg_regcomp(str)
  4816. case_cache = ruby_ignorecase;
  4817. kcode_cache = reg_kcode;
  4818. - return reg_cache = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
  4819. - ruby_ignorecase);
  4820. + reg_cache = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len, ruby_ignorecase);
  4821. + RB_GC_GUARD(save_str);
  4822. + return reg_cache;
  4823. }
  4824. static int
  4825. diff --git a/ruby.h b/ruby.h
  4826. index 2701f0d..3cf4e19 100644
  4827. --- a/ruby.h
  4828. +++ b/ruby.h
  4829. @@ -224,6 +224,8 @@ VALUE rb_ull2inum _((unsigned LONG_LONG));
  4830. #define TYPE(x) rb_type((VALUE)(x))
  4831. +#define RB_GC_GUARD(v) (*(volatile VALUE *)&(v))
  4832. +
  4833. void rb_check_type _((VALUE,int));
  4834. #define Check_Type(v,t) rb_check_type((VALUE)(v),t)
  4835. diff --git a/rubysig.h b/rubysig.h
  4836. index f716824..fae0869 100644
  4837. --- a/rubysig.h
  4838. +++ b/rubysig.h
  4839. @@ -12,8 +12,80 @@
  4840. #ifndef SIG_H
  4841. #define SIG_H
  4842. +
  4843. #include <errno.h>
  4844. +#if defined __ppc__ || defined __powerpc__ || \
  4845. + defined __ppc64__ || defined __powerpc64__
  4846. +#define __anyPowerPC__ 1 /* for compatibility with older gcc versions */
  4847. +#endif
  4848. +
  4849. +/* STACK_WIPE_SITES determines where attempts are made to exorcise
  4850. + "ghost object refereces" from the stack and how the stack is cleared:
  4851. +
  4852. + 0x*001 --> wipe stack just after every thread_switch
  4853. + 0x*002 --> wipe stack just after every EXEC_TAG()
  4854. + 0x*004 --> wipe stack in CHECK_INTS
  4855. + 0x*010 --> wipe stack in while & until loops
  4856. + 0x*020 --> wipe stack before yield() in iterators and outside eval.c
  4857. + 0x*040 --> wipe stack on catch and thread save context
  4858. + 0x*100 --> update stack extent on each object allocation
  4859. + 0x*200 --> update stack extent on each object reallocation
  4860. + 0x*400 --> update stack extent during GC marking passes
  4861. + 0x*800 --> update stack extent on each throw (use with 0x040)
  4862. + 0x1000 --> use inline assembly code for x86, PowerPC, or ARM CPUs
  4863. +
  4864. + 0x0*** --> do not even call rb_wipe_stack()
  4865. + 0x2*** --> call dummy rb_wipe_stack() (for debugging and profiling)
  4866. + 0x4*** --> safe, portable stack clearing in memory allocated with alloca
  4867. + 0x6*** --> use faster, but less safe stack clearing in unallocated stack
  4868. + 0x8*** --> use faster, but less safe stack clearing (with inline code)
  4869. +
  4870. + for most effective gc use 0x*707
  4871. + for fastest micro-benchmarking use 0x0000
  4872. + 0x*770 prevents almost all memory leaks caused by ghost references
  4873. + without adding much overhead for stack clearing.
  4874. + Other good trade offs are 0x*270, 0x*703, 0x*303 or even 0x*03
  4875. +
  4876. + In general, you may lessen the default -mpreferred-stack-boundary
  4877. + only if using less safe stack clearing (0x6***). Lessening the
  4878. + stack alignment with portable stack clearing (0x4***) may fail to clear
  4879. + all ghost references off the stack.
  4880. +
  4881. + When using 0x6*** or 0x8***, the compiler could insert
  4882. + stack push(s) between reading the stack pointer and clearing
  4883. + the ghost references. The register(s) pushed will be
  4884. + cleared by the rb_gc_stack_wipe(), typically resulting in a segfault
  4885. + or an interpreter hang.
  4886. +
  4887. + STACK_WIPE_SITES of 0x8770 works well compiled with gcc on most machines
  4888. + using the recommended CFLAGS="-O2 -fno-stack-protector". However...
  4889. + If it hangs or crashes for you, try changing STACK_WIPE_SITES to 0x4770
  4890. + and please report your details. i.e. CFLAGS, compiler, version, CPU
  4891. +
  4892. + Note that it is redundant to wipe_stack in looping constructs if
  4893. + also doing so in CHECK_INTS. It is also redundant to wipe_stack on
  4894. + each thread_switch if wiping after every thread save context.
  4895. +*/
  4896. +#ifndef STACK_WIPE_SITES
  4897. +# ifdef __x86_64__ /* deal with "red zone" by not inlining stack clearing */
  4898. +# define STACK_WIPE_SITES 0x6770
  4899. +# elif defined __anyPowerPC__ /* On any PowerPC, deal with... */
  4900. +# define STACK_WIPE_SITES 0x7764 /* red zone & alloc(0) doesn't return sp */
  4901. +# else
  4902. +# define STACK_WIPE_SITES 0x8770 /*normal case, use 0x4770 if problems arise*/
  4903. +# endif
  4904. +#endif
  4905. +
  4906. +#if (STACK_WIPE_SITES & 0x14) == 0x14
  4907. +#warning wiping stack in CHECK_INTS makes wiping in loops redundant
  4908. +#endif
  4909. +#if (STACK_WIPE_SITES & 0x41) == 0x41
  4910. +#warning wiping stack after thread save makes wiping on thread_switch redundant
  4911. +#endif
  4912. +
  4913. +#define STACK_WIPE_METHOD (STACK_WIPE_SITES>>13)
  4914. +
  4915. #ifdef _WIN32
  4916. typedef LONG rb_atomic_t;
  4917. @@ -79,9 +151,152 @@ void rb_trap_restore_mask _((void));
  4918. RUBY_EXTERN int rb_thread_critical;
  4919. void rb_thread_schedule _((void));
  4920. +
  4921. +RUBY_EXTERN VALUE *rb_gc_stack_end;
  4922. +RUBY_EXTERN int rb_gc_stack_grow_direction; /* -1 for down or 1 for up */
  4923. +
  4924. +#if STACK_GROW_DIRECTION > 0
  4925. +
  4926. +/* clear stack space between end and sp (not including *sp) */
  4927. +#define __stack_zero(end,sp) __stack_zero_up(end,sp)
  4928. +
  4929. +/* true if top has grown past limit, i.e. top deeper than limit */
  4930. +#define __stack_past(limit,top) __stack_past_up(limit,top)
  4931. +
  4932. +/* depth of mid below stack top */
  4933. +#define __stack_depth(top,mid) __stack_depth_up(top,mid)
  4934. +
  4935. +/* stack pointer top adjusted to include depth more items */
  4936. +#define __stack_grow(top,depth) __stack_grow_up(top,depth)
  4937. +
  4938. +
  4939. +#elif STACK_GROW_DIRECTION < 0
  4940. +#define __stack_zero(end,sp) __stack_zero_down(end,sp)
  4941. +#define __stack_past(limit,top) __stack_past_down(limit,top)
  4942. +#define __stack_depth(top,mid) __stack_depth_down(top,mid)
  4943. +#define __stack_grow(top,depth) __stack_grow_down(top,depth)
  4944. +
  4945. +#else /* limp along if stack direction can't be determined at compile time */
  4946. +#define __stack_zero(end,sp) if (rb_gc_stack_grow_direction<0) \
  4947. + __stack_zero_down(end,sp); else __stack_zero_up(end,sp);
  4948. +#define __stack_past(limit,top) (rb_gc_stack_grow_direction<0 ? \
  4949. + __stack_past_down(limit,top) : __stack_past_up(limit,top))
  4950. +#define __stack_depth(top,mid) (rb_gc_stack_grow_direction<0 ? \
  4951. + __stack_depth_down(top,mid) : __stack_depth_up(top,mid))
  4952. +#define __stack_grow(top,depth) (rb_gc_stack_grow_direction<0 ? \
  4953. + __stack_grow_down(top,depth) : __stack_grow_up(top,depth))
  4954. +#endif
  4955. +
  4956. +#define __stack_zero_up(end,sp) while (end >= ++sp) *sp=0
  4957. +#define __stack_past_up(limit,top) ((limit) < (top))
  4958. +#define __stack_depth_up(top,mid) ((top) - (mid))
  4959. +#define __stack_grow_up(top,depth) ((top)+(depth))
  4960. +
  4961. +#define __stack_zero_down(end,sp) while (end <= --sp) *sp=0
  4962. +#define __stack_past_down(limit,top) ((limit) > (top))
  4963. +#define __stack_depth_down(top,mid) ((mid) - (top))
  4964. +#define __stack_grow_down(top,depth) ((top)-(depth))
  4965. +
  4966. +/* Make alloca work the best possible way. */
  4967. +#ifdef __GNUC__
  4968. +# ifndef atarist
  4969. +# ifndef alloca
  4970. +# define alloca __builtin_alloca
  4971. +# endif
  4972. +# endif /* atarist */
  4973. +
  4974. +# define nativeAllocA __builtin_alloca
  4975. +
  4976. +/* use assembly to get stack pointer quickly */
  4977. +# if STACK_WIPE_SITES & 0x1000
  4978. +# define __defspfn(asmb) \
  4979. +static inline VALUE *__sp(void) __attribute__((always_inline)); \
  4980. +static inline VALUE *__sp(void) \
  4981. +{ \
  4982. + VALUE *sp; asm(asmb); \
  4983. + return sp; \
  4984. +}
  4985. +# ifdef __anyPowerPC__
  4986. +__defspfn("addi %0, r1, 0": "=r"(sp))
  4987. +# elif defined __i386__
  4988. +__defspfn("movl %%esp, %0": "=r"(sp))
  4989. +# elif defined __x86_64__
  4990. +#warn ===> x86_64 inline assembler is known to crash -- change STACK_WIPE_SITES
  4991. +__defspfn("movq %%rsp, %0": "=r"(sp))
  4992. +# elif __arm__
  4993. +__defspfn("mov %0, sp": "=r"(sp))
  4994. +# else
  4995. +# define __sp() ((VALUE *)__builtin_alloca(0))
  4996. +# warning No assembly version of __sp() defined for this CPU.
  4997. +# endif
  4998. +# else
  4999. +# define __sp() ((VALUE *)__builtin_alloca(0))
  5000. +# endif
  5001. +
  5002. +#else // not GNUC
  5003. +
  5004. +# ifdef HAVE_ALLOCA_H
  5005. +# include <alloca.h>
  5006. +# else
  5007. +# ifndef _AIX
  5008. +# ifndef alloca /* predefined by HP cc +Olibcalls */
  5009. +void *alloca ();
  5010. +# endif
  5011. +# endif /* AIX */
  5012. +# endif /* HAVE_ALLOCA_H */
  5013. +
  5014. +# if STACK_WIPE_SITES & 0x1000
  5015. +# warning No assembly versions of __sp() defined for this compiler.
  5016. +# endif
  5017. +# if HAVE_ALLOCA
  5018. +# define __sp() ((VALUE *)alloca(0))
  5019. +# define nativeAllocA alloca
  5020. +# else
  5021. +RUBY_EXTERN VALUE *__sp(void);
  5022. +# if STACK_WIPE_SITES
  5023. +# define STACK_WIPE_SITES 0
  5024. +# warning Disabled Stack Wiping because there is no native alloca()
  5025. +# endif
  5026. +# endif
  5027. +#endif /* __GNUC__ */
  5028. +
  5029. +
  5030. +/*
  5031. + Zero memory that was (recently) part of the stack, but is no longer.
  5032. + Invoke when stack is deep to mark its extent and when it's shallow to wipe it.
  5033. +*/
  5034. +#if STACK_WIPE_METHOD == 0
  5035. +#define rb_gc_wipe_stack() ((void)0)
  5036. +#elif STACK_WIPE_METHOD == 4
  5037. +#define rb_gc_wipe_stack() { \
  5038. + VALUE *end = rb_gc_stack_end; \
  5039. + VALUE *sp = __sp(); \
  5040. + rb_gc_stack_end = sp; \
  5041. + __stack_zero(end, sp); \
  5042. +}
  5043. +#else
  5044. +RUBY_EXTERN void rb_gc_wipe_stack(void);
  5045. +#endif
  5046. +
  5047. +/*
  5048. + Update our record of maximum stack extent without zeroing unused stack
  5049. +*/
  5050. +#define rb_gc_update_stack_extent() do { \
  5051. + VALUE *sp = __sp(); \
  5052. + if __stack_past(rb_gc_stack_end, sp) rb_gc_stack_end = sp; \
  5053. +} while(0)
  5054. +
  5055. +
  5056. +#if STACK_WIPE_SITES & 4
  5057. +# define CHECK_INTS_wipe_stack() rb_gc_wipe_stack()
  5058. +#else
  5059. +# define CHECK_INTS_wipe_stack() (void)0
  5060. +#endif
  5061. +
  5062. #if defined(HAVE_SETITIMER) || defined(_THREAD_SAFE)
  5063. RUBY_EXTERN int rb_thread_pending;
  5064. # define CHECK_INTS do {\
  5065. + CHECK_INTS_wipe_stack(); \
  5066. if (!(rb_prohibit_interrupt || rb_thread_critical)) {\
  5067. if (rb_thread_pending) rb_thread_schedule();\
  5068. if (rb_trap_pending) rb_trap_exec();\
  5069. @@ -92,13 +307,14 @@ RUBY_EXTERN int rb_thread_pending;
  5070. RUBY_EXTERN int rb_thread_tick;
  5071. #define THREAD_TICK 500
  5072. #define CHECK_INTS do {\
  5073. + CHECK_INTS_wipe_stack(); \
  5074. if (!(rb_prohibit_interrupt || rb_thread_critical)) {\
  5075. if (rb_thread_tick-- <= 0) {\
  5076. rb_thread_tick = THREAD_TICK;\
  5077. rb_thread_schedule();\
  5078. }\
  5079. + if (rb_trap_pending) rb_trap_exec();\
  5080. }\
  5081. - if (rb_trap_pending) rb_trap_exec();\
  5082. } while (0)
  5083. #endif
  5084. diff --git a/signal.c b/signal.c
  5085. index b6cad9d..bc9deae 100644
  5086. --- a/signal.c
  5087. +++ b/signal.c
  5088. @@ -389,6 +389,7 @@ rb_f_kill(argc, argv)
  5089. rb_sys_fail(0);
  5090. }
  5091. }
  5092. + CHECK_INTS; /* in case we killed ourselves */
  5093. return INT2FIX(i-1);
  5094. }
  5095. diff --git a/test/ruby/suicide.rb b/test/ruby/suicide.rb
  5096. index 2687ed0..c7a0a67 100644
  5097. --- a/test/ruby/suicide.rb
  5098. +++ b/test/ruby/suicide.rb
  5099. @@ -1,2 +1,4 @@
  5100. STDERR.reopen(STDOUT)
  5101. -at_exit{Process.kill(:INT, $$)}
  5102. +at_exit{Process.kill(:INT, $$); sleep 0}
  5103. +# brent@mbari.org says
  5104. +# sleep 0 avoids race between process termination and signal reception
  5105. diff --git a/version.c b/version.c
  5106. index b235673..dbc65b0 100644
  5107. --- a/version.c
  5108. +++ b/version.c
  5109. @@ -14,23 +14,44 @@
  5110. #include "version.h"
  5111. #include <stdio.h>
  5112. +#define PRINT(type) puts(ruby_##type)
  5113. +#define MKSTR(type) rb_obj_freeze(rb_str_new(ruby_##type, sizeof(ruby_##type)-1))
  5114. +
  5115. const char ruby_version[] = RUBY_VERSION;
  5116. const char ruby_release_date[] = RUBY_RELEASE_DATE;
  5117. const char ruby_platform[] = RUBY_PLATFORM;
  5118. const int ruby_patchlevel = RUBY_PATCHLEVEL;
  5119. +const char *ruby_description;
  5120. +const char *ruby_copyright;
  5121. void
  5122. Init_version()
  5123. {
  5124. - VALUE v = rb_obj_freeze(rb_str_new2(ruby_version));
  5125. - VALUE d = rb_obj_freeze(rb_str_new2(ruby_release_date));
  5126. - VALUE p = rb_obj_freeze(rb_str_new2(ruby_platform));
  5127. + static char description[128];
  5128. + static char copyright[128];
  5129. + VALUE v = MKSTR(version);
  5130. + VALUE d = MKSTR(release_date);
  5131. + VALUE p = MKSTR(platform);
  5132. + VALUE tmp;
  5133. rb_define_global_const("RUBY_VERSION", v);
  5134. rb_define_global_const("RUBY_RELEASE_DATE", d);
  5135. rb_define_global_const("RUBY_PLATFORM", p);
  5136. rb_define_global_const("RUBY_PATCHLEVEL", INT2FIX(RUBY_PATCHLEVEL));
  5137. + snprintf(description, sizeof(description), "ruby %s (%s %s %d) [%s]",
  5138. + RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_RELEASE_STR,
  5139. + RUBY_RELEASE_NUM, RUBY_PLATFORM);
  5140. + ruby_description = description;
  5141. + tmp = rb_obj_freeze(rb_str_new2(description));
  5142. + rb_define_global_const("RUBY_DESCRIPTION", tmp);
  5143. +
  5144. + snprintf(copyright, sizeof(copyright), "ruby - Copyright (C) %d-%d %s",
  5145. + RUBY_BIRTH_YEAR, RUBY_RELEASE_YEAR, RUBY_AUTHOR);
  5146. + ruby_copyright = copyright;
  5147. + tmp = rb_obj_freeze(rb_str_new2(copyright));
  5148. + rb_define_global_const("RUBY_COPYRIGHT", tmp);
  5149. +
  5150. /* obsolete constants */
  5151. rb_define_global_const("VERSION", v);
  5152. rb_define_global_const("RELEASE_DATE", d);
  5153. @@ -40,13 +61,13 @@ Init_version()
  5154. void
  5155. ruby_show_version()
  5156. {
  5157. - printf("ruby %s (%s patchlevel %d) [%s]\n", RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PATCHLEVEL, RUBY_PLATFORM);
  5158. + PRINT(description);
  5159. fflush(stdout);
  5160. }
  5161. void
  5162. ruby_show_copyright()
  5163. {
  5164. - printf("ruby - Copyright (C) 1993-%d Yukihiro Matsumoto\n", RUBY_RELEASE_YEAR);
  5165. + PRINT(copyright);
  5166. exit(0);
  5167. }
  5168. diff --git a/version.h b/version.h
  5169. index 88ddd67..fa36fa5 100644
  5170. --- a/version.h
  5171. +++ b/version.h
  5172. @@ -1,21 +1,43 @@
  5173. #define RUBY_VERSION "1.8.6"
  5174. -#define RUBY_RELEASE_DATE "2008-08-11"
  5175. +#define RUBY_RELEASE_DATE "2009-3-1"
  5176. #define RUBY_VERSION_CODE 186
  5177. -#define RUBY_RELEASE_CODE 20080811
  5178. +#define RUBY_RELEASE_CODE 20090301
  5179. #define RUBY_PATCHLEVEL 287
  5180. #define RUBY_VERSION_MAJOR 1
  5181. #define RUBY_VERSION_MINOR 8
  5182. #define RUBY_VERSION_TEENY 6
  5183. -#define RUBY_RELEASE_YEAR 2008
  5184. -#define RUBY_RELEASE_MONTH 8
  5185. -#define RUBY_RELEASE_DAY 11
  5186. +#define RUBY_RELEASE_YEAR 2009
  5187. +#define RUBY_RELEASE_MONTH 3
  5188. +#define RUBY_RELEASE_DAY 1
  5189. #ifdef RUBY_EXTERN
  5190. RUBY_EXTERN const char ruby_version[];
  5191. RUBY_EXTERN const char ruby_release_date[];
  5192. RUBY_EXTERN const char ruby_platform[];
  5193. RUBY_EXTERN const int ruby_patchlevel;
  5194. +RUBY_EXTERN const char *ruby_description;
  5195. +RUBY_EXTERN const char *ruby_copyright;
  5196. #endif
  5197. +#define RUBY_AUTHOR "Yukihiro Matsumoto"
  5198. +#define RUBY_BIRTH_YEAR 1993
  5199. +#define RUBY_BIRTH_MONTH 2
  5200. +#define RUBY_BIRTH_DAY 24
  5201. +
  5202. +#include "rubysig.h"
  5203. +
  5204. +#define string_arg(s) #s
  5205. +
  5206. +#ifdef MBARI_API
  5207. +#define _mbari_rev_ "MBARI"
  5208. +#else
  5209. +#define _mbari_rev_ "mbari"
  5210. +#endif
  5211. +
  5212. +#define MBARI_RELEASE(wipe_sites) _mbari_rev_ " 8B/" string_arg(wipe_sites)
  5213. +
  5214. +#define RUBY_RELEASE_STR MBARI_RELEASE(STACK_WIPE_SITES) " on patchlevel"
  5215. +#define RUBY_RELEASE_NUM RUBY_PATCHLEVEL
  5216. +
  5217. --- ruby-1.8.6-p287/configure 2008-08-10 17:38:35.000000000 -0700
  5218. +++ ruby-1.8.6-mbari/configure 2009-02-26 22:43:03.000000000 -0800
  5219. @@ -1352,6 +1352,7 @@
  5220. is disabled or omitted entirely, then the package
  5221. will be built only for the target platform
  5222. --enable-frame-address use GCC __builtin_frame_address().
  5223. + --enable-mbari-api enable API changes from the MBARI patches.
  5224. --disable-largefile omit support for large files
  5225. --enable-pthread use pthread library.
  5226. --disable-fastthread do not use the fastthread mutex
  5227. @@ -1364,6 +1365,7 @@
  5228. --with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
  5229. --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
  5230. --without-gcc never use gcc
  5231. + --with-wipe-sites=MASK override default STACK_WIPES_SITES mask in rubysig.h
  5232. --with-winsock2 link winsock2 (MinGW only)
  5233. --with-libc_r link libc_r if possible (FreeBSD only)
  5234. --with-default-kcode=CODE specify default value for \$KCODE (utf8|euc|sjis|none)
  5235. @@ -2080,6 +2082,35 @@
  5236. _ACEOF
  5237. fi
  5238. +# Check whether --enable-mbari-api was given.
  5239. +if test "${enable_mbari_api+set}" = set; then
  5240. + enableval=$enable_mbari_api; mbari_api=$enableval
  5241. +fi
  5242. +
  5243. +if test "$mbari_api" = yes; then
  5244. + cat >>confdefs.h <<\_ACEOF
  5245. +#define MBARI_API 1
  5246. +_ACEOF
  5247. +
  5248. +fi
  5249. +
  5250. +# Check whether --with-wipe-sites was given.
  5251. +if test "${with_wipe_sites+set}" = set; then
  5252. + withval=$with_wipe_sites; wipe_sites=$withval
  5253. +fi
  5254. +
  5255. +if test "$wipe_sites" != ""; then
  5256. + case $wipe_sites in
  5257. + none|no) wipe_sites=0x0;;
  5258. + yes) wipe_sites=;;
  5259. + esac
  5260. + if test -n "$wipe_sites"; then
  5261. + cat >>confdefs.h <<_ACEOF
  5262. +#define STACK_WIPE_SITES $wipe_sites
  5263. +_ACEOF
  5264. +
  5265. + fi
  5266. +fi
  5267. test "$program_prefix" != NONE &&
  5268. program_transform_name="s&^&$program_prefix&;$program_transform_name"