restart.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314
  1. /* restart.c Copyright (C) 1989-1999 Codemist Ltd */
  2. /*
  3. * Code needed to start off Lisp when no initial heap image is available,
  4. * or to re-instate links between heap and C-coded core if there IS a
  5. * heap loaded. This code is run in a state that is in effect (in-package
  6. * "lisp").
  7. */
  8. /* Signature: 2c97b73c 07-Mar-2000 */
  9. #include <stdarg.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <float.h>
  13. #include "machine.h"
  14. #include "version.h"
  15. #include "tags.h"
  16. #include "cslerror.h"
  17. #include "externs.h"
  18. #include "read.h"
  19. #include "stream.h"
  20. #include "arith.h"
  21. #include "entries.h"
  22. #ifdef TIMEOUT
  23. #include "timeout.h"
  24. #endif
  25. #ifdef ADDRESS_SIGN_UNKNOWN
  26. int32 address_sign;
  27. #endif
  28. Lisp_Object C_nil;
  29. Lisp_Object *stackbase;
  30. Lisp_Object *nilsegment;
  31. Lisp_Object *stacksegment;
  32. int32 stack_segsize = 1;
  33. char *exit_charvec = NULL;
  34. #ifdef NILSEG_EXTERNS
  35. unsigned32 byteflip;
  36. Lisp_Object codefringe;
  37. Lisp_Object volatile codelimit;
  38. Lisp_Object * volatile stacklimit;
  39. Lisp_Object fringe;
  40. Lisp_Object volatile heaplimit;
  41. Lisp_Object volatile vheaplimit;
  42. Lisp_Object vfringe;
  43. int32 nwork;
  44. int32 exit_reason;
  45. int32 exit_count;
  46. unsigned32 gensym_ser, print_precision, miscflags;
  47. int32 current_modulus, fastget_size, package_bits;
  48. Lisp_Object lisp_true, lambda, funarg, unset_var, opt_key, rest_key;
  49. Lisp_Object quote_symbol, function_symbol, comma_symbol, comma_at_symbol;
  50. Lisp_Object cons_symbol, eval_symbol, work_symbol, evalhook, applyhook;
  51. Lisp_Object macroexpand_hook, append_symbol, exit_tag;
  52. Lisp_Object exit_value, catch_tags;
  53. #ifdef COMMON
  54. Lisp_Object keyword_package;
  55. #endif
  56. Lisp_Object current_package;
  57. Lisp_Object startfn;
  58. #ifdef COMMON
  59. Lisp_Object all_packages, package_symbol, internal_symbol;
  60. Lisp_Object external_symbol, inherited_symbol;
  61. #endif
  62. Lisp_Object gensym_base, string_char_sym, boffo;
  63. #ifdef COMMON
  64. Lisp_Object key_key, allow_other_keys, aux_key;
  65. #endif
  66. Lisp_Object err_table;
  67. #ifdef COMMON
  68. Lisp_Object format_symbol;
  69. #endif
  70. Lisp_Object progn_symbol;
  71. #ifdef COMMON
  72. Lisp_Object expand_def_symbol, allow_key_key, declare_symbol, special_symbol;
  73. #endif
  74. Lisp_Object lisp_work_stream, charvec, raise_symbol, lower_symbol, echo_symbol;
  75. Lisp_Object codevec, litvec, supervisor, B_reg, savedef, comp_symbol;
  76. Lisp_Object compiler_symbol, faslvec, tracedfn, lisp_terminal_io;
  77. Lisp_Object lisp_standard_output, lisp_standard_input, lisp_error_output;
  78. Lisp_Object lisp_trace_output, lisp_debug_io, lisp_query_io;
  79. Lisp_Object prompt_thing, faslgensyms, prinl_symbol, emsg_star, redef_msg;
  80. Lisp_Object expr_symbol, fexpr_symbol, macro_symbol;
  81. Lisp_Object cl_symbols, active_stream, current_module;
  82. Lisp_Object features_symbol, lisp_package, sys_hash_table;
  83. Lisp_Object help_index, cfunarg, lex_words, get_counts, fastget_names;
  84. Lisp_Object input_libraries, output_library, current_file, break_function;
  85. Lisp_Object standard_output, standard_input, debug_io;
  86. Lisp_Object error_output, query_io, terminal_io, trace_output, fasl_stream;
  87. Lisp_Object native_code, native_symbol, traceprint_symbol, loadsource_symbol;
  88. Lisp_Object hankaku_symbol;
  89. Lisp_Object workbase[51];
  90. #endif
  91. Lisp_Object user_base_0, user_base_1, user_base_2, user_base_3, user_base_4;
  92. Lisp_Object user_base_5, user_base_6, user_base_7, user_base_8, user_base_9;
  93. Lisp_Object eq_hash_tables, equal_hash_tables;
  94. /*
  95. * On an Intel 80x86 (because I am almost forced to) and on other machines
  96. * (much more cheerfully, and for choice!) I will arrange my memory as
  97. * a number of pages. A general pool of these pages gets used
  98. * to satisfy requests for heap, vector heap and BPS space. Running
  99. * under the Phar Lap 286 DOS extender in protected mode it seemed that
  100. * all segments returned by malloc() will have their top bit (when punned
  101. * as a long int) zero. Thus even on an 80x86 I could make progress..
  102. * Ditto under the Zortech (Rational Systems) 80286 extender. Win32
  103. * (Windows NT) seems OK too, but Win32s has given me pointers with
  104. * their top bit set... Ugh! For the 80286 and small machines the pages
  105. * will be just 64 Kbytes - for larger machines I will make them a bit
  106. * bigger (typically 256 Kbytes). See PAGE_BITS in machine.h.
  107. *
  108. * Since this code was first written it has become silly to even consider
  109. * computers with 16-bit segmented addressing! It is still convenient to
  110. * allocate memory in chunks, although that does set an upper limit to the
  111. * size of any individual object: this may hurt if a user wants a big vector
  112. * and it does constrain the range of big-numbers supported by the
  113. * artithmetic.
  114. */
  115. void **pages,
  116. **heap_pages,
  117. **vheap_pages,
  118. **bps_pages,
  119. **native_pages;
  120. #ifndef NO_COPYING_GC
  121. void **new_heap_pages,
  122. **new_vheap_pages,
  123. **new_bps_pages,
  124. **new_native_pages;
  125. #endif
  126. int32 pages_count,
  127. heap_pages_count,
  128. vheap_pages_count,
  129. bps_pages_count,
  130. native_pages_count;
  131. #ifndef NO_COPYING_GC
  132. int32 new_heap_pages_count,
  133. new_vheap_pages_count,
  134. new_bps_pages_count,
  135. new_native_pages_count;
  136. #endif
  137. char program_name[64] = {0};
  138. #ifndef COMMON
  139. #ifdef CWIN
  140. /*
  141. * Could be useful for other windowed systems.
  142. */
  143. char **loadable_packages, **switches;
  144. #endif
  145. #endif
  146. int native_code_tag;
  147. int native_pages_changed;
  148. int32 native_fringe;
  149. int current_fp_rep;
  150. static int old_fp_rep;
  151. static CSLbool flip_needed;
  152. static int old_page_bits;
  153. /*
  154. * The next function is handed a page
  155. * of hard code that has just been loaded into memory and it must scan it
  156. * performing all relevant relocation. fringe give the offset within the
  157. * page that is the first byte not in use. The first 4 bytes of the page
  158. * are reserved for storing fringe from one run to the next. The exact
  159. * format of the rest must be sufficient to allow this code to scan
  160. * and correct the code, but thus far I have not defined it, and it will
  161. * anyway tend to need extension each time a new target architecture is
  162. * incorporated (to support the new and curious relocation modes tha the
  163. * new machine requires).
  164. */
  165. static void relocate_native_code(unsigned char *p, int32 n)
  166. {
  167. /*
  168. * One helpful observation here. In pretty well all other parts of CSL
  169. * there is a possibility that an image file created on one computer will
  170. * be reloaded on another and so all the code is ultra-careful to avoid
  171. * sensitivity to byte order etc etc issues. But here the native code that
  172. * is being loaded MUST have been created using the conventions of the
  173. * current computer (otherwise I should not be loading it and I will be
  174. * in huge trouble when I try to execute code from it). So direct and
  175. * simple access to data is legitimate.
  176. */
  177. int32 k = 8;
  178. term_printf("Native code page type %d size %d to be relocated\n",
  179. native_code_tag, n);
  180. while (k <= n)
  181. { unsigned char *block = p + k;
  182. int32 len = *(int32 *)block;
  183. term_printf("Block of %d bytes found\n", len);
  184. if (len == 0)
  185. { term_printf("End of native page reached\n");
  186. break;
  187. }
  188. relocate_native_function(block);
  189. k += len;
  190. }
  191. }
  192. void relocate_native_function(unsigned char *bps)
  193. {
  194. /*
  195. * Each chunk of memory allocated by make-native will have its length (in
  196. * bytes) in its first 32-bit word. Next comes the offset of the
  197. * start of real code in the block. Just after that there will be a
  198. * hunk of relocation information. The code proper must not start until
  199. * after the relocation records. Relocation information is stored in the
  200. * following format as a sequence of bytes:
  201. * 0 end of relocation information.
  202. * 1 to 170/xx encode a value 0 to 169
  203. * 171 to 255/xx/yy extra byte yy extends following offset xx, and
  204. * its top bit is used to extend opcode to range
  205. * 0 to 169.
  206. * The opcode now in the range 0 to 169 is interpreted as
  207. * 169 no operation
  208. * otherwise (0-12)*(0-12) as target*mode
  209. */
  210. unsigned char *r = bps + 4;
  211. unsigned char *next;
  212. int32 n;
  213. int code;
  214. n = *r++; /* code start offset in LSB format */
  215. n |= (*r++) << 8;
  216. n |= (*r++) << 16;
  217. n |= (*r++) << 24;
  218. next = bps + n;
  219. #define RELOC_END 0
  220. while ((code = *r++) != RELOC_END)
  221. { int32 off = *r++;
  222. unsigned char *target;
  223. /*
  224. * A native compiler will have to generate a sequence of bytes that adhere to
  225. * the contorted format used here.
  226. */
  227. if (code <= 170) code--;
  228. else
  229. { int off1 = *r++;
  230. code = 2*(code-171) + (off1 >> 7);
  231. off = off | ((off1 & 0x7f) << 8);
  232. }
  233. next += off; /* address where next relocation is to be applied */
  234. #define RELOC_NOP 169
  235. /*
  236. * One might like to note that with a long offset the NOP opcode turns into
  237. * an opcode byte 0xff. And if it then has the longest possible offset one]
  238. * gets the 3-byte sequence 0xff/0xff/0xff.
  239. */
  240. if (code == RELOC_NOP) continue;
  241. #define RELOC_0_ARGS 0
  242. #define RELOC_1_ARGS 1
  243. #define RELOC_2_ARGS 2
  244. #define RELOC_3_ARGS 3
  245. #define RELOC_DIRECT_ENTRY 4
  246. #define RELOC_VAR 5
  247. #define RELOC_SELF_1 6
  248. #define RELOC_SELF_2 7
  249. switch (code % 13)
  250. {
  251. default:
  252. term_printf("Illegal relocation byte %.2x\n", code);
  253. my_exit(EXIT_FAILURE);
  254. case RELOC_SELF_1:
  255. /*
  256. * base of current native code block with an 8-bit offset.
  257. */
  258. target = bps + *r++;
  259. break;
  260. case RELOC_SELF_2:
  261. /*
  262. * base of current native code block with 15 or 23-bit offset. The first byte
  263. * is the low 8-bits of the offset. The next is the next 7 bits, with its
  264. * 0x80 bit selecting whether a third byte is needed (which it will hardly
  265. * ever be).
  266. */
  267. off = *r++;
  268. off = off + (*r++ << 8);
  269. if (off & 0x8000) off = (off & 0x7fff) + (*r++ << 15);
  270. target = bps + off;
  271. break;
  272. case RELOC_0_ARGS:
  273. /*
  274. * The next few relocation modes provide access to the C entrypoints
  275. * associated with a medium number of Lisp functions. The tables and
  276. * offsets used are documented in file "eval4.c" and are as used with the
  277. * byte-code compiler.
  278. */
  279. target = (unsigned char *)zero_arg_functions[*r++];
  280. break;
  281. case RELOC_1_ARGS:
  282. target = (unsigned char *)one_arg_functions[*r++];
  283. break;
  284. case RELOC_2_ARGS:
  285. target = (unsigned char *)two_arg_functions[*r++];
  286. break;
  287. case RELOC_3_ARGS:
  288. target = (unsigned char *)three_arg_functions[*r++];
  289. break;
  290. case RELOC_DIRECT_ENTRY:
  291. /*
  292. * There are some entrypoints into the CSL kernel that are not
  293. * called using the usual Lisp conventions but are at a lower-level.
  294. * A selection of these are visible via the table "useful_functions"
  295. * in file fns3.c. This table can be extended if a native-mode compiler
  296. * needs access to any other speciality.
  297. */
  298. target = (unsigned char *)useful_functions[*r++];
  299. break;
  300. case RELOC_VAR:
  301. /*
  302. * The function address_f_var (in fns3.c) returns the address of a Lisp
  303. * internal variable. See there for the numeric encoding used and what can
  304. * be accessed.
  305. */
  306. target = (unsigned char *)address_of_var(*r++);
  307. break;
  308. }
  309. #define RELMODE_ABSOLUTE 0
  310. #define RELMODE_RELATIVE 1
  311. #define RELMODE_REL_PLUS_4 2
  312. #define RELMODE_REL_MINUS_2 3
  313. #define RELMODE_REL_MINUS_4 4
  314. #define RELMODE_REL_OFFSET 5
  315. #define RELMODE_SPARE1 6
  316. #define RELMODE_SPARE2 7
  317. switch (code/13)
  318. {
  319. default:
  320. term_printf("Illegal relocation byte %.2x\n", code);
  321. my_exit(EXIT_FAILURE);
  322. case RELMODE_ABSOLUTE:
  323. /*
  324. * relocate by pointing a 32-bit value directly at the absolute address
  325. * of the target.
  326. */
  327. *(int32 *)next = (int32)target;
  328. break;
  329. case RELMODE_RELATIVE:
  330. /*
  331. * relocate by setting a 32-bit value of the offset from its own first
  332. * byte to the target.
  333. */
  334. *(int32 *)next = (int32)target - (int32)next;
  335. break;
  336. case RELMODE_REL_PLUS_4:
  337. /*
  338. * relocate by setting a 32-bit value of the offset from the start of the
  339. * word after it.
  340. */
  341. *(int32 *)next = (int32)target - ((int32)next + 4);
  342. break;
  343. case RELMODE_REL_MINUS_2:
  344. /*
  345. * relocate by setting a 32-bit value of the offset from the address 2 bytes
  346. * before its start. This may be used on machines where the relative address
  347. * is computed based on the start of the instruction rather than the start of
  348. * the field within the instruction that contains the offset.
  349. */
  350. *(int32 *)next = (int32)target - ((int32)next - 2);
  351. break;
  352. case RELMODE_REL_MINUS_4:
  353. /*
  354. * relocate by setting a 32-bit value of the offset from the address 4 bytes
  355. * before its start. This may be used on machines where the relative address
  356. * is computed based on the start of the instruction rather than the start of
  357. * the field within the instruction that contains the offset.
  358. */
  359. *(int32 *)next = (int32)target - ((int32)next - 4);
  360. break;
  361. case RELMODE_REL_OFFSET:
  362. /*
  363. * relocate by setting a 32-bit value of the offset from some place
  364. * offset using an 8-bit signed value from the start of the address. The
  365. * offset represents the number of bytes after the start of the address
  366. * that is to be used in the calculation. Note that the special values
  367. * -4, -2, 0 and 4 need never be used here because there are special
  368. * relocation modes for those common cases.
  369. */
  370. code = *r++;
  371. if (code & 0x80) code |= ~0xff; /* Sign extend */
  372. *(int32 *)next = (int32)target - ((int32)next + code);
  373. break;
  374. }
  375. }
  376. }
  377. static int32 fread_count;
  378. static unsigned char *fread_ptr;
  379. #ifdef SIXTEEN_BIT
  380. #define FREAD_BUFFER_SIZE 0x4000 /* 16 Kbytes of buffer */
  381. #else
  382. #define FREAD_BUFFER_SIZE 0xc000 /* 48 Kbytes of buffer */
  383. #endif
  384. static unsigned char *pair_c, *char_stack;
  385. static unsigned short int *pair_prev;
  386. static void Cfread(char *p, int32 n)
  387. {
  388. /*
  389. * The decompression process does not need hashed access to see if
  390. * character-pairs have been seen before, but it can need a stack to
  391. * unwind codes that have very lengthy expansions.
  392. */
  393. int c1, k;
  394. unsigned int prev, c, next_code;
  395. int32 count = fread_count;
  396. unsigned char *ptr = fread_ptr;
  397. if (n < compression_worth_while)
  398. {
  399. #ifdef MAYBE_AVOID_A_DATA_COPY_OPERATION
  400. if (n > count)
  401. { memcpy(p, ptr, (size_t)count);
  402. p += count;
  403. n -= count;
  404. }
  405. while (n >= FREAD_BUFFER_SIZE)
  406. { count = Iread((unsigned char *)p, FREAD_BUFFER_SIZE);
  407. p += count;
  408. n -= count;
  409. }
  410. /*
  411. * Here I believe that I am in the clear using the (Lisp) stack as
  412. * a buffer area. This is because Cfread is only used to read heap images,
  413. * and when such an image is being loaded the contents of the old one
  414. * (including the stack) are not too relevant.
  415. */
  416. ptr = (unsigned char *)stack;
  417. count = Iread(ptr, FREAD_BUFFER_SIZE);
  418. #endif
  419. while (n > count)
  420. { memcpy(p, ptr, (size_t)count);
  421. p += count;
  422. n -= count;
  423. ptr = (unsigned char *)stack;
  424. count = Iread(ptr, FREAD_BUFFER_SIZE);
  425. }
  426. if (n != 0)
  427. { memcpy(p, ptr, (size_t)n);
  428. ptr += n;
  429. count -= n;
  430. }
  431. fread_count = count;
  432. fread_ptr = ptr;
  433. return;
  434. }
  435. next_code = 256;
  436. if (count == 0)
  437. { ptr = (unsigned char *)stack;
  438. count = Iread(ptr, FREAD_BUFFER_SIZE);
  439. }
  440. c = *ptr++;
  441. count--;
  442. if (count == 0)
  443. { ptr = (unsigned char *)stack;
  444. count = Iread(ptr, FREAD_BUFFER_SIZE);
  445. }
  446. c = (c << 8) | *ptr++;
  447. count--;
  448. prev = c >> 4;
  449. *p++ = (char)prev; /* The first character is not compressed */
  450. n--;
  451. while (n > 0)
  452. { if (count == 0)
  453. { ptr = (unsigned char *)stack;
  454. count = Iread(ptr, FREAD_BUFFER_SIZE);
  455. }
  456. c = ((c & 0xf) << 8) | *ptr++;
  457. count--;
  458. /*
  459. * Decode the next 12 bit character
  460. */
  461. c1 = c;
  462. k = 1;
  463. while (c1 >= 256)
  464. { char_stack[k++] = pair_c[c1];
  465. if (pair_prev[c1] > CODESIZE || k >= CODESIZE)
  466. { term_printf("Bad decoded char %x -> %x, k=%d\n", c1, pair_prev[c1], k);
  467. my_exit(EXIT_FAILURE);
  468. }
  469. c1 = pair_prev[c1];
  470. }
  471. /*
  472. * Write the decoded stuff into the output array.
  473. */
  474. n -= k;
  475. *p++ = (char)c1;
  476. while (k != 1)
  477. { *p++ = char_stack[--k];
  478. }
  479. /*
  480. * ... then build up the decoding tables ready for next time. Of course
  481. * the table building in this decoder MUST exactly match the behaviour of
  482. * the compression code above.
  483. */
  484. if (next_code >= CODESIZE) next_code = 256;
  485. else
  486. { pair_prev[next_code] = (unsigned short int)prev;
  487. pair_c[next_code] = c1;
  488. next_code++;
  489. }
  490. prev = c;
  491. if (n <= 0) break;
  492. /*
  493. * read the next 12 bit character.
  494. */
  495. if (count == 0)
  496. { ptr = (unsigned char *)stack;
  497. count = Iread(ptr, FREAD_BUFFER_SIZE);
  498. }
  499. c = *ptr++;
  500. count--;
  501. if (count == 0)
  502. { ptr = (unsigned char *)stack;
  503. count = Iread(ptr, FREAD_BUFFER_SIZE);
  504. }
  505. c = (c << 8) | *ptr++;
  506. count--;
  507. /*
  508. * Decode it...
  509. */
  510. c1 = c >> 4;
  511. k = 1;
  512. while (c1 >= 256)
  513. { char_stack[k++] = pair_c[c1];
  514. if (pair_prev[c1] > CODESIZE || k >= CODESIZE)
  515. { term_printf("Bad decoded char %x -> %x, k=%d\n", c1, pair_prev[c1], k);
  516. my_exit(EXIT_FAILURE);
  517. }
  518. c1 = pair_prev[c1];
  519. }
  520. /*
  521. * Write the decoded stuff into the output array.
  522. */
  523. n -= k;
  524. *p++ = (char)c1;
  525. while (k != 1)
  526. { *p++ = char_stack[--k];
  527. }
  528. /*
  529. * ... then build up the decoding tables ready for next time. Of course
  530. * the table building in this decoder MUST exactly match the behaviour of
  531. * the compression code above.
  532. */
  533. if (next_code >= CODESIZE) next_code = 256;
  534. else
  535. { pair_prev[next_code] = (unsigned short int)prev;
  536. pair_c[next_code] = c1;
  537. next_code++;
  538. }
  539. prev = c >> 4;
  540. }
  541. fread_count = count;
  542. fread_ptr = ptr;
  543. }
  544. #define flip_bytes(a) (flip_needed ? flip_bytes_fn(a) : (a))
  545. static unsigned32 flip_bytes_fn(unsigned32 x)
  546. {
  547. unsigned32 b0, b1, b2, b3;
  548. b0 = (x >> 24) & 0xffU;
  549. b1 = (x >> 8) & 0xff00U;
  550. b2 = (x << 8) & 0xff0000U;
  551. b3 = (x << 24) & 0xff000000U;
  552. return b0 | b1 | b2 | b3;
  553. }
  554. #define flip_halfwords(a) (flip_needed ? flip_halfwords_fn(a) : (a))
  555. static unsigned32 flip_halfwords_fn(unsigned32 x)
  556. {
  557. unsigned32 b0, b1, b2, b3;
  558. b0 = (x >> 8) & 0xffU;
  559. b1 = (x << 8) & 0xff00U;
  560. b2 = (x >> 8) & 0xff0000U;
  561. b3 = (x << 8) & 0xff000000U;
  562. return b0 | b1 | b2 | b3;
  563. }
  564. void convert_fp_rep(void *p, int old_rep, int new_rep, int type)
  565. {
  566. unsigned32 *f = (unsigned32 *)p;
  567. if (old_rep == new_rep) return;
  568. /*
  569. * type == 0 for sfloat, 1 for single float, 2 for double and 3 for extended.
  570. * in CSL mode only case 2 can arise.
  571. */
  572. /*
  573. * At present conversions involving IBM370 or VAX representations are not
  574. * supported, i.e. only IEEE floating point units are allowed for.
  575. * Extended mode is not implemented yet... i.e. I only really expect
  576. * to be using (64-bit) double precision floating point values.
  577. */
  578. if (type >= 2 && ((old_rep ^ new_rep) & FP_WORD_ORDER))
  579. { unsigned32 w = f[0];
  580. f[0] = f[1];
  581. f[1] = w;
  582. }
  583. if ((old_rep ^ new_rep) & FP_BYTE_ORDER)
  584. { f[0] = flip_bytes_fn(f[0]);
  585. if (type >= 2) f[1] = flip_bytes_fn(f[1]);
  586. }
  587. if ((old_rep|new_rep) & (FP_VAXREP|FP_IBMREP))
  588. { term_printf(
  589. "\n+++ This cross-floating representation conversion not supported\n");
  590. /*
  591. * I have not implemented conversions that involve VAX or s/370 representations
  592. */
  593. my_exit(EXIT_FAILURE);
  594. }
  595. return;
  596. }
  597. static void adjust(Lisp_Object *cp)
  598. /*
  599. * If p is a pointer to an object that has moved, adjust it.
  600. */
  601. {
  602. Lisp_Object nil = C_nil, p = flip_bytes(*cp);
  603. if (p == SPID_NIL) *cp = nil;
  604. else if (is_cons(p))
  605. { int32 h = (int32)heap_pages[(p>>PAGE_BITS) & PAGE_MASK];
  606. *cp = (Lisp_Object)((char *)doubleword_align_up(h) +
  607. (p & OFFSET_MASK));
  608. }
  609. else if (is_immed_or_cons(p))
  610. {
  611. #ifdef COMMON
  612. if (is_sfloat(p))
  613. { int32 w = flip_bytes(p); /* delicate here!! */
  614. convert_fp_rep((void *)&w, old_fp_rep, current_fp_rep, 0);
  615. *cp = w;
  616. }
  617. #endif
  618. *cp = p; /* Immediate data here */
  619. }
  620. else
  621. { int32 h = (int32)vheap_pages[(p>>PAGE_BITS) & PAGE_MASK];
  622. *cp = (Lisp_Object)((char *)doubleword_align_up(h) +
  623. (p & OFFSET_MASK));
  624. }
  625. }
  626. static void adjust_consheap(void)
  627. {
  628. nil_as_base
  629. int32 page_number;
  630. for (page_number = 0; page_number < heap_pages_count; page_number++)
  631. { void *page = heap_pages[page_number];
  632. char *low = (char *)doubleword_align_up((int32)page);
  633. char *start = low + CSL_PAGE_SIZE;
  634. int32 len = flip_bytes(qcar(low));
  635. char *fr;
  636. qcar(low) = len;
  637. fr = low + len;
  638. fringe = (Lisp_Object)fr;
  639. heaplimit = (Lisp_Object)(low + SPARE);
  640. while (fr < start)
  641. { adjust((Lisp_Object *)fr);
  642. fr += sizeof(Lisp_Object);
  643. }
  644. }
  645. }
  646. entry_point entries_table[] =
  647. {
  648. /*
  649. * All values that can go in the function cells of symbols to stand for
  650. * special interpreter activity are kept here. In most cases where there
  651. * is an entrypoint there is a corresponding one that behaves just the
  652. * same except that it has tracing enabled.
  653. */
  654. {0, "xillegal"},
  655. {(void *)undefined1, "1undefined1"},
  656. {(void *)undefined2, "2undefined2"},
  657. {(void *)undefinedn, "nundefinedn"},
  658. {(void *)autoload1, "1autoload1"},
  659. {(void *)autoload2, "2autoload2"},
  660. {(void *)autoloadn, "nautoloadn"},
  661. {(void *)interpreted1, "1interpreted1"},
  662. {(void *)traceinterpreted1, "1traceinterpreted1"},
  663. {(void *)double_interpreted1, "1double_interpreted1"},
  664. {(void *)interpreted2, "2interpreted2"},
  665. {(void *)traceinterpreted2, "2traceinterpreted2"},
  666. {(void *)double_interpreted2, "2double_interpreted2"},
  667. {(void *)interpretedn, "ninterpretedn"},
  668. {(void *)traceinterpretedn, "ntraceinterpretedn"},
  669. {(void *)double_interpretedn, "ndouble_interpretedn"},
  670. {(void *)funarged1, "1funarged1"},
  671. {(void *)tracefunarged1, "1tracefunarged1"},
  672. {(void *)double_funarged1, "1double_funarged1"},
  673. {(void *)funarged2, "2funarged2"},
  674. {(void *)tracefunarged2, "2tracefunarged2"},
  675. {(void *)double_funarged2, "2double_funarged2"},
  676. {(void *)funargedn, "nfunargedn"},
  677. {(void *)tracefunargedn, "ntracefunargedn"},
  678. {(void *)double_funargedn, "ndouble_funargedn"},
  679. {(void *)bytecoded0, "nbytecoded0"},
  680. {(void *)tracebytecoded0, "ntracebytecoded0"},
  681. {(void *)double_bytecoded0, "ndouble_bytecoded0"},
  682. {(void *)bytecoded1, "1bytecoded1"},
  683. {(void *)tracebytecoded1, "1tracebytecoded1"},
  684. {(void *)double_bytecoded1, "1double_bytecoded1"},
  685. {(void *)bytecoded2, "2bytecoded2"},
  686. {(void *)tracebytecoded2, "2tracebytecoded2"},
  687. {(void *)double_bytecoded2, "2double_bytecoded2"},
  688. {(void *)bytecoded3, "nbytecoded3"},
  689. {(void *)tracebytecoded3, "ntracebytecoded3"},
  690. {(void *)double_bytecoded3, "ndouble_bytecoded3"},
  691. {(void *)bytecodedn, "nbytecodedn"},
  692. {(void *)tracebytecodedn, "ntracebytecodedn"},
  693. {(void *)double_bytecodedn, "ndouble_bytecodedn"},
  694. {(void *)byteopt1, "1byteopt1"},
  695. {(void *)tracebyteopt1, "1tracebyteopt1"},
  696. {(void *)double_byteopt1, "1double_byteopt1"},
  697. {(void *)byteopt2, "2byteopt2"},
  698. {(void *)tracebyteopt2, "2tracebyteopt2"},
  699. {(void *)double_byteopt2, "2double_byteopt2"},
  700. {(void *)byteoptn, "nbyteoptn"},
  701. {(void *)tracebyteoptn, "ntracebyteoptn"},
  702. {(void *)double_byteoptn, "ndouble_byteoptn"},
  703. {(void *)hardopt1, "1hardopt1"},
  704. {(void *)tracehardopt1, "1tracehardopt1"},
  705. {(void *)double_hardopt1, "1double_hardopt1"},
  706. {(void *)hardopt2, "2hardopt2"},
  707. {(void *)tracehardopt2, "2tracehardopt2"},
  708. {(void *)double_hardopt2, "2double_hardopt2"},
  709. {(void *)hardoptn, "nhardoptn"},
  710. {(void *)tracehardoptn, "ntracehardoptn"},
  711. {(void *)double_hardoptn, "ndouble_hardoptn"},
  712. {(void *)byteoptrest1, "1byteoptrest1"},
  713. {(void *)tracebyteoptrest1, "1tracebyteoptrest1"},
  714. {(void *)double_byteoptrest1, "1double_byteoptrest1"},
  715. {(void *)byteoptrest2, "2byteoptrest2"},
  716. {(void *)tracebyteoptrest2, "2tracebyteoptrest2"},
  717. {(void *)double_byteoptrest2, "2double_byteoptrest2"},
  718. {(void *)byteoptrestn, "nbyteoptrestn"},
  719. {(void *)tracebyteoptrestn, "ntracebyteoptrestn"},
  720. {(void *)double_byteoptrestn, "ndouble_byteoptrestn"},
  721. {(void *)hardoptrest1, "1hardoptrest1"},
  722. {(void *)tracehardoptrest1, "1tracehardoptrest1"},
  723. {(void *)double_hardoptrest1, "1double_hardoptrest1"},
  724. {(void *)hardoptrest2, "2hardoptrest2"},
  725. {(void *)tracehardoptrest2, "2tracehardoptrest2"},
  726. {(void *)double_hardoptrest2, "2double_hardoptrest2"},
  727. {(void *)hardoptrestn, "nhardoptrestn"},
  728. {(void *)tracehardoptrestn, "ntracehardoptrestn"},
  729. {(void *)double_hardoptrestn, "ndouble_hardoptrestn"},
  730. {(void *)too_many_1, "2too_many_1"},
  731. {(void *)wrong_no_1, "nwrong_no_1"},
  732. {(void *)too_few_2, "1too_few_2"},
  733. {(void *)wrong_no_2, "nwrong_no_2"},
  734. {(void *)wrong_no_0a, "1wrong_no_0a"},
  735. {(void *)wrong_no_0b, "2wrong_no_0b"},
  736. {(void *)wrong_no_3a, "1wrong_no_3a"},
  737. {(void *)wrong_no_3b, "2wrong_no_3b"},
  738. {(void *)wrong_no_na, "1wrong_no_na"},
  739. {(void *)wrong_no_nb, "2wrong_no_nb"},
  740. /*
  741. * As a matter of convenience I will put stream-handling functions here
  742. * too -- they are not kept in symbol headers but in stream control
  743. * blocks, but they too need careful treatment across preserve/restart.
  744. */
  745. {(void *)char_from_illegal, "Xchar_from_illegal"},
  746. {(void *)char_to_illegal, "Xchar_to_illegal"},
  747. {(void *)read_action_illegal, "Xread_action_illegal"},
  748. {(void *)write_action_illegal, "Xwrite_action_illegal"},
  749. {(void *)char_from_terminal, "Xchar_from_terminal"},
  750. {(void *)char_to_terminal, "Xchar_to_terminal"},
  751. {(void *)read_action_terminal, "Xread_action_terminal"},
  752. {(void *)write_action_terminal, "Xwrite_action_terminal"},
  753. {(void *)char_from_file, "Xchar_from_file"},
  754. {(void *)char_to_file, "Xchar_to_file"},
  755. {(void *)read_action_file, "Xread_action_file"},
  756. {(void *)read_action_output_file, "Xread_action_output_file"},
  757. {(void *)write_action_file, "Xwrite_action_file"},
  758. {(void *)binary_outchar, "Xbinary_outchar"},
  759. {(void *)char_from_list, "Xchar_from_list"},
  760. {(void *)char_to_list, "Xchar_to_list"},
  761. {(void *)code_to_list, "Xcode_to_list"},
  762. {(void *)read_action_list, "Xread_action_list"},
  763. {(void *)write_action_list, "Xwrite_action_list"},
  764. {(void *)count_character, "Xcount_character"},
  765. {(void *)char_to_pipeout, "Xchar_to_pipeout"},
  766. {(void *)write_action_pipe, "Xwrite_action_pipe"},
  767. {(void *)char_from_synonym, "Xchar_from_synonym"},
  768. {(void *)char_to_synonym, "Xchar_to_synonym"},
  769. {(void *)read_action_synonym, "Xread_action_synonym"},
  770. {(void *)write_action_synonym, "Xwrite_action_synonym"},
  771. {(void *)char_from_concatenated, "Xchar_from_concatenated"},
  772. {(void *)char_to_broadcast, "Xchar_to_broadcast"},
  773. {(void *)read_action_concatenated, "Xread_action_concatenated"},
  774. {(void *)write_action_broadcast, "Xwrite_action_broadcast"},
  775. {(void *)char_from_echo, "Xchar_from_echo"},
  776. /*
  777. * The batch here relate to function re-work that discards unwanted
  778. * extra arguments.
  779. */
  780. {(void *)f0_as_0, "n0->0"},
  781. {(void *)f1_as_0, "11->0"},
  782. {(void *)f2_as_0, "22->0"},
  783. {(void *)f3_as_0, "n3->0"},
  784. {(void *)f1_as_1, "11->1"},
  785. {(void *)f2_as_1, "22->1"},
  786. {(void *)f3_as_1, "n3->1"},
  787. {(void *)f2_as_2, "22->2"},
  788. {(void *)f3_as_2, "n3->2"},
  789. {(void *)f3_as_3, "n3->3"},
  790. #ifdef CJAVA
  791. {(void *)java0, "njava0"},
  792. {(void *)java1, "1java1"},
  793. {(void *)java2, "2java2"},
  794. {(void *)java3, "njava3"},
  795. {(void *)javan, "njavan"},
  796. #endif
  797. {NULL, "Xdummy"}
  798. };
  799. static void adjust_vecheap(void)
  800. {
  801. nil_as_base
  802. int32 page_number, i;
  803. for (page_number = 0; page_number < vheap_pages_count; page_number++)
  804. { void *page = vheap_pages[page_number];
  805. char *low = (char *)doubleword_align_up((int32)page);
  806. int32 len = flip_bytes(qcar(low));
  807. char *fr;
  808. qcar(low) = len;
  809. fr = low + len;
  810. vfringe = (Lisp_Object)fr;
  811. vheaplimit = (Lisp_Object)(low + (CSL_PAGE_SIZE - 8));
  812. low += 8;
  813. while (low < fr)
  814. { Header h = flip_bytes(*(Header *)low);
  815. *(Header *)low = h;
  816. if (is_symbol_header(h))
  817. { Lisp_Object ss = (Lisp_Object)(low + TAG_SYMBOL);
  818. adjust(&qvalue(ss));
  819. adjust(&qenv(ss));
  820. adjust(&qpname(ss));
  821. adjust(&qplist(ss));
  822. adjust(&qfastgets(ss));
  823. #ifdef COMMON
  824. adjust(&qpackage(ss));
  825. #endif
  826. /*
  827. * The mess here is because when CSL is re-loaded the position of all
  828. * C-coded entrypoints will very probably have changed since the
  829. * previous run - the set of entrypoints tested for here has to be
  830. * a complete list, except for ones established via "restart.c". Note
  831. * that setup establishes entrypoints later on, so I can afford to leave
  832. * junk in the function cells of things that will be initialised then.
  833. * Thus if a "real" function pointer left over from last time happens
  834. * to look like one of the small integers used here to stand for special
  835. * built-in cases the false-hit I get here is not important.
  836. */
  837. i = flip_bytes(ifn1(ss));
  838. /*
  839. * Another delicacy - somebody intent on cleaning up this code may spot the
  840. * cast from a function pointer to an integer here, and decide it would be
  841. * neater to make entries_table[] contain a union type that could hide the
  842. * conversion. But that MIGHT not be good enough if sizeof(void *)==8 and
  843. * sizeof(int32)==4, but it has been arranged that all pointers used have
  844. * their 32 most significant bits all zero..... And this odd case can be
  845. * persuaded to apply on a DEC Alpha! The cast as written here really does
  846. * change data representation, but with luck does not lose any vital
  847. * information.
  848. */
  849. if (0 < i && i < entry_table_size)
  850. { if (*entries_table[i].s != '1')
  851. { term_printf("?fn1 cell %d %s\n", i, entries_table[i].s);
  852. }
  853. ifn1(ss) = (int32)entries_table[i].p;
  854. }
  855. else ifn1(ss) = (int32)undefined1;
  856. i = flip_bytes(ifn2(ss));
  857. if (0 < i && i < entry_table_size)
  858. { if (*entries_table[i].s != '2')
  859. { term_printf("?fn2 cell %d %s\n", i, entries_table[i].s);
  860. }
  861. ifn2(ss) = (int32)entries_table[i].p;
  862. }
  863. else ifn2(ss) = (int32)undefined2;
  864. i = flip_bytes(ifnn(ss));
  865. if (0 < i && i < entry_table_size)
  866. { if (*entries_table[i].s != 'n')
  867. { term_printf("?fnn cell %d %s\n", i, entries_table[i].s);
  868. }
  869. ifnn(ss) = (int32)entries_table[i].p;
  870. }
  871. else ifnn(ss) = (int32)undefinedn;
  872. qcount(ss) = flip_bytes(qcount(ss));
  873. low += symhdr_length;
  874. continue;
  875. }
  876. else switch (type_of_header(h))
  877. {
  878. #ifdef COMMON
  879. case TYPE_RATNUM:
  880. case TYPE_COMPLEX_NUM:
  881. adjust((Lisp_Object *)(low+4));
  882. adjust((Lisp_Object *)(low+8));
  883. break;
  884. #endif
  885. case TYPE_HASH:
  886. case TYPE_SIMPLE_VEC:
  887. case TYPE_ARRAY:
  888. case TYPE_STRUCTURE:
  889. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  890. adjust((Lisp_Object *)(low+i));
  891. break;
  892. case TYPE_MIXED1:
  893. case TYPE_MIXED2:
  894. case TYPE_MIXED3:
  895. case TYPE_STREAM:
  896. for (i=4; i<16; i+=4) adjust((Lisp_Object *)(low+i));
  897. for (; i<doubleword_align_up(length_of_header(h)); i+=4)
  898. *(Lisp_Object *)(low+i) =
  899. flip_bytes(*(Lisp_Object *)(low+i));
  900. if (type_of_header(h) == TYPE_STREAM)
  901. { Lisp_Object ss = (Lisp_Object)(low + TAG_VECTOR);
  902. i = elt(ss, 4);
  903. if (0 < i && i < entry_table_size)
  904. { if (*entries_table[i].s != 'X')
  905. term_printf("Stream fn %d %s\n", i, entries_table[i].s);
  906. elt(ss, 4) = (int32)entries_table[i].p;
  907. }
  908. else elt(ss, 4) = (int32)char_to_illegal;
  909. i = elt(ss, 5);
  910. if (0 < i && i < entry_table_size)
  911. { if (*entries_table[i].s != 'X')
  912. term_printf("Stream fn %d %s\n", i, entries_table[i].s);
  913. elt(ss, 5) = (int32)entries_table[i].p;
  914. }
  915. else elt(ss, 5) = (int32)write_action_illegal;
  916. i = elt(ss, 8);
  917. if (0 < i && i < entry_table_size)
  918. { if (*entries_table[i].s != 'X')
  919. term_printf("Stream fn %d %s\n", i, entries_table[i].s);
  920. elt(ss, 8) = (int32)entries_table[i].p;
  921. }
  922. else elt(ss, 8) = (int32)char_from_illegal;
  923. i = elt(ss, 9);
  924. if (0 < i && i < entry_table_size)
  925. { if (*entries_table[i].s != 'X')
  926. term_printf("Stream fn %d %s\n", i, entries_table[i].s);
  927. elt(ss, 9) = (int32)entries_table[i].p;
  928. }
  929. else elt(ss, 9) = (int32)read_action_illegal;
  930. }
  931. break;
  932. case TYPE_BIGNUM:
  933. case TYPE_VEC32:
  934. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  935. *(Lisp_Object *)(low+i) =
  936. flip_bytes(*(Lisp_Object *)(low+i));
  937. break;
  938. case TYPE_VEC16:
  939. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  940. *(Lisp_Object *)(low+i) =
  941. flip_halfwords(*(Lisp_Object *)(low+i));
  942. break;
  943. case TYPE_DOUBLE_FLOAT:
  944. /*
  945. * note that this conversion is triggered by the vector header, not by
  946. * the pointer to the object, so punning associated with the pnames of
  947. * un-printed gensyms will not cause any confusion.
  948. */
  949. convert_fp_rep((void *)(low + 8L),
  950. old_fp_rep, current_fp_rep, 2);
  951. break;
  952. #ifdef COMMON
  953. case TYPE_SINGLE_FLOAT:
  954. convert_fp_rep((void *)(low + 4L),
  955. old_fp_rep, current_fp_rep, 1);
  956. break;
  957. case TYPE_LONG_FLOAT:
  958. /* Beware - if long floats move up to 3-word values the +8 here will change */
  959. convert_fp_rep((void *)(low + 8L),
  960. old_fp_rep, current_fp_rep, 3);
  961. break;
  962. #endif
  963. case TYPE_FLOAT32:
  964. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  965. convert_fp_rep((void *)(low+i),
  966. old_fp_rep, current_fp_rep, 1);
  967. break;
  968. case TYPE_FLOAT64:
  969. for (i=8; i<doubleword_align_up(length_of_header(h)); i+=8)
  970. convert_fp_rep((void *)(low+i),
  971. old_fp_rep, current_fp_rep, 2);
  972. break;
  973. default:
  974. break;
  975. }
  976. low += doubleword_align_up(length_of_header(h));
  977. }
  978. }
  979. }
  980. static void adjust_bpsheap(void)
  981. /*
  982. * This is needed so that (e.g.) headers in the code here get byte-flipped
  983. * if necessary. Also to set codefringe.
  984. */
  985. {
  986. nil_as_base
  987. int32 page_number;
  988. #ifdef ENVIRONMENT_VECTORS_IN_BPS_HEAP
  989. int32 i;
  990. #endif
  991. codelimit = codefringe = 0;
  992. for (page_number = 0; page_number < bps_pages_count; page_number++)
  993. { void *page = bps_pages[page_number];
  994. char *low = (char *)doubleword_align_up((int32)page);
  995. int32 len = flip_bytes(qcar(low));
  996. char *fr;
  997. qcar(low) = len;
  998. fr = low + len;
  999. codefringe = (Lisp_Object)fr;
  1000. codelimit = (Lisp_Object)(low + 8);
  1001. while (fr < low + CSL_PAGE_SIZE)
  1002. { Header h = flip_bytes(*(Header *)fr);
  1003. *(Header *)fr = h;
  1004. #ifdef ENVIRONMENT_VECTORS_IN_BPS_HEAP
  1005. switch (type_of_header(h))
  1006. {
  1007. case TYPE_SIMPLE_VEC: /* This option not used at present */
  1008. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  1009. adjust((Lisp_Object *)(fr+i));
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. #endif
  1015. fr += doubleword_align_up(length_of_header(h));
  1016. }
  1017. }
  1018. }
  1019. void adjust_all(void)
  1020. {
  1021. int32 i;
  1022. Lisp_Object nil = C_nil;
  1023. qheader(nil) = TAG_ODDS+TYPE_SYMBOL+SYM_SPECIAL_VAR;
  1024. #ifdef COMMON
  1025. qheader(nil) |= SYM_EXTERN_IN_HOME;
  1026. #endif
  1027. qvalue(nil) = nil;
  1028. qenv(nil) = nil;
  1029. ifn1(nil) = (int32)undefined1;
  1030. ifn2(nil) = (int32)undefined2;
  1031. ifnn(nil) = (int32)undefinedn;
  1032. adjust(&(qpname(nil))); /* not a gensym */
  1033. adjust(&(qplist(nil)));
  1034. adjust(&(qfastgets(nil)));
  1035. #ifdef COMMON
  1036. adjust(&(qpackage(nil)));
  1037. #endif
  1038. copy_into_nilseg(NO);
  1039. for (i = first_nil_offset; i<last_nil_offset; i++)
  1040. adjust(&(((Lisp_Object *)nil)[i]));
  1041. copy_out_of_nilseg(NO);
  1042. adjust_consheap();
  1043. adjust_vecheap();
  1044. adjust_bpsheap();
  1045. }
  1046. static void *allocate_page(void)
  1047. {
  1048. if (pages_count == 0) fatal_error(err_no_store);
  1049. return pages[--pages_count];
  1050. }
  1051. #ifdef MEMORY_TRACE
  1052. int32 memory_base, memory_size, memory_count, memory_records = 0;
  1053. unsigned char *memory_map = NULL;
  1054. static int32 memory_lowest = 0x7fffffff, memory_highest = -1;
  1055. FILE *memory_file = NULL;
  1056. void memory_comment(int n)
  1057. {
  1058. if (memory_map != NULL)
  1059. { putc(0xc0 + (n & 0x3f), memory_file);
  1060. putc(0, memory_file);
  1061. putc(0, memory_file);
  1062. }
  1063. }
  1064. int kk = 0;
  1065. static void identify_one(void *p, int32 size, int type)
  1066. {
  1067. int32 i, j;
  1068. int32 base = (int32)p;
  1069. int32 a = 0, b = 0;
  1070. int32 da = 1, db = 1;
  1071. int32 click = size/0x400;
  1072. switch (type)
  1073. {
  1074. case 0: b = click; break;
  1075. case 1: db = -1; break;
  1076. case 2: b = click; da = db = 2; break;
  1077. case 3: da = 2; db = -2; break;
  1078. case 4: db = 0; break;
  1079. case 5: da = -1; db = 0; break;
  1080. default: b = click; da = db = 0; break;
  1081. }
  1082. if (size > 256)
  1083. { da *= (size/256);
  1084. db *= (size/256);
  1085. }
  1086. memory_count |= 0x3ff;
  1087. cmemory_reference(base);
  1088. memory_comment(kk ? 3 : 5);
  1089. kk = !kk;
  1090. for (i=0; i<32; i++)
  1091. { int x;
  1092. memory_count |= 0x3ff;
  1093. cmemory_reference(base);
  1094. for (j=0; j<0x400; j++)
  1095. { x = a + j*(size/8);
  1096. while (x > size) x -= size;
  1097. while (x < 0) x += size;
  1098. cmemory_reference(base+x);
  1099. x = b + j*(size/8);
  1100. while (x > size) x -= size;
  1101. while (x < 0) x += size;
  1102. cmemory_reference(base+x);
  1103. }
  1104. a += da;
  1105. b += db;
  1106. }
  1107. }
  1108. static void identify_page(void *p[], int32 n, int type)
  1109. {
  1110. while (n != 0)
  1111. { void *w = p[--n];
  1112. if (w != NULL) identify_one(w, CSL_PAGE_SIZE, type);
  1113. }
  1114. }
  1115. void identify_page_types()
  1116. {
  1117. identify_page(pages, pages_count, 0);
  1118. identify_page(heap_pages, heap_pages_count, 1);
  1119. identify_page(vheap_pages, vheap_pages_count, 2);
  1120. identify_page(bps_pages, bps_pages_count, 3);
  1121. identify_page(native_pages, native_pages_count, 4);
  1122. identify_one((void *)stacksegment, CSL_PAGE_SIZE, 5);
  1123. identify_one((void *)nilsegment, NIL_SEGMENT_SIZE, 6);
  1124. }
  1125. long int car_counter;
  1126. unsigned long int car_low, car_high;
  1127. Cons_Cell *memory_reference(int32 p)
  1128. {
  1129. if (p & 0x3)
  1130. { term_printf("Access to mis-aligned address %.8x\n", p);
  1131. ensure_screen();
  1132. abort();
  1133. }
  1134. return (Cons_Cell *)cmemory_reference(p);
  1135. }
  1136. char *cmemory_reference(int32 p)
  1137. {
  1138. int32 a = p - memory_base;
  1139. #ifdef CHECK_ONLY
  1140. return (char *)p;
  1141. #endif
  1142. if (memory_map != NULL && a >= 0 && a < memory_size)
  1143. { int bit;
  1144. a = a >> 2; /* Get a word address */
  1145. a = a >> 2; /* reduce to 4-word resolution */
  1146. if (memory_count >= car_counter &&
  1147. (unsigned long int)a >= car_low &&
  1148. (unsigned long int)a <= car_high)
  1149. { Lisp_Object nil = C_nil;
  1150. if (exception_pending()) nil = (Lisp_Object)((int32)nil ^ 1);
  1151. interrupt_pending = 1;
  1152. #ifdef SOFTWARE_TICKS
  1153. countdown = 0;
  1154. #else
  1155. deal_with_tick(); /* pretend tick arrived here and now */
  1156. #endif
  1157. miscflags |= HEADLINE_FLAG | MESSAGES_FLAG;
  1158. car_counter = 0x7fffffff; /* Do not interrupt again */
  1159. }
  1160. bit = 1 << (a & 7);
  1161. a = a >> 3;
  1162. if (a < memory_lowest) memory_lowest = a;
  1163. if (a > memory_highest) memory_highest = a;
  1164. memory_map[a] |= bit;
  1165. if ((++memory_count & 0x3ff) == 0) /* Every 1024 references... */
  1166. { unsigned char *pp;
  1167. int c;
  1168. int32 run = 0, i;
  1169. /*
  1170. * I use a run-length encoded representation for the file that I write out.
  1171. * Each scan-line is stored as a collection of bytes each of which indicates
  1172. * the number of '0' items before the next '1' in the bit-vector. The encoding
  1173. * of individual lengths is as follows:
  1174. * 0 - 127 1 byte
  1175. * 128 - 16K First byte has 0x80 plus 6 bits of data (+ 1 more)
  1176. * 16K - 4M First byte has 0xc0 plus 6 bits of data (+ 2 more)
  1177. * The byte pair (0x8n, 0x00) stands for n times 4M as a a prefix to
  1178. * one of the above. This gives up to 2^28 as the max span.
  1179. * The byte pair (0x80, 0x00) can be used to terminate a line.
  1180. * Codes (0xcn, 0x00, 0x00) give 64 special codes that can be used
  1181. * to interveave comments and annotations within the stream.
  1182. */
  1183. pp = memory_map + memory_lowest;
  1184. run = 8*memory_lowest;
  1185. for (i=memory_lowest; i<=memory_highest; i++)
  1186. { c = *pp++;
  1187. if (c != 0)
  1188. { bit = 1;
  1189. while ((c & bit) == 0) run++, bit = bit << 1;
  1190. if (run >= 0x400000)
  1191. { putc(0x80 + ((run >> 22) & 0x3f), memory_file);
  1192. putc(0x00, memory_file);
  1193. run &= 0x3fffff;
  1194. }
  1195. if (run < 0x80) putc(run, memory_file);
  1196. else if (run < 0x4000)
  1197. { putc(0x80 + (run & 0x3f), memory_file);
  1198. putc((run >> 6) & 0xff, memory_file);
  1199. }
  1200. else
  1201. { putc(0xc0 + (run & 0x3f), memory_file);
  1202. putc((run >> 6) & 0xff, memory_file);
  1203. putc((run >> 14) & 0xff, memory_file);
  1204. }
  1205. c &= ~bit;
  1206. run = 0;
  1207. bit = bit << 1;
  1208. while (c != 0)
  1209. { while ((c & bit) == 0) run++, bit = bit << 1;
  1210. putc(run, memory_file);
  1211. c &= ~bit;
  1212. run = 0;
  1213. bit = bit << 1;
  1214. }
  1215. while (bit != 0x100) run++, bit = bit << 1;
  1216. }
  1217. else run += 8;
  1218. }
  1219. putc(0x80, memory_file);
  1220. putc(0x00, memory_file);
  1221. memory_lowest = 0x7fffffff;
  1222. memory_highest = -1;
  1223. memset(memory_map, 0, memory_size/32+8);
  1224. memory_records++;
  1225. }
  1226. }
  1227. return (char *)p;
  1228. }
  1229. #endif
  1230. static char *global_handle;
  1231. void *my_malloc(size_t n)
  1232. {
  1233. #ifdef NO_WORRY_ABOUT_MEMORY_PROBLEMS
  1234. return malloc(n);
  1235. #else
  1236. #define EXPLICIT_FREE_AT_END_OF_RUN 1
  1237. char *r = (char *)malloc(n+32);
  1238. int *p = (int *)doubleword_align_up(r);
  1239. if (r == NULL) return NULL;
  1240. inject_randomness((int)r);
  1241. p[0] = (int)r; /* base address for free() */
  1242. p[1] = n;
  1243. p[2] = 0x12345678; /* Marker word for security */
  1244. p[3] = 0x3456789a;
  1245. r = (char *)&p[4];
  1246. *(int *)(r+n) = 0x87654321;
  1247. *(int *)(r+n+4) = 0xcba98765;
  1248. return (void *)r;
  1249. #endif
  1250. }
  1251. #ifndef HOLD_BACK_MEMORY
  1252. static char *big_chunk_start, *big_chunk_end;
  1253. #endif
  1254. #ifdef EXPLICIT_FREE_AT_END_OF_RUN
  1255. static void my_free(void *r)
  1256. {
  1257. #ifdef NO_WORRY_ABOUT_MEMORY_PROBLEMS
  1258. #ifndef HOLD_BACK_MEMORY
  1259. char *rr = (char *)r;
  1260. /*
  1261. * I will not free it if the pointer is strictly inside the single big
  1262. * chunk that I grabbed at the start of the run.
  1263. */
  1264. if (rr > big_chunk_start && rr <= big_chunk_end) return;
  1265. #endif
  1266. free(r);
  1267. #else
  1268. int *p, *q, n;
  1269. #ifndef HOLD_BACK_MEMORY
  1270. char *rr = (char *)r;
  1271. /*
  1272. * I will not free it if the pointer is strictly inside the single big
  1273. * chunk that I grabbed at the start of the run.
  1274. */
  1275. if (rr > big_chunk_start && rr <= big_chunk_end) return;
  1276. #endif
  1277. p = (int *)((char *)r - 16);
  1278. n = p[1];
  1279. if (p[2] != 0x12345678 ||
  1280. p[3] != 0x3456789a)
  1281. { term_printf("Corruption at start of memory block %.8x: %.8x %.8x\n",
  1282. (int)r, p[2], p[3]);
  1283. ensure_screen();
  1284. my_exit(0);
  1285. }
  1286. q = (int *)((char *)r + n);
  1287. if (q[0] != 0x87654321 ||
  1288. q[1] != 0xcba98765)
  1289. { term_printf("Corruption at end of memory block %.8x: %.8x %.8x\n",
  1290. (int)r, q[0], q[1]);
  1291. ensure_screen();
  1292. my_exit(0);
  1293. }
  1294. free((void *)p[0]);
  1295. #endif
  1296. }
  1297. #endif
  1298. static void *my_malloc_1(size_t n)
  1299. /*
  1300. * This is a pretty silly function - it gobbles up 24Kbytes of
  1301. * stack and then just calls malloc - it stuffs a pointer to the
  1302. * stack-chunk into a static variable so that compilers can not
  1303. * detect (I hope!) that the array remains unused. The purpose of this
  1304. * is to make malloc fail if it is about to encroach on space that
  1305. * should be used for stack. This is relevant on small systems where
  1306. * stack and heap grow towards one another and where one space has been
  1307. * grabbed by malloc it is unavailable for stack (even if it is FREEd).
  1308. * The number 24000 is pretty arbitrary - but if I have 24Kbytes of stack
  1309. * I will be able to do at least something.
  1310. * Also this code verifies that the memory addresses returned have the
  1311. * correct most significant bit. I allocate just a bit more memory than
  1312. * is really needed to leave a one-word (or so) guard-band between
  1313. * allocated blocks. This is necessary on some releases of an SGI C
  1314. * compiler (library) where blocks of memory that are word but not
  1315. * doubleword aligned can be returned.
  1316. */
  1317. {
  1318. char gobble_stack[24000];
  1319. char *r;
  1320. int32 pun, pun1;
  1321. global_handle = gobble_stack;
  1322. r = (char *)my_malloc(n+8);
  1323. pun = (int32)r;
  1324. pun1 = (int32)(r + n);
  1325. /*
  1326. * I will moan if the block of memory allocated spans zero.
  1327. * Note that if this does happen then something very funny is happening
  1328. * about 0 cast to a pointer (i.e. a NULL pointer) since NULL is supposed
  1329. * not to be valid as an address (?) but appears to be within the address
  1330. * range of the block of store just allocated.
  1331. */
  1332. if ((pun ^ pun1) < 0) fatal_error(err_mem_spans_zero);
  1333. /*
  1334. * Now if I get a block with the "wrong" top bit I will just return NULL
  1335. * to suggest that no more memory was available - CSL can then proceed
  1336. * or fail as it sees fit.
  1337. */
  1338. #ifdef ADDRESS_SIGN_UNKNOWN
  1339. /*
  1340. * For dynamic address sign I should not test the address sign on the
  1341. * first call - instead I just remember what it was. On subsequent calls
  1342. * I will check it.
  1343. */
  1344. if (nilsegment != NULL)
  1345. { if ((pun + address_sign) < 0) return NULL;
  1346. /* fatal_error(err_top_bit); */
  1347. }
  1348. else address_sign = pun & 0x80000000;
  1349. #else
  1350. #ifdef ADDRESSES_HAVE_TOP_BIT_SET
  1351. if (pun > 0) return NULL; /* fatal_error(err_top_bit); */
  1352. #else
  1353. if (pun < 0) return NULL; /* fatal_error(err_top_bit); */
  1354. #endif
  1355. #endif
  1356. return (void *)r;
  1357. }
  1358. static void *my_malloc_2(size_t n)
  1359. /*
  1360. * Rather like my_malloc_1(), but does NOT check the sign bit of the
  1361. * returned pointer. Provided as a place to put hooks to check memory
  1362. * allocation problems.
  1363. */
  1364. {
  1365. char gobble_stack[24000];
  1366. char *r;
  1367. global_handle = gobble_stack;
  1368. r = (char *)my_malloc(n+8);
  1369. return (void *)r;
  1370. }
  1371. static void init_heap_segments(double store_size)
  1372. /*
  1373. * This function just makes nil and the pool of page-frames available
  1374. */
  1375. {
  1376. char *memfile = "memory.use"; /* For memory statistics etc */
  1377. pages = (void **)my_malloc_2(MAX_PAGES*sizeof(void *));
  1378. heap_pages = (void **)my_malloc_2(MAX_PAGES*sizeof(void *));
  1379. vheap_pages = (void **)my_malloc_2(MAX_PAGES*sizeof(void *));
  1380. bps_pages = (void **)my_malloc_2(MAX_BPS_PAGES*sizeof(void *));
  1381. native_pages = (void **)my_malloc_2(MAX_NATIVE_PAGES*sizeof(void *));
  1382. #ifndef NO_COPYING_GC
  1383. new_heap_pages = (void **)my_malloc_2(MAX_PAGES*sizeof(void *));
  1384. new_vheap_pages = (void **)my_malloc_2(MAX_PAGES*sizeof(void *));
  1385. new_bps_pages = (void **)my_malloc_2(MAX_BPS_PAGES*sizeof(void *));
  1386. new_native_pages = (void **)my_malloc_2(MAX_NATIVE_PAGES*sizeof(void *));
  1387. #endif
  1388. pair_c = (unsigned char *)my_malloc_2(CODESIZE);
  1389. /*
  1390. * The next line is utterly unsatisfactory at present
  1391. */
  1392. char_stack = (unsigned char *)my_malloc_2(CSL_PAGE_SIZE /*CODESIZE*/);
  1393. pair_prev = (unsigned short int *)
  1394. my_malloc_2(CODESIZE*sizeof(unsigned short int));
  1395. if (pages == NULL ||
  1396. #ifndef NO_COPYING_GC
  1397. new_heap_pages == NULL ||
  1398. new_vheap_pages == NULL ||
  1399. new_bps_pages == NULL ||
  1400. new_native_pages == NULL ||
  1401. #endif
  1402. heap_pages == NULL ||
  1403. vheap_pages == NULL ||
  1404. bps_pages == NULL ||
  1405. native_pages == NULL ||
  1406. pair_c == NULL ||
  1407. char_stack == NULL ||
  1408. pair_prev == NULL) fatal_error(err_no_store);
  1409. {
  1410. #ifdef COMMON
  1411. int32 free_space = 8000000; /* Try 8 Mbyte as a default heap size */
  1412. #else
  1413. int32 free_space = 8000000; /* Try 8 Mbyte as a default heap size */
  1414. #endif
  1415. int32 request = (int32)store_size;
  1416. if (request != 0) free_space = 1024*request;
  1417. free_space = free_space/(CSL_PAGE_SIZE+4);
  1418. if (free_space > MAX_PAGES) free_space = MAX_PAGES;
  1419. pages_count = heap_pages_count = vheap_pages_count =
  1420. bps_pages_count = native_pages_count = 0;
  1421. native_fringe = 0;
  1422. /*
  1423. * I grab memory using a function called my_malloc_1(), which verifies that
  1424. * all addresses used in the heap have the same top bit. The very first time
  1425. * it is called nilsegment will be NULL - that time it does less checking.
  1426. */
  1427. nilsegment = NULL;
  1428. #ifdef HOLD_BACK_MEMORY
  1429. /*
  1430. * Try to grab a bit extra since I will then hand it back. This version
  1431. * of the code MUST be used on machines where size_t (the argument to
  1432. * malloc) denotes a 16-bit value.
  1433. */
  1434. free_space += HOLD_BACK_MEMORY;
  1435. /*
  1436. * I should wait until later to grab space, I suspect... I.e. not pre-allocate
  1437. * pages for the heap now at the start of the run but wait until the garbage
  1438. * collector tells me that enlarging the CSL heap would be a good idea.
  1439. * What I do at present is to allocate a reasonable amount of memory here,
  1440. * and on small machines I will leave it at that. On large machines
  1441. * I will allocate more space (maybe) during garbage collection. I use
  1442. * the HOLD_BACK_MEMORY and (init_flags & INIT_EXPANDABLE) to control things
  1443. * in a finer way.
  1444. */
  1445. nilsegment = (Lisp_Object *)my_malloc_1(NIL_SEGMENT_SIZE);
  1446. #ifdef COMMON
  1447. /*
  1448. * NB here that NIL is tagged as a CONS not as a symbol. That means that
  1449. * qheader(nil) is BEFORE the place that might normally have been the start
  1450. * of the segment, so I add 8 to preserve alignment but to leave room for
  1451. * the header. I have made NIL_SEGMENT_SIZE large enough by a useful margin
  1452. * (in externs.h) so I should not run off th eend of it.
  1453. */
  1454. C_nil = doubleword_align_up(nilsegment) + TAG_CONS + 8;
  1455. #else
  1456. C_nil = doubleword_align_up(nilsegment) + TAG_SYMBOL;
  1457. #endif
  1458. while (pages_count < free_space)
  1459. {
  1460. /*
  1461. * I get 8 bytes more than seems necessary because I will need to
  1462. * align my page frames up to a doubleword boundary, and that can
  1463. * potentially waste 7 bytes.
  1464. */
  1465. void *page = (void *)my_malloc_1((size_t)(CSL_PAGE_SIZE+8));
  1466. if (page == NULL) break;
  1467. pages[pages_count++] = page;
  1468. }
  1469. /*
  1470. * Now release some memory for the operating system to play with. This
  1471. * is a bit crude, in that it can "waste" precious space on small
  1472. * machines, but on at least the Acorn Archimedes (RISCOS) and the
  1473. * Macintosh (system 7) it appears to be VITAL.
  1474. */
  1475. { int32 i;
  1476. for (i=0; i<HOLD_BACK_MEMORY; i++)
  1477. if (pages_count != 0) my_free(pages[--pages_count]);
  1478. }
  1479. #else /* HOLD_BACK_MEMORY */
  1480. #ifdef DEBUG
  1481. if (sizeof(size_t) < 4 && PAGE_BITS > 16)
  1482. { fprintf(stderr,
  1483. "This machine needs configuration with HOLD_BACK_MEMORY\n");
  1484. fprintf(stderr,
  1485. "and PAGE_BITS <= 16. Change machine.h and recompile\n");
  1486. my_exit(1);
  1487. }
  1488. #endif
  1489. { size_t n = (size_t)(NIL_SEGMENT_SIZE+free_space*(CSL_PAGE_SIZE+8));
  1490. /*
  1491. * I try to get the whole of the initial hunk of memory that I need in
  1492. * one gulp since that (maybe) gives me the best chance to obtain all
  1493. * the memory in just one half of my address space.
  1494. */
  1495. char *pool = (char *)my_malloc_1(n);
  1496. /*
  1497. * I get 8 bytes more than seems necessary because I will need to
  1498. * align my page frames up to a doubleword boundary, and that can
  1499. * potentially waste 7 bytes.
  1500. */
  1501. if (pool != NULL)
  1502. { big_chunk_start = (char *)pool;
  1503. big_chunk_end = big_chunk_start + (n-1);
  1504. #ifdef __mips__
  1505. /*
  1506. * Some versions of the C compiler on 64-bit SGI machines leave
  1507. * big_chunk_start and _end both zero despite the above. The effect is
  1508. * unimportant until the end of a run of CSL, when the test at the start
  1509. * of my_free misbehaves and a report is generated about memory "corruption".
  1510. * With LUCK the extra reference to the variables via the call to the
  1511. * dummy function will cause the compiler to behave in a more conservative
  1512. * manner and get things correct. The problem has not been seen on
  1513. * 32-bit SGI systems, but the extra dummy function call is not very
  1514. * expensive so can be tolerated in such cases.
  1515. * ACN: August 1996
  1516. */
  1517. dummy_function_call("64-bit SGI machines",
  1518. big_chunk_start, big_chunk_end);
  1519. #endif
  1520. #ifdef MEMORY_TRACE
  1521. memory_base = (int32)pool;
  1522. memory_size = n;
  1523. memory_count = 0;
  1524. memory_map = (unsigned char *)malloc(n/32 + 16);
  1525. if (memory_map != 0)
  1526. { memset(memory_map, 0, n/32+8);
  1527. memory_file = fopen(memfile, "wb");
  1528. if (memory_file == NULL)
  1529. { free(memory_map);
  1530. memory_map = 0;
  1531. }
  1532. else
  1533. { n = n/32 + 8;
  1534. putc(0, memory_file);
  1535. putc(0, memory_file);
  1536. putc(0, memory_file); /* 3 bytes to overwrite later on */
  1537. putc(n, memory_file);
  1538. putc(n>>8, memory_file);
  1539. putc(n>>16, memory_file);
  1540. memory_comment(2); /* startup code */
  1541. init_flags &= ~INIT_EXPANDABLE;
  1542. }
  1543. }
  1544. #endif
  1545. nilsegment = (Lisp_Object *)pool;
  1546. pool = pool + NIL_SEGMENT_SIZE;
  1547. #ifdef COMMON
  1548. /* NB here that NIL is tagged as a CONS not as a symbol */
  1549. C_nil = doubleword_align_up(nilsegment) + TAG_CONS + 8;
  1550. #else
  1551. C_nil = doubleword_align_up(nilsegment) + TAG_SYMBOL;
  1552. #endif
  1553. /*
  1554. * If at the end of the run I am going to free some space I had better not
  1555. * free these pages. When I free the nilsegment they all get discarded at
  1556. * once.
  1557. */
  1558. while (pages_count < free_space)
  1559. { void *page = (void *)&pool[pages_count*(CSL_PAGE_SIZE+8)];
  1560. pages[pages_count++] = page;
  1561. }
  1562. }
  1563. }
  1564. #endif
  1565. }
  1566. if (nilsegment != NULL && pages_count > 0)
  1567. { if (stack_segsize != 1)
  1568. { stacksegment =
  1569. (Lisp_Object *)my_malloc(stack_segsize*CSL_PAGE_SIZE + 8);
  1570. if (stacksegment == NULL) fatal_error(err_no_store);
  1571. }
  1572. stacksegment = (Lisp_Object *)pages[--pages_count];
  1573. }
  1574. else fatal_error(err_no_store);
  1575. MD5_Update((unsigned char *)memfile, 8);
  1576. /*
  1577. * The stack does not need to be doubleword aligned, but it does need
  1578. * to be word aligned (otherwise certain back-pointers in the garbage
  1579. * collector give trouble), so I fix it up here. Note that stacksegment
  1580. * remains pointing at the original base so that I can free() it later.
  1581. */
  1582. stackbase = (Lisp_Object *)doubleword_align_up((int32)stacksegment);
  1583. }
  1584. #ifdef EXPLICIT_FREE_AT_END_OF_RUN
  1585. /*
  1586. * In general I will let CSL exit without bothering to free up all the
  1587. * memory that it allocated - that job can be left (to the extent that
  1588. * it is needed at all) to the run-time system. But if for some reason
  1589. * you really mind about such things here is some code to do it for you...
  1590. */
  1591. static void abandon(void *p[], int32 n)
  1592. {
  1593. while (n != 0)
  1594. { void *w = p[--n];
  1595. /*
  1596. * The test here that avoids calling free on a NULL pointer is
  1597. * certainly not needed with an ANSI compliant library - but
  1598. * rumour has it that many Unix libraries are unkind in this
  1599. * respect, and the test is pretty cheap...
  1600. */
  1601. if (w != NULL) my_free(w);
  1602. }
  1603. }
  1604. #endif
  1605. void drop_heap_segments(void)
  1606. {
  1607. #ifdef MEMORY_TRACE
  1608. identify_page_types();
  1609. #endif
  1610. #ifdef EXPLICIT_FREE_AT_END_OF_RUN
  1611. abandon(pages, pages_count);
  1612. abandon(heap_pages, heap_pages_count);
  1613. abandon(vheap_pages, vheap_pages_count);
  1614. abandon(bps_pages, bps_pages_count);
  1615. abandon(native_pages, native_pages_count);
  1616. my_free(stacksegment);
  1617. my_free(nilsegment);
  1618. #endif
  1619. #ifdef MEMORY_TRACE
  1620. fseek(memory_file, 0L, SEEK_SET);
  1621. putc(memory_records & 0xff, memory_file);
  1622. putc((memory_records>>8) & 0xff, memory_file);
  1623. putc((memory_records>>16) & 0xff, memory_file);
  1624. fclose(memory_file);
  1625. memory_file = NULL;
  1626. memory_map = NULL;
  1627. #endif
  1628. }
  1629. static char *find_checksum(char *name, int32 len, const setup_type *p)
  1630. {
  1631. char *n;
  1632. while (p->name != NULL) p++;
  1633. n = (char *)p->one;
  1634. if (strlen(n) == len && memcmp(name, n, len) == 0)
  1635. return (char *)p->two;
  1636. else return NULL;
  1637. }
  1638. static Lisp_Object MS_CDECL Lcheck_c_code(Lisp_Object nil, int nargs, ...)
  1639. {
  1640. Lisp_Object name, lc1, lc2, lc3;
  1641. int32 c1=-1, c2=-1, c3=-1;
  1642. long int x1=-2, x2=-2, x3=-2;
  1643. int32 len;
  1644. va_list a;
  1645. char *p;
  1646. char *sname;
  1647. argcheck(nargs, 4, "check-c-code");
  1648. va_start(a, nargs);
  1649. name = va_arg(a, Lisp_Object);
  1650. lc1 = va_arg(a, Lisp_Object);
  1651. lc2 = va_arg(a, Lisp_Object);
  1652. lc3 = va_arg(a, Lisp_Object);
  1653. va_end(a);
  1654. if (!is_vector(name) ||
  1655. type_of_header(vechdr(name)) != TYPE_STRING ||
  1656. !is_fixnum(lc1) ||
  1657. !is_fixnum(lc2) ||
  1658. !is_fixnum(lc3)) return aerror1("check-c-code", name);
  1659. c1 = int_of_fixnum(lc1);
  1660. c2 = int_of_fixnum(lc2);
  1661. c3 = int_of_fixnum(lc3);
  1662. sname = &celt(name, 0);
  1663. len = length_of_header(vechdr(name)) - 4;
  1664. /*
  1665. * trace_printf("+++ Checking %.*s %d %d %d\n",
  1666. * (int)len, sname, c1, c2, c3);
  1667. */
  1668. p = find_checksum(sname, len, u01_setup);
  1669. if (p == NULL) p = find_checksum(sname, len, u02_setup);
  1670. if (p == NULL) p = find_checksum(sname, len, u03_setup);
  1671. if (p == NULL) p = find_checksum(sname, len, u04_setup);
  1672. if (p == NULL) p = find_checksum(sname, len, u05_setup);
  1673. if (p == NULL) p = find_checksum(sname, len, u06_setup);
  1674. if (p == NULL) p = find_checksum(sname, len, u07_setup);
  1675. if (p == NULL) p = find_checksum(sname, len, u08_setup);
  1676. if (p == NULL) p = find_checksum(sname, len, u09_setup);
  1677. if (p == NULL) p = find_checksum(sname, len, u10_setup);
  1678. if (p == NULL) p = find_checksum(sname, len, u11_setup);
  1679. if (p == NULL) p = find_checksum(sname, len, u12_setup);
  1680. if (p == NULL) return aerror1("check-c-code", name);
  1681. if (sscanf(p, "%ld %ld %ld", &x1, &x2, &x3) != 3)
  1682. return aerror("check-c-code");
  1683. if (c1 == x1 && c2 == x2 && c3 == x3) return onevalue(nil);
  1684. err_printf("\n+++++ C code and environment files not compatible\n");
  1685. err_printf("please check, re-compile and try again\n");
  1686. return aerror("check-c-code");
  1687. }
  1688. static setup_type const restart_setup[] =
  1689. /*
  1690. * things that are in modules that do not define enough Lisp entrypoints
  1691. * to be worth giving separate entry-tables.
  1692. */
  1693. {
  1694. {"check-c-code", wrong_no_na, wrong_no_nb, Lcheck_c_code},
  1695. {"define-in-module", Ldefine_in_module, too_many_1, wrong_no_1},
  1696. {"modulep", Lmodule_exists, too_many_1, wrong_no_1},
  1697. {"start-module", Lstart_module, too_many_1, wrong_no_1},
  1698. {"write-module", Lwrite_module, too_many_1, wrong_no_1},
  1699. {"copy-module", Lcopy_module, too_many_1, wrong_no_1},
  1700. {"delete-module", Ldelete_module, too_many_1, wrong_no_1},
  1701. {"load-module", Lload_module, too_many_1, wrong_no_1},
  1702. {"list-modules", wrong_no_na, wrong_no_nb, Llist_modules},
  1703. {"writable-libraryp", Lwritable_libraryp, too_many_1, wrong_no_1},
  1704. {"library-members", Llibrary_members, too_many_1, Llibrary_members0},
  1705. {"startup-banner", Lbanner, too_many_1, wrong_no_1},
  1706. #ifdef HELP_SYSTEM
  1707. {"write-help-module", too_few_2, Lwrite_help_module, wrong_no_2},
  1708. {"help", Lhelp, Lhelp_2, Lhelp_n},
  1709. {"?", Lhelp, too_many_1, wrong_no_1},
  1710. #endif
  1711. {"set-help-file", too_few_2, Lset_help_file, wrong_no_2},
  1712. {"mapstore", Lmapstore, too_many_1, Lmapstore0},
  1713. {"verbos", Lverbos, too_many_1, wrong_no_1},
  1714. #ifdef COMMON
  1715. {"errorset", Lerrorset1, Lerrorset2, Lerrorsetn},
  1716. {"gc", Lgc, too_many_1, Lgc0},
  1717. #else
  1718. {"errorset", Lerrorset1, Lerrorset2, Lerrorsetn},
  1719. {"reclaim", Lgc, too_many_1, Lgc0},
  1720. #endif
  1721. {NULL, 0, 0, 0}
  1722. };
  1723. static void create_symbols(setup_type const s[], CSLbool restartp)
  1724. {
  1725. int i;
  1726. for (i=0; s[i].name != NULL; i++)
  1727. make_symbol(s[i].name, restartp, s[i].one, s[i].two, s[i].n);
  1728. }
  1729. static int32 defined_symbols;
  1730. static void count_symbols(setup_type const s[])
  1731. {
  1732. int i;
  1733. for (i=0; s[i].name != NULL; i++) defined_symbols++;
  1734. }
  1735. static void set_up_variables(CSLbool restartp);
  1736. static void warm_setup()
  1737. {
  1738. /*
  1739. * Here I need to read in the bulk of the checkpoint file.
  1740. */
  1741. Lisp_Object nil = C_nil;
  1742. int32 i;
  1743. Cfread((char *)&heap_pages_count, sizeof(heap_pages_count));
  1744. Cfread((char *)&vheap_pages_count, sizeof(vheap_pages_count));
  1745. Cfread((char *)&bps_pages_count, sizeof(bps_pages_count));
  1746. heap_pages_count = flip_bytes(heap_pages_count);
  1747. vheap_pages_count = flip_bytes(vheap_pages_count);
  1748. bps_pages_count = flip_bytes(bps_pages_count);
  1749. /*
  1750. * Here I want to arrange to have at least one free page after re-loading
  1751. * an image. If malloc can give me enough I grab it here. Note that I do
  1752. * not yet know how many pages will be needed for hard code, which is a
  1753. * bit of a nuisance!
  1754. */
  1755. i = heap_pages_count+vheap_pages_count+
  1756. bps_pages_count+1 - pages_count;
  1757. #ifdef MEMORY_TRACE
  1758. if (i > 0) fatal_error(err_no_store);
  1759. #else
  1760. while (i-- > 0)
  1761. { void *page = my_malloc_1((size_t)(CSL_PAGE_SIZE + 8));
  1762. if (page == NULL) fatal_error(err_no_store);
  1763. else pages[pages_count++] = page;
  1764. }
  1765. #endif
  1766. { char dummy[16];
  1767. Cfread(dummy, 8);
  1768. }
  1769. #ifdef MEMORY_TRACE
  1770. memory_comment(6); /* vector heap */
  1771. #endif
  1772. for (i=0; i<vheap_pages_count; i++)
  1773. { int32 p;
  1774. vheap_pages[i] = allocate_page();
  1775. p = doubleword_align_up((int32)vheap_pages[i]);
  1776. Cfread((char *)p, CSL_PAGE_SIZE);
  1777. }
  1778. { char dummy[16];
  1779. Cfread(dummy, 8);
  1780. }
  1781. #ifdef MEMORY_TRACE
  1782. memory_comment(5); /* cons heap */
  1783. #endif
  1784. for (i=0; i<heap_pages_count; i++)
  1785. { int32 p;
  1786. heap_pages[i] = allocate_page();
  1787. p = doubleword_align_up((int32)heap_pages[i]);
  1788. Cfread((char *)p, CSL_PAGE_SIZE);
  1789. }
  1790. { char dummy[16];
  1791. Cfread(dummy, 8);
  1792. }
  1793. #ifdef MEMORY_TRACE
  1794. memory_comment(14); /* BPS heap */
  1795. #endif
  1796. for (i=0; i<bps_pages_count; i++)
  1797. { int32 p;
  1798. bps_pages[i] = allocate_page();
  1799. p = doubleword_align_up((int32)bps_pages[i]);
  1800. Cfread((char *)p, CSL_PAGE_SIZE);
  1801. }
  1802. { char endmsg[32];
  1803. Cfread(endmsg, 24); /* the termination record */
  1804. /*
  1805. * Although I check here I will not make the system crash if I see an
  1806. * error - at least until I have tested things and found this test
  1807. * properly reliable.
  1808. */
  1809. #ifdef COMMON
  1810. if (strncmp(endmsg, "\n\nEnd of CCL dump file\n\n", 24) != 0)
  1811. #else
  1812. if (strncmp(endmsg, "\n\nEnd of CSL dump file\n\n", 24) != 0)
  1813. #endif
  1814. { term_printf("\n+++ Bad end record |%s|\n", endmsg);
  1815. }
  1816. }
  1817. /*
  1818. * There is a delicacy here - Cfread uses Iread to read chunks of
  1819. * data from the real input file, but it never goes beyond the recorded
  1820. * end of file mark. This buffering ensures that at this stage any
  1821. * pending part-word of data will have been read - this because the
  1822. * read buffer used is a multiple of 4 bytes long. This point matters
  1823. * with regard to checksum validation on these files.
  1824. */
  1825. crypt_active = -1; /* Have read all of the initial image file */
  1826. IcloseInput(YES);
  1827. #ifdef MEMORY_TRACE
  1828. memory_comment(9); /* adjusting */
  1829. #endif
  1830. inject_randomness((int)clock());
  1831. adjust_all();
  1832. #ifdef MEMORY_TRACE
  1833. memory_comment(12); /* remainder of setup */
  1834. #endif
  1835. eq_hash_tables = eq_hash_table_list;
  1836. equal_hash_tables = equal_hash_table_list;
  1837. eq_hash_table_list = equal_hash_table_list = nil;
  1838. { Lisp_Object qq;
  1839. for (qq = eq_hash_tables; qq!=nil; qq=qcdr(qq))
  1840. rehash_this_table(qcar(qq));
  1841. for (qq = equal_hash_tables; qq!=nil; qq=qcdr(qq))
  1842. rehash_this_table(qcar(qq));
  1843. }
  1844. gensym_ser = flip_bytes(gensym_ser);
  1845. print_precision = flip_bytes(print_precision);
  1846. miscflags = flip_bytes(miscflags);
  1847. current_modulus = flip_bytes(current_modulus);
  1848. fastget_size = flip_bytes(fastget_size);
  1849. package_bits = flip_bytes(package_bits);
  1850. set_up_functions(1);
  1851. set_up_variables(1);
  1852. /*
  1853. * Now I have closed the main heap image, but if there is any hard machine
  1854. * code available for this architecture I should load it. When I do this
  1855. * the main heap has been loaded and relocated and all the entrypoints
  1856. * in it that relate to kernel code have been inserted.
  1857. */
  1858. if (native_code_tag != 0) /* Not worth trying if none available */
  1859. { if (!IopenRoot(NULL, -native_code_tag))
  1860. { int32 nn = Igetc() & 0xff;
  1861. nn = nn + ((Igetc() & 0xff) << 8);
  1862. native_pages_count = nn;
  1863. for (i=0; i<native_pages_count; i++)
  1864. { int32 p;
  1865. /*
  1866. * Because I did not know earlier how many pages would be needed here I
  1867. * may not have overall enough. So I expand my heap (if possible)
  1868. * when things start to look tight here.
  1869. */
  1870. if (pages_count <= 1)
  1871. { void *page = my_malloc_1((size_t)(CSL_PAGE_SIZE + 8));
  1872. if (page == NULL) fatal_error(err_no_store);
  1873. else pages[pages_count++] = page;
  1874. }
  1875. native_pages[i] = allocate_page();
  1876. p = (int32)native_pages[i];
  1877. p = doubleword_align_up(p);
  1878. fread_count = 0;
  1879. Cfread((char *)p, CSL_PAGE_SIZE);
  1880. native_fringe = *(int32 *)p;
  1881. relocate_native_code((unsigned char *)p, native_fringe);
  1882. }
  1883. IcloseInput(YES);
  1884. }
  1885. }
  1886. /*
  1887. * Finally with a warm start I must instate the definitions of all functions
  1888. * that may have been compiled into hard code on this platform. Functions that
  1889. * may be hard-coded on SOME platform may also be in a mess and will have
  1890. * a byte-coded definition put back in place at this point. Observe that this
  1891. * happens AFTER the system has otherwise been loaded and relocated.
  1892. */
  1893. { Lisp_Object f_list = native_code, byte_code_def;
  1894. do_not_kill_native_code = 1;
  1895. while (f_list != nil)
  1896. { Lisp_Object w, fn, defs;
  1897. int32 nargs;
  1898. int instated_something = 0;
  1899. byte_code_def = nil;
  1900. w = qcar(f_list);
  1901. f_list = qcdr(f_list);
  1902. fn = qcar(w); w = qcdr(w);
  1903. nargs = int_of_fixnum(qcar(w));
  1904. defs = qcdr(w);
  1905. while (defs != nil)
  1906. { int32 n, tag, type, page, off;
  1907. void *e;
  1908. w = qcar(defs);
  1909. defs = qcdr(defs);
  1910. n = int_of_fixnum(qcar(w));
  1911. w = qcdr(w);
  1912. tag = (n >> 20) & 0xff;
  1913. type = (n >> 18) & 0x3;
  1914. page = n & 0x3ffff;
  1915. if (tag == 0)
  1916. { byte_code_def = qcdr(w);
  1917. continue;
  1918. }
  1919. if (tag != native_code_tag) continue; /* Not for me today */
  1920. instated_something = 1;
  1921. off = int_of_fixnum(qcar(w));
  1922. w = qcdr(w);
  1923. /*
  1924. * Now fn should be a symbol, the function to be defined. w is the thing to go
  1925. * into its environment cell. page and off define a location in the hard
  1926. * code space and type tells me which of the 3 function cells to put that in.
  1927. *
  1928. * I will not (yet) mess around with the removal of C definition
  1929. * flags and all the other delicacies. Note that this means attempts to
  1930. * redefine built-in functions with user-provided native code varients
  1931. * may cause all sorts of muddle! Please do not try it, but when you
  1932. * do (!) tell me and I will attempt to work out what ought to happen.
  1933. * Maybe it will all be OK provided that a consistent byte-code definition
  1934. * is in place before any native code gets generated.
  1935. */
  1936. page = (int32)native_pages[page];
  1937. page = doubleword_align_up(page);
  1938. e = (void *)((char *)page + off);
  1939. switch (type)
  1940. {
  1941. /*
  1942. * Warning - I just support nargs being a simple integer here, with no
  1943. * fancy encoding for variable numbers of args or &rest args etc. I think
  1944. * that for native code all such cases need to be dealt with via non-zero
  1945. * type code so that the 3 individual function cells get filled in one
  1946. * by 1.
  1947. */
  1948. case 0: switch (nargs)
  1949. {
  1950. case 0: set_fns(fn, wrong_no_0a, wrong_no_0b, (n_args *)e);
  1951. break;
  1952. case 1: set_fns(fn, (one_args *)e, too_many_1, wrong_no_1);
  1953. break;
  1954. case 2: set_fns(fn, too_few_2, (two_args *)e, wrong_no_2);
  1955. break;
  1956. case 3: set_fns(fn, wrong_no_3a, wrong_no_3b, (n_args *)e);
  1957. break;
  1958. default:set_fns(fn, wrong_no_na, wrong_no_nb, (n_args *)e);
  1959. break;
  1960. }
  1961. break;
  1962. /*
  1963. * A non-zero type field allows me to fill in just one of the function cells.
  1964. * Note that I ought to arrange to get ALL of them filled in somehow, either
  1965. * by using type=0 or by using all three of type = 1,2,3.
  1966. */
  1967. case 1: ifn1(fn) = (int32)e;
  1968. break;
  1969. case 2: ifn2(fn) = (int32)e;
  1970. break;
  1971. case 3: ifnn(fn) = (int32)e;
  1972. break;
  1973. }
  1974. qenv(fn) = w;
  1975. }
  1976. if (!instated_something && byte_code_def != nil)
  1977. { w = cons(fixnum_of_int(nargs), byte_code_def);
  1978. /*
  1979. * You can look at this bit of code and moan, saying "What happens if
  1980. * the call to CONS causes a garbage collection?". Well I have this policy
  1981. * that garbage collection attempts during startup should be thought of
  1982. * as fatal, and that the user should give enough memory to make it possible
  1983. * to get at least started. I hope that I do not generate much litter here
  1984. * and in other places within the startup code. Not thinking about GC
  1985. * safety leaves the code neater and easier to work with.
  1986. */
  1987. Lsymbol_set_definition(nil, fn, w);
  1988. }
  1989. }
  1990. do_not_kill_native_code = 0;
  1991. }
  1992. inject_randomness((int)clock());
  1993. }
  1994. static void cold_setup()
  1995. {
  1996. Lisp_Object nil = C_nil;
  1997. void *p;
  1998. p = vheap_pages[vheap_pages_count++] = allocate_page();
  1999. vfringe = (Lisp_Object)(8 + (char *)doubleword_align_up((int32)p));
  2000. vheaplimit = (Lisp_Object)((char *)vfringe + (CSL_PAGE_SIZE - 16));
  2001. p = heap_pages[heap_pages_count++] = allocate_page();
  2002. heaplimit = doubleword_align_up((int32)p);
  2003. fringe = (Lisp_Object)((char *)heaplimit + CSL_PAGE_SIZE);
  2004. heaplimit = (Lisp_Object)((char *)heaplimit + SPARE);
  2005. codelimit = codefringe = 0; /* no BPS to start with */
  2006. miscflags = 3;
  2007. qplist(nil) = nil;
  2008. qfastgets(nil) = nil;
  2009. qenv(nil) = nil; /* points to self in undefined case */
  2010. ifn1(nil) = (int32)undefined1;
  2011. ifn2(nil) = (int32)undefined2;
  2012. ifnn(nil) = (int32)undefinedn;
  2013. qheader(nil) = TAG_ODDS+TYPE_SYMBOL+SYM_SPECIAL_VAR;
  2014. qvalue(nil) = nil;
  2015. #ifdef COMMON
  2016. qpname(nil) = make_string("NIL");
  2017. #else
  2018. qpname(nil) = make_string("nil");
  2019. #endif
  2020. qcount(nil) = 0;
  2021. exit_tag = exit_value = nil;
  2022. exit_reason = UNWIND_NULL;
  2023. eq_hash_tables = equal_hash_tables = nil;
  2024. current_package = nil;
  2025. qvalue(nil) = getvector_init(sizeof(Package), nil);
  2026. #ifdef COMMON
  2027. qpackage(nil) = qvalue(nil); /* For sake of restart code */
  2028. all_packages = ncons(qvalue(nil));
  2029. #endif
  2030. packhdr_(CP) = TYPE_STRUCTURE + (packhdr_(CP) & ~header_mask);
  2031. #ifdef COMMON
  2032. packname_(CP) = make_string("LISP");
  2033. #endif
  2034. /*
  2035. * The size chosen here is only an initial size - the hash table in a package
  2036. * can grow later on if needbe - but I ought to ensure that the initial
  2037. * size is big enough for the built-in symbols that Lisp creates in
  2038. * this restart code. The size must be a power of 2.
  2039. */
  2040. #define INIT_OBVECI_SIZE 4096
  2041. packint_(CP) = getvector_init(INIT_OBVECI_SIZE+4, fixnum_of_int(0));
  2042. packvint_(CP) = fixnum_of_int(1);
  2043. packflags_(CP) = fixnum_of_int(++package_bits);
  2044. #ifdef COMMON
  2045. /*
  2046. * Common Lisp also has "external" symbols to allow for...
  2047. */
  2048. #define INIT_OBVECX_SIZE 4096
  2049. packnint_(CP) = fixnum_of_int(0);
  2050. packext_(CP) = getvector_init(INIT_OBVECX_SIZE+4, fixnum_of_int(0));
  2051. packvext_(CP) = fixnum_of_int(1);
  2052. packnext_(CP) = fixnum_of_int(1); /* Allow for nil */
  2053. { int i = (int)(hash_lisp_string(qpname(nil)) &
  2054. (INIT_OBVECX_SIZE/4 - 1));
  2055. elt(packext_(CP), i) = nil;
  2056. }
  2057. #else
  2058. packnint_(CP) = fixnum_of_int(1); /* Allow for nil */
  2059. { int i = (int)(hash_lisp_string(qpname(nil)) &
  2060. (INIT_OBVECI_SIZE/4 - 1));
  2061. elt(packint_(CP), i) = nil;
  2062. }
  2063. #endif
  2064. gensym_ser = 0;
  2065. print_precision = 15;
  2066. current_modulus = 1;
  2067. fastget_size = 32;
  2068. package_bits = 0;
  2069. unset_var = nil;
  2070. /*
  2071. * there had better not be a need for garbage collection here...
  2072. * ... or elsewhere in setup, since the world is not yet put together.
  2073. * Ditto interrupts.
  2074. */
  2075. #define boffo_size 256
  2076. boffo = getvector(TAG_VECTOR, TYPE_STRING, boffo_size+4);
  2077. memset((void *)((char *)boffo + (4 - TAG_VECTOR)), '@', boffo_size);
  2078. #ifndef COMMON
  2079. if (current_package == nil)
  2080. { current_package = make_undefined_symbol("*package*");
  2081. qheader(current_package) |= SYM_SPECIAL_VAR;
  2082. lisp_package = qvalue(current_package) = qvalue(nil);
  2083. qvalue(nil) = nil;
  2084. }
  2085. #else
  2086. /*
  2087. * The next line has hidden depths. When it is obeyed during cold start
  2088. * the C variable current_package has the value nil, hence make_symbol
  2089. * looks in the value cell of nil to find the package to intern wrt. Once
  2090. * this has been done I can put nil back how it ought to have been!
  2091. */
  2092. current_package = make_undefined_symbol("*package*");
  2093. qheader(current_package)|= SYM_SPECIAL_VAR;
  2094. lisp_package = qvalue(current_package) = qpackage(nil);
  2095. qvalue(nil) = nil; /* Whew! */
  2096. #endif
  2097. B_reg = nil; /* safe for GC */
  2098. unset_var = make_undefined_symbol("~indefinite-value~");
  2099. qheader(unset_var) |= SYM_SPECIAL_VAR;
  2100. qvalue(unset_var) = unset_var;
  2101. Lunintern(nil, unset_var);
  2102. /*
  2103. * Now in some minor sense the world is in a self-consistent state
  2104. */
  2105. lisp_true = make_undefined_symbol("t");
  2106. qheader(lisp_true) |= SYM_SPECIAL_VAR;
  2107. qvalue(lisp_true) = lisp_true;
  2108. savedef = make_undefined_symbol("*savedef");
  2109. comma_symbol = make_undefined_symbol("~comma");
  2110. comma_at_symbol = make_undefined_symbol("~comma-at");
  2111. lambda = make_undefined_symbol("lambda");
  2112. funarg = make_undefined_symbol("funarg");
  2113. cfunarg = make_undefined_symbol("cfunarg");
  2114. opt_key = make_undefined_symbol("&optional");
  2115. rest_key = make_undefined_symbol("&rest");
  2116. #ifdef COMMON
  2117. key_key = make_undefined_symbol("&key");
  2118. allow_other_keys = make_undefined_symbol("&allow-other-keys");
  2119. aux_key = make_undefined_symbol("&aux");
  2120. #endif
  2121. work_symbol = make_undefined_symbol("~magic-internal-symbol~");
  2122. Lunintern(nil, work_symbol);
  2123. #ifndef COMMON
  2124. packid_(CP) = make_undefined_symbol("package");
  2125. #else
  2126. package_symbol = make_undefined_symbol("package");
  2127. packid_(CP) = package_symbol;
  2128. #endif
  2129. macroexpand_hook = make_undefined_symbol("*macroexpand-hook*");
  2130. qheader(macroexpand_hook) |= SYM_SPECIAL_VAR;
  2131. evalhook = make_undefined_symbol("*evalhook*");
  2132. qheader(evalhook) |= SYM_SPECIAL_VAR;
  2133. qvalue(evalhook) = nil;
  2134. applyhook = make_undefined_symbol("*applyhook*");
  2135. qheader(applyhook) |= SYM_SPECIAL_VAR;
  2136. qvalue(applyhook) = nil;
  2137. #ifdef COMMON
  2138. keyword_package = make_undefined_symbol("*keyword-package*");
  2139. qheader(keyword_package) |= SYM_SPECIAL_VAR;
  2140. qvalue(keyword_package) = make_package(make_string("KEYWORD"));
  2141. err_table = make_undefined_symbol("*ERROR-MESSAGE*");
  2142. #else
  2143. err_table = make_undefined_symbol("*error-messages*");
  2144. #endif
  2145. qheader(err_table) |= SYM_SPECIAL_VAR;
  2146. qvalue(err_table) = nil;
  2147. #ifdef COMMON
  2148. #define make_keyword(name) \
  2149. Lintern_2(nil, make_string(name), qvalue(keyword_package))
  2150. internal_symbol = make_keyword("INTERNAL");
  2151. external_symbol = make_keyword("EXTERNAL");
  2152. inherited_symbol = make_keyword("INHERITED");
  2153. allow_key_key = make_keyword("ALLOW-OTHER-KEYS");
  2154. #else
  2155. #define make_keyword(name) make_undefined_symbol(name)
  2156. #endif
  2157. gensym_base = make_string("G");
  2158. #ifdef COMMON
  2159. special_symbol = make_undefined_symbol("special");
  2160. expand_def_symbol = make_undefined_symbol("expand-definer");
  2161. format_symbol = make_undefined_symbol("format");
  2162. string_char_sym = make_undefined_symbol("string-char");
  2163. cl_symbols = make_undefined_symbol("*cl-symbols*");
  2164. /*
  2165. * cl_symbols has to be at least a vector or else I can not
  2166. * read in the Lisp file that sets its proper value...
  2167. */
  2168. qvalue(cl_symbols) = getvector_init(8<<2, nil);
  2169. features_symbol = make_undefined_symbol("*features*");
  2170. qheader(cl_symbols) |= SYM_SPECIAL_VAR;
  2171. qheader(features_symbol) |= SYM_SPECIAL_VAR;
  2172. { Lisp_Object w;
  2173. #define make_constant(name, value) \
  2174. w = make_undefined_symbol(name); \
  2175. qheader(w) |= SYM_SPECIAL_VAR; \
  2176. qvalue(w) = value;
  2177. make_constant("most-positive-fixnum", fixnum_of_int(0x07ffffff));
  2178. make_constant("most-negative-fixnum", fixnum_of_int(0xf8000000));
  2179. /* #undef TYPE_LONG_FLOAT */
  2180. /* #define TYPE_LONG_FLOAT TYPE_DOUBLE_FLOAT */
  2181. make_constant("pi",
  2182. make_boxfloat(3.141592653589793238, TYPE_LONG_FLOAT));
  2183. }
  2184. #endif
  2185. append_symbol = make_undefined_symbol("append");
  2186. raise_symbol = make_undefined_symbol("*raise");
  2187. lower_symbol = make_undefined_symbol("*lower");
  2188. echo_symbol = make_undefined_symbol("*echo");
  2189. /*
  2190. * I think that having a built-in symbol called *hankaku even if Kanji support
  2191. * is not otherwise present is not too severe a problem, and making the
  2192. * symbol present always will help keep image files re-usable from one
  2193. * version of CSL to another.
  2194. */
  2195. hankaku_symbol = make_undefined_symbol("*hankaku");
  2196. comp_symbol = make_undefined_symbol("*comp");
  2197. compiler_symbol = make_undefined_symbol("compile");
  2198. native_symbol = make_undefined_symbol("native-compile");
  2199. traceprint_symbol = make_undefined_symbol("trace-print");
  2200. loadsource_symbol = make_symbol("load-source", 0, Lload_source, too_many_1, wrong_no_1);
  2201. prinl_symbol = make_symbol("prinl", 0, Lprin, too_many_1, wrong_no_1);
  2202. emsg_star = make_undefined_symbol("emsg*");
  2203. redef_msg = make_undefined_symbol("*redefmsg");
  2204. expr_symbol = make_undefined_symbol("expr");
  2205. fexpr_symbol = make_undefined_symbol("fexpr");
  2206. macro_symbol = make_undefined_symbol("macro");
  2207. break_function = make_undefined_symbol("*break-loop*");
  2208. qheader(raise_symbol) |= SYM_SPECIAL_VAR;
  2209. qheader(lower_symbol) |= SYM_SPECIAL_VAR;
  2210. qheader(echo_symbol) |= SYM_SPECIAL_VAR;
  2211. qheader(hankaku_symbol) |= SYM_SPECIAL_VAR;
  2212. qheader(comp_symbol) |= SYM_SPECIAL_VAR;
  2213. qheader(emsg_star) |= SYM_SPECIAL_VAR;
  2214. qheader(redef_msg) |= SYM_SPECIAL_VAR;
  2215. qheader(break_function) |= SYM_SPECIAL_VAR;
  2216. qvalue(break_function) = nil;
  2217. qheader(loadsource_symbol) |= SYM_SPECIAL_VAR;
  2218. qvalue(loadsource_symbol) = nil;
  2219. { Lisp_Object common = make_undefined_symbol("common-lisp-mode");
  2220. qheader(common) |= SYM_SPECIAL_VAR;
  2221. #ifdef COMMON
  2222. qvalue(common) = lisp_true;
  2223. qvalue(raise_symbol) = lisp_true;
  2224. qvalue(lower_symbol) = nil;
  2225. #else
  2226. qvalue(common) = nil;
  2227. qvalue(raise_symbol) = nil;
  2228. qvalue(lower_symbol) = lisp_true;
  2229. #endif
  2230. }
  2231. qvalue(echo_symbol) = nil;
  2232. qvalue(hankaku_symbol) = nil;
  2233. qvalue(comp_symbol) = nil;
  2234. qvalue(emsg_star) = nil;
  2235. qvalue(redef_msg) = lisp_true;
  2236. sys_hash_table = Lmkhash(nil, 3, fixnum_of_int(5), fixnum_of_int(2), nil);
  2237. get_counts = Lmkhash(nil, 3, fixnum_of_int(5), fixnum_of_int(0), nil);
  2238. /*
  2239. * I make the vector that can hold the names used for "fast" get tags big
  2240. * enough for the largest possible number.
  2241. */
  2242. fastget_names = getvector_init((MAX_FASTGET_SIZE+2)<<2, SPID_NOPROP);
  2243. /*
  2244. * The next bit is a horrid fudge, used in read.c (function orderp) to
  2245. * support REDUCE. It ensures that the flag 'noncom is subject to an
  2246. * optimisation for flag/flagp that allows it to be tested for using a
  2247. * simple bit-test. This MUST use entry zero (coded as 1 here!).
  2248. * Also I insist that 'lose be the second fastget thing!
  2249. */
  2250. { Lisp_Object nc = make_undefined_symbol("noncom");
  2251. qheader(nc) |= (1L << SYM_FASTGET_SHIFT);
  2252. elt(fastget_names, 0) = nc;
  2253. nc = make_undefined_symbol("lose");
  2254. qheader(nc) |= (2L << SYM_FASTGET_SHIFT);
  2255. elt(fastget_names, 1) = nc;
  2256. }
  2257. /*
  2258. * I create the stream objects just once at cold-start time, but every time I
  2259. * restart I will fill in their components in the standard way again.
  2260. */
  2261. lisp_work_stream = make_stream_handle();
  2262. lisp_terminal_io = make_stream_handle();
  2263. lisp_standard_output = make_stream_handle();
  2264. lisp_standard_input = make_stream_handle();
  2265. lisp_error_output = make_stream_handle();
  2266. lisp_trace_output = make_stream_handle();
  2267. lisp_debug_io = make_stream_handle();
  2268. lisp_query_io = make_stream_handle();
  2269. inject_randomness((int)clock());
  2270. set_up_functions(0);
  2271. set_up_variables(0);
  2272. }
  2273. void set_up_functions(CSLbool restartp)
  2274. {
  2275. /*
  2276. * All symbols that have a pointer to C code in their function cell must
  2277. * be set up whether we are in a warm OR a cold start state, because the
  2278. * actual addresses associated with C entrypoints will vary from version
  2279. * to version of the binary of the system.
  2280. */
  2281. int i;
  2282. nil_as_base
  2283. #ifdef COMMON
  2284. /*
  2285. * In Common Lisp mode it could be that the user had something other than the
  2286. * LISP package active when the image was saved. But I want all the symbols
  2287. * that I create or restore here to be in the LISP (or sometimes keyword)
  2288. * package. So I temporarily reset the package here...
  2289. */
  2290. Lisp_Object saved_package = CP;
  2291. CP = find_package("LISP", 4);
  2292. #endif
  2293. function_symbol = make_symbol("function", restartp, function_fn, bad_special2, bad_specialn);
  2294. qheader(function_symbol)|= SYM_SPECIAL_FORM;
  2295. quote_symbol = make_symbol("quote", restartp, quote_fn, bad_special2, bad_specialn);
  2296. qheader(quote_symbol) |= SYM_SPECIAL_FORM;
  2297. progn_symbol = make_symbol("progn", restartp, progn_fn, bad_special2, bad_specialn);
  2298. qheader(progn_symbol) |= SYM_SPECIAL_FORM;
  2299. #ifdef COMMON
  2300. declare_symbol = make_symbol("declare", restartp, declare_fn, bad_special2, bad_specialn);
  2301. qheader(declare_symbol) |= SYM_SPECIAL_FORM;
  2302. #endif
  2303. cons_symbol = make_symbol("cons", restartp, too_few_2, Lcons, wrong_no_2);
  2304. eval_symbol = make_symbol("eval", restartp, Leval, too_many_1, wrong_no_1);
  2305. loadsource_symbol = make_symbol("load-source", restartp, Lload_source, too_many_1, wrong_no_1);
  2306. /*
  2307. * The main bunch of symbols can be handed using a table that
  2308. * gives names and values.
  2309. */
  2310. for (i=0; eval2_setup[i].name != NULL; i++)
  2311. qheader(make_symbol(eval2_setup[i].name,
  2312. restartp,
  2313. eval2_setup[i].one,
  2314. eval2_setup[i].two,
  2315. eval2_setup[i].n)) |= SYM_SPECIAL_FORM;
  2316. for (i=0; eval3_setup[i].name != NULL; i++)
  2317. qheader(make_symbol(eval3_setup[i].name,
  2318. restartp,
  2319. eval3_setup[i].one,
  2320. eval3_setup[i].two,
  2321. eval3_setup[i].n)) |= SYM_SPECIAL_FORM;
  2322. create_symbols(arith06_setup, restartp);
  2323. create_symbols(arith08_setup, restartp);
  2324. create_symbols(arith10_setup, restartp);
  2325. create_symbols(arith12_setup, restartp);
  2326. create_symbols(char_setup, restartp);
  2327. create_symbols(eval1_setup, restartp);
  2328. create_symbols(funcs1_setup, restartp);
  2329. create_symbols(funcs2_setup, restartp);
  2330. create_symbols(funcs3_setup, restartp);
  2331. create_symbols(print_setup, restartp);
  2332. create_symbols(read_setup, restartp);
  2333. create_symbols(restart_setup, restartp);
  2334. /*
  2335. create_symbols(mpi_setup, restartp);
  2336. */
  2337. /*
  2338. * Although almost everything is mappeed into upper case in a Common Lisp
  2339. * world I will preserve the case of symbols defined un u01 to u12.
  2340. */
  2341. create_symbols(u01_setup, restartp | 2);
  2342. create_symbols(u02_setup, restartp | 2);
  2343. create_symbols(u03_setup, restartp | 2);
  2344. create_symbols(u04_setup, restartp | 2);
  2345. create_symbols(u05_setup, restartp | 2);
  2346. create_symbols(u06_setup, restartp | 2);
  2347. create_symbols(u07_setup, restartp | 2);
  2348. create_symbols(u08_setup, restartp | 2);
  2349. create_symbols(u09_setup, restartp | 2);
  2350. create_symbols(u10_setup, restartp | 2);
  2351. create_symbols(u11_setup, restartp | 2);
  2352. create_symbols(u12_setup, restartp | 2);
  2353. #ifdef NAG
  2354. create_symbols(asp_setup, restartp);
  2355. create_symbols(nag_setup, restartp);
  2356. create_symbols(socket_setup, restartp);
  2357. create_symbols(xdr_setup, restartp);
  2358. create_symbols(grep_setup, restartp);
  2359. create_symbols(axfns_setup, restartp);
  2360. create_symbols(gr_setup, restartp);
  2361. #endif
  2362. #ifdef MEMORY_TRACE
  2363. memory_comment(13); /* tail end of setup */
  2364. #endif
  2365. #ifdef COMMON
  2366. CP = saved_package;
  2367. #endif
  2368. }
  2369. static int MS_CDECL alpha0(const void *a, const void *b)
  2370. {
  2371. return strcmp(*(const char **)a, *(const char **)b);
  2372. }
  2373. static int MS_CDECL alpha1(const void *a, const void *b)
  2374. {
  2375. return strcmp(1+*(const char **)a, 1+*(const char **)b);
  2376. }
  2377. static void set_up_variables(CSLbool restartp)
  2378. {
  2379. Lisp_Object nil = C_nil;
  2380. int i;
  2381. #ifdef COMMON
  2382. Lisp_Object saved_package = CP;
  2383. CP = find_package("LISP", 4);
  2384. #endif
  2385. qvalue(macroexpand_hook) = make_symbol("funcall", restartp, Lfuncall1, Lfuncall2, Lfuncalln);
  2386. input_libraries = make_undefined_symbol("input-libraries");
  2387. qheader(input_libraries) |= SYM_SPECIAL_FORM;
  2388. qvalue(input_libraries) = nil;
  2389. for (i=number_of_fasl_paths-1; i>=0; i--)
  2390. qvalue(input_libraries) = cons(SPID_LIBRARY + (((int32)i)<<20),
  2391. qvalue(input_libraries));
  2392. output_library = make_undefined_symbol("output-library");
  2393. qheader(output_library) |= SYM_SPECIAL_FORM;
  2394. qvalue(output_library) = output_directory < 0 ? nil :
  2395. SPID_LIBRARY + (((int32)output_directory)<<20);
  2396. /*
  2397. * The Lisp variable lispsystem* gets set here. (in COMMON mode it is
  2398. * the variable *features*)
  2399. * Its value is a list.
  2400. * csl says I am a CSL Lisp
  2401. * (executable . "string") name of current executable (if available)
  2402. * pipes do I support open-pipe?
  2403. * (version . "string") eg "2.11"
  2404. * (name . "string") eg "MSDOS/386"
  2405. * (opsys . id) unix/msdos/riscos/win32/finder/riscos/...
  2406. * id unix/msdos etc again...
  2407. * win32s qualifier when (opsys . win32) is set
  2408. * win95 ditto
  2409. * help help mechanism provided within Lisp
  2410. * (native . number) native code tag
  2411. * (c-code . number) u01.c through u12.c define n functions
  2412. *
  2413. * In COMMON mode the tags on the *features* list are generally in the
  2414. * keyword package. Otherwise they are just regular symbols. This makes it
  2415. * slightly hard to use code that tests this list in a generic environment!
  2416. */
  2417. {
  2418. #ifdef COMMON
  2419. Lisp_Object n = features_symbol;
  2420. Lisp_Object w;
  2421. char opsys[32];
  2422. char *p1 = opsys, *p2 = OPSYS;
  2423. while ((*p1++ = toupper(*p2++)) != 0);
  2424. *p1 = 0;
  2425. w = cons(make_keyword(opsys), nil);
  2426. #else
  2427. Lisp_Object n = make_undefined_symbol("lispsystem*");
  2428. Lisp_Object w = cons(make_keyword(OPSYS), nil), w1;
  2429. qheader(n) |= SYM_SPECIAL_VAR;
  2430. #endif
  2431. defined_symbols = 0;
  2432. count_symbols(u01_setup); count_symbols(u02_setup);
  2433. count_symbols(u03_setup); count_symbols(u04_setup);
  2434. count_symbols(u05_setup); count_symbols(u06_setup);
  2435. count_symbols(u07_setup); count_symbols(u08_setup);
  2436. count_symbols(u09_setup); count_symbols(u10_setup);
  2437. count_symbols(u11_setup); count_symbols(u12_setup);
  2438. #ifdef COMMON
  2439. /*
  2440. * A gratuitous misery here is the need to make COMMON words
  2441. * upper case.
  2442. */
  2443. w = acons(make_keyword("OPSYS"),
  2444. make_undefined_symbol(OPSYS), w);
  2445. w = acons(make_keyword("NATIVE"),
  2446. fixnum_of_int(native_code_tag), w);
  2447. w = acons(make_keyword("C-CODE"),
  2448. fixnum_of_int(defined_symbols), w);
  2449. #ifdef WINDOWS_NT
  2450. if (win32s==1) w = cons(make_keyword("WIN32S"), w);
  2451. else if (win32s==2) w = cons(make_keyword("WIN95"), w);
  2452. #endif
  2453. #ifdef PIPES
  2454. #ifdef PIPES_SOMETIMES
  2455. if (pipes_today)
  2456. #endif
  2457. w = cons(make_keyword("PIPES"), w);
  2458. #endif
  2459. #ifdef HELP_SYSTEM
  2460. w = cons(make_keyword("HELP"), w);
  2461. #endif
  2462. w = cons(make_keyword("RECORD_GET"), w);
  2463. if (program_name[0] != 0)
  2464. w = acons(make_keyword("EXECUTABLE"),
  2465. make_string(program_name), w);
  2466. w = acons(make_keyword("NAME"), make_string(IMPNAME), w);
  2467. w = acons(make_keyword("VERSION"), make_string(VERSION), w);
  2468. w = cons(make_keyword("CCL"), w);
  2469. w = cons(make_keyword("COMMON-LISP"), w);
  2470. #else /* !COMMON */
  2471. w = acons(make_keyword("opsys"),
  2472. make_undefined_symbol(OPSYS), w);
  2473. w = acons(make_keyword("native"),
  2474. fixnum_of_int(native_code_tag), w);
  2475. w = acons(make_keyword("c-code"),
  2476. fixnum_of_int(defined_symbols), w);
  2477. #ifdef WINDOWS_NT
  2478. if (win32s==1) w = cons(make_keyword("win32s"), w);
  2479. else if (win32s==2) w = cons(make_keyword("win95"), w);
  2480. #endif
  2481. #ifdef PIPES
  2482. #ifdef PIPES_SOMETIMES
  2483. if (pipes_today)
  2484. #endif
  2485. w = cons(make_keyword("pipes"), w);
  2486. #endif
  2487. #ifdef HELP_SYSTEM
  2488. w = cons(make_keyword("help"), w);
  2489. #endif
  2490. #ifdef RECORD_GET
  2491. w = cons(make_keyword("record_get"), w);
  2492. #endif
  2493. if (program_name[0] != 0)
  2494. w = acons(make_keyword("executable"),
  2495. make_string(program_name), w);
  2496. w = acons(make_keyword("name"), make_string(IMPNAME), w);
  2497. w = acons(make_keyword("version"), make_string(VERSION), w);
  2498. w = cons(make_keyword("csl"), w);
  2499. /*
  2500. * Ha Ha a trick here - if a symbol ADDSQ is defined I view this image
  2501. * as being one for REDUCE and push that information onto lispsystem*,
  2502. * and I also reset the "about box" information (if using cwin).
  2503. */
  2504. w1 = make_undefined_symbol("addsq");
  2505. if (qfn1(w1) != undefined1)
  2506. { w = cons(make_keyword("reduce"), w);
  2507. /*
  2508. * I then inspect VERSION!* to try to see whether I have 3.6 or 3.7
  2509. */
  2510. w1 = qvalue(make_undefined_symbol("version*"));
  2511. if (is_vector(w1) &&
  2512. type_of_header(vechdr(w1)) == TYPE_STRING)
  2513. {
  2514. #ifdef CWIN
  2515. int n = length_of_header(vechdr(w1))-4;
  2516. sprintf(about_box_title, "About %.*s",
  2517. (n > 31-strlen("About ") ? 31-strlen("About ") : n),
  2518. &celt(w1, 0));
  2519. sprintf(about_box_description, "%.*s",
  2520. (n > 31 ? 31 : n),
  2521. &celt(w1, 0));
  2522. /*
  2523. * 3.6 did not have copyright1!* etc info, so take it specially here.
  2524. */
  2525. if (memcmp(&celt(w1, 0), "REDUCE 3.6", 10) == 0)
  2526. { strcpy(about_box_rights_1, "Copyright RAND 1995");
  2527. strcpy(about_box_rights_2, "Copyright Codemist 1996");
  2528. }
  2529. else
  2530. { w1 = qvalue(make_undefined_symbol("copyright1*"));
  2531. if (is_vector(w1) &&
  2532. type_of_header(vechdr(w1)) == TYPE_STRING)
  2533. { n = length_of_header(vechdr(w1))-4;
  2534. sprintf(about_box_rights_1, "%.*s",
  2535. n > 31 ? 31 : n, &celt(w1, 0));
  2536. }
  2537. else strcpy(about_box_rights_1, "Copyright A C Hearn/RAND");
  2538. w1 = qvalue(make_undefined_symbol("copyright2*"));
  2539. if (is_vector(w1) &&
  2540. type_of_header(vechdr(w1)) == TYPE_STRING)
  2541. { n = length_of_header(vechdr(w1))-4;
  2542. sprintf(about_box_rights_2, "%.*s",
  2543. n > 31 ? 31 : n, &celt(w1, 0));
  2544. }
  2545. else strcpy(about_box_rights_2, "Copyright Codemist Ltd");
  2546. }
  2547. #endif
  2548. }
  2549. else
  2550. {
  2551. #ifdef CWIN
  2552. strcpy(about_box_title, "About REDUCE");
  2553. strcpy(about_box_description, "REDUCE");
  2554. strcpy(about_box_rights_1, "Copyright A C Hearn/RAND");
  2555. strcpy(about_box_rights_2, "Copyright Codemist Ltd");
  2556. #endif
  2557. }
  2558. }
  2559. #endif
  2560. qheader(n) |= SYM_SPECIAL_VAR;
  2561. qvalue(n) = w;
  2562. }
  2563. #ifdef COMMON
  2564. /*
  2565. * Floating point characteristics are taken from <float.h> where it is
  2566. * supposed that the C compiler involved has got the values correct.
  2567. * I do this every time the system is loaded rather than just when an
  2568. * image is cold-created. This is because an image file may have been created
  2569. * on a system differing from the one on which it is used. Mayve in fact
  2570. * IEEE arithmetic is ALMOST universal and I am being too cautious here?
  2571. */
  2572. { Lisp_Object w;
  2573. make_constant("short-float-epsilon",
  2574. make_sfloat(16.0*FLT_EPSILON));
  2575. make_constant("single-float-epsilon",
  2576. make_boxfloat(FLT_EPSILON, TYPE_SINGLE_FLOAT));
  2577. make_constant("double-float-epsilon",
  2578. make_boxfloat(DBL_EPSILON, TYPE_DOUBLE_FLOAT));
  2579. /* For now "long" = "double" */
  2580. make_constant("long-float-epsilon",
  2581. make_boxfloat(DBL_EPSILON, TYPE_LONG_FLOAT));
  2582. /*
  2583. * I assume that I have a radix 2 representation, and float-negative-epsilon
  2584. * is just half float-epsilon. Correct me if I am wrong...
  2585. */
  2586. make_constant("short-float-negative-epsilon",
  2587. make_sfloat(16.0*FLT_EPSILON/2.0));
  2588. make_constant("single-float-negative-epsilon",
  2589. make_boxfloat(FLT_EPSILON/2.0, TYPE_SINGLE_FLOAT));
  2590. make_constant("double-float-negative-epsilon",
  2591. make_boxfloat(DBL_EPSILON/2.0, TYPE_DOUBLE_FLOAT));
  2592. /* For now "long" = "double" */
  2593. make_constant("long-float-negative-epsilon",
  2594. make_boxfloat(DBL_EPSILON/2.0, TYPE_LONG_FLOAT));
  2595. /*
  2596. * I hope that the C header file gets extremal values correct. Note that
  2597. * because make_sfloat() truncates (rather than rounding) it should give
  2598. * correct values for most-positive-short-float etc
  2599. */
  2600. make_constant("most-positive-short-float",
  2601. make_sfloat(FLT_MAX));
  2602. make_constant("most-positive-single-float",
  2603. make_boxfloat(FLT_MAX, TYPE_SINGLE_FLOAT));
  2604. make_constant("most-positive-double-float",
  2605. make_boxfloat(DBL_MAX, TYPE_DOUBLE_FLOAT));
  2606. make_constant("most-positive-long-float",
  2607. make_boxfloat(DBL_MAX, TYPE_LONG_FLOAT));
  2608. /*
  2609. * Here I assume that the floating point representation is sign-and-magnitude
  2610. * and hence symmetric about zero.
  2611. */
  2612. make_constant("most-negative-short-float",
  2613. make_sfloat(-FLT_MAX));
  2614. make_constant("most-negative-single-float",
  2615. make_boxfloat(-FLT_MAX, TYPE_SINGLE_FLOAT));
  2616. make_constant("most-negative-double-float",
  2617. make_boxfloat(-DBL_MAX, TYPE_DOUBLE_FLOAT));
  2618. make_constant("most-negative-long-float",
  2619. make_boxfloat(-DBL_MAX, TYPE_LONG_FLOAT));
  2620. /*
  2621. * The "least-xxx" set of values did not consider the case of denormalised
  2622. * numbers too carefully in ClTl-1, so in ClTl-2 there are elaborations. I
  2623. * believe that a proper C header file <float.h> will make the macros that
  2624. * I use here refer to NORMALISED values, so the numeric results I use
  2625. * here will not be quite proper (ie there are smaller floats that are
  2626. * un-normalised). But I will ignore that worry just for now.
  2627. */
  2628. make_constant("least-positive-short-float",
  2629. make_sfloat(FLT_MIN));
  2630. make_constant("least-positive-single-float",
  2631. make_boxfloat(FLT_MIN, TYPE_SINGLE_FLOAT));
  2632. make_constant("least-positive-double-float",
  2633. make_boxfloat(DBL_MIN, TYPE_DOUBLE_FLOAT));
  2634. make_constant("least-positive-long-float",
  2635. make_boxfloat(DBL_MIN, TYPE_LONG_FLOAT));
  2636. make_constant("least-negative-short-float",
  2637. make_sfloat(-FLT_MIN));
  2638. make_constant("least-negative-single-float",
  2639. make_boxfloat(-FLT_MIN, TYPE_SINGLE_FLOAT));
  2640. make_constant("least-negative-double-float",
  2641. make_boxfloat(-DBL_MIN, TYPE_DOUBLE_FLOAT));
  2642. make_constant("least-negative-long-float",
  2643. make_boxfloat(-DBL_MIN, TYPE_LONG_FLOAT));
  2644. /*
  2645. * The bunch here are intended to be NORMALISED numbers, while the unqualified
  2646. * ones above may not be.
  2647. */
  2648. make_constant("least-positive-normalized-short-float",
  2649. make_sfloat(FLT_MIN));
  2650. make_constant("least-positive-normalized-single-float",
  2651. make_boxfloat(FLT_MIN, TYPE_SINGLE_FLOAT));
  2652. make_constant("least-positive-normalized-double-float",
  2653. make_boxfloat(DBL_MIN, TYPE_DOUBLE_FLOAT));
  2654. make_constant("least-positive-normalized-long-float",
  2655. make_boxfloat(DBL_MIN, TYPE_LONG_FLOAT));
  2656. make_constant("least-negative-normalized-short-float",
  2657. make_sfloat(-FLT_MIN));
  2658. make_constant("least-negative-normalized-single-float",
  2659. make_boxfloat(-FLT_MIN, TYPE_SINGLE_FLOAT));
  2660. make_constant("least-negative-normalized-double-float",
  2661. make_boxfloat(-DBL_MIN, TYPE_DOUBLE_FLOAT));
  2662. make_constant("least-negative-normalized-long-float",
  2663. make_boxfloat(-DBL_MIN, TYPE_LONG_FLOAT));
  2664. #ifdef UNIX_TIMES
  2665. /* /*
  2666. * ACN believes that the following is misguided, since the time-reading
  2667. * function (defined in fns1.c) that CCL provides always returns its answer
  2668. * in milliseconds. This the 1000 below is NOT as arbitrary as all that, it
  2669. * represents the unit that CCL (across all platforms) returns time
  2670. * measurements in. The UNIX_TIMES macro is set on Unix systems to
  2671. * influence whether the times() function or clock() is used to read
  2672. * time, where in the former case Unix makes it possible to separate
  2673. * user and system time.
  2674. */
  2675. /* UNIX_TIMES is set in machine.h and will usually be HZ. */
  2676. make_constant("internal-time-units-per-second",
  2677. fixnum_of_int(UNIX_TIMES));
  2678. #else
  2679. make_constant("internal-time-units-per-second",
  2680. fixnum_of_int(1000)); /* Arbitrary figure */
  2681. #endif
  2682. }
  2683. #endif
  2684. #ifdef MEMORY_TRACE
  2685. memory_comment(3); /* creating symbols */
  2686. #endif
  2687. charvec = getvector_init(257<<2, nil);
  2688. faslvec = nil;
  2689. faslgensyms = nil;
  2690. qheader(terminal_io = make_undefined_symbol("*terminal-io*"))
  2691. |= SYM_SPECIAL_VAR;
  2692. qheader(standard_input = make_undefined_symbol("*standard-input*"))
  2693. |= SYM_SPECIAL_VAR;
  2694. qheader(standard_output = make_undefined_symbol("*standard-output*"))
  2695. |= SYM_SPECIAL_VAR;
  2696. qheader(error_output = make_undefined_symbol("*error-output*"))
  2697. |= SYM_SPECIAL_VAR;
  2698. qheader(trace_output = make_undefined_symbol("*trace-output*"))
  2699. |= SYM_SPECIAL_VAR;
  2700. qheader(debug_io = make_undefined_symbol("*debug-io*"))
  2701. |= SYM_SPECIAL_VAR;
  2702. qheader(query_io = make_undefined_symbol("*query-io*"))
  2703. |= SYM_SPECIAL_VAR;
  2704. stream_type(lisp_work_stream) = make_undefined_symbol("work-stream");
  2705. { Lisp_Object f = lisp_terminal_io;
  2706. stream_type(f) = make_undefined_symbol("terminal-stream");
  2707. set_stream_read_fn(f, char_from_terminal);
  2708. set_stream_read_other(f, read_action_terminal);
  2709. set_stream_write_fn(f, char_to_terminal);
  2710. set_stream_write_other(f, write_action_terminal);
  2711. qvalue(terminal_io) = f;
  2712. f = lisp_standard_input;
  2713. stream_type(f) = make_undefined_symbol("synonym-stream");
  2714. #ifdef COMMON
  2715. /*
  2716. * If I do not have COMMON defined I will take a slight short cut here and
  2717. * make reading from *standard-input* read directly from the terminal. For
  2718. * full Common Lisp compatibility I think *standard-input* is required to
  2719. * be a synonym stream that will dynamically look at the value of the variable
  2720. * *terminal-io* every time it does anything. Ugh, since people who assign to
  2721. * or re-bind *terminal-io* seem to me to be asking for terrible trouble!
  2722. */
  2723. set_stream_read_fn(f, char_from_synonym);
  2724. #else
  2725. set_stream_read_fn(f, char_from_terminal);
  2726. #endif
  2727. set_stream_read_other(f, read_action_synonym);
  2728. stream_read_data(f) = terminal_io;
  2729. qvalue(standard_input) = f;
  2730. f = lisp_standard_output;
  2731. stream_type(f) = make_undefined_symbol("synonym-stream");
  2732. #ifdef COMMON
  2733. set_stream_write_fn(f, char_to_synonym);
  2734. #else
  2735. set_stream_write_fn(f, char_to_terminal);
  2736. #endif
  2737. set_stream_write_other(f, write_action_synonym);
  2738. stream_write_data(f) = terminal_io;
  2739. qvalue(standard_output) = f;
  2740. f = lisp_error_output;
  2741. stream_type(f) = make_undefined_symbol("synonym-stream");
  2742. #ifdef COMMON
  2743. set_stream_write_fn(f, char_to_synonym);
  2744. #else
  2745. set_stream_write_fn(f, char_to_terminal);
  2746. #endif
  2747. set_stream_write_other(f, write_action_synonym);
  2748. stream_write_data(f) = terminal_io;
  2749. qvalue(error_output) = f;
  2750. f = lisp_trace_output;
  2751. stream_type(f) = make_undefined_symbol("synonym-stream");
  2752. #ifdef COMMON
  2753. set_stream_write_fn(f, char_to_synonym);
  2754. #else
  2755. set_stream_write_fn(f, char_to_terminal);
  2756. #endif
  2757. set_stream_write_other(f, write_action_synonym);
  2758. stream_write_data(f) = terminal_io;
  2759. qvalue(trace_output) = f;
  2760. f = lisp_debug_io;
  2761. stream_type(f) = make_undefined_symbol("synonym-stream");
  2762. #ifdef COMMON
  2763. set_stream_read_fn(f, char_from_synonym);
  2764. #else
  2765. set_stream_read_fn(f, char_from_terminal);
  2766. #endif
  2767. set_stream_read_other(f, read_action_synonym);
  2768. stream_read_data(f) = terminal_io;
  2769. #ifdef COMMON
  2770. set_stream_write_fn(f, char_to_synonym);
  2771. #else
  2772. set_stream_write_fn(f, char_to_terminal);
  2773. #endif
  2774. set_stream_write_other(f, write_action_synonym);
  2775. stream_write_data(f) = terminal_io;
  2776. qvalue(debug_io) = f;
  2777. f = lisp_query_io;
  2778. stream_type(f) = make_undefined_symbol("synonym-stream");
  2779. #ifdef COMMON
  2780. set_stream_read_fn(f, char_from_synonym);
  2781. #else
  2782. set_stream_read_fn(f, char_from_terminal);
  2783. #endif
  2784. set_stream_read_other(f, read_action_synonym);
  2785. stream_read_data(f) = terminal_io;
  2786. #ifdef COMMON
  2787. set_stream_write_fn(f, char_to_synonym);
  2788. #else
  2789. set_stream_write_fn(f, char_to_terminal);
  2790. #endif
  2791. set_stream_write_other(f, write_action_synonym);
  2792. stream_write_data(f) = terminal_io;
  2793. qvalue(query_io) = f;
  2794. }
  2795. /*
  2796. * I can not handle boffo overflow very well here, but I do really hope that
  2797. * symbols spelt out on the command line will always be fairly short.
  2798. */
  2799. for (i=0; i<number_of_symbols_to_define; i++)
  2800. { CSLbool undef = undefine_this_one[i];
  2801. char *s = symbols_to_define[i];
  2802. if (undef)
  2803. { Lisp_Object n = make_undefined_symbol(s);
  2804. qvalue(n) = unset_var;
  2805. }
  2806. else
  2807. { char buffer[256];
  2808. char *p = buffer;
  2809. int c;
  2810. Lisp_Object n, v;
  2811. while ((c = *s++) != 0 && c != '=') *p++ = c;
  2812. *p = 0;
  2813. n = make_undefined_symbol(buffer);
  2814. push(n);
  2815. if (c == 0) v = lisp_true;
  2816. else
  2817. {
  2818. /*
  2819. * I have been having a big difficulty here, caused by the inconsistent and
  2820. * awkward behaviours of various shells and "make" utilities. In a tidy
  2821. * and simple world I might like a command-line option -Dxx=yyy to allow
  2822. * arbitrary text for yyy terminating it at the next whitespace. Then yyy
  2823. * could be processed by the Lisp reader so that numbers, symbols, strings
  2824. * etc could be specified. However I find that things I often want to
  2825. * use involve characters such as "\" and ":" (as components of file-names
  2826. * on some machines), and sometimes "make" treats these as terminators, or
  2827. * wants to do something magic with "\". If I put things within quote marks
  2828. * then sometimes the quotes get passed through to Lisp and sometimes not.
  2829. * This is all a BIG misery in a multi-platform situation! As a fresh
  2830. * attempt to inject sanity I will always convert yyy to a Lisp string. If
  2831. * it is specified with leading and trailing '"' marks I will strip them. Thus
  2832. * both -Dxxx=yyy and -Dxxx="yyy" will leave the variable xxx set to the
  2833. * string "yyy". Then as a Lisp user I can parse the string if I need to
  2834. * interpret it as something else.
  2835. */
  2836. #ifndef PASS_PREDEFINES_THROUGH_READER
  2837. if (*s == '"') /* Convert "yyy" to just yyy */
  2838. { p = ++s;
  2839. while (*p != 0) p++;
  2840. if (*--p == '"') *p = 0;
  2841. }
  2842. #endif
  2843. v = make_string(s);
  2844. #ifdef PASS_PREDEFINES_THROUGH_READER
  2845. v = Lexplodec(nil, v);
  2846. v = Lcompress(nil, v);
  2847. /*
  2848. * The above will first make the value in -Dname=value into a string,
  2849. * then explode it into a list, and compress back - the effect is as if the
  2850. * original value had been passed through the regular Lisp READ function,
  2851. * so symbols, numbers and even s-expressions can be parsed. If the
  2852. * parsing fails I (silently) treat the value as just NIL.
  2853. */
  2854. #endif
  2855. nil = C_nil;
  2856. if (exception_pending()) v = flip_exception();
  2857. }
  2858. pop(n);
  2859. qheader(n) |= SYM_SPECIAL_VAR;
  2860. qvalue(n) = v;
  2861. }
  2862. }
  2863. #ifndef COMMON
  2864. #ifdef CWIN
  2865. /*
  2866. * Now if I have the CWIN windowed system (I might want this info elsewhere
  2867. * too if another windowed implementation of CSL can use it) I look in the
  2868. * Lisp variables
  2869. * loadable-packages!*
  2870. * switches!*
  2871. * (both expected to be lists of symbols) and copy info into a couple of
  2872. * C vectors, whence it can go to the window manager and be used to create
  2873. * suitable menus.
  2874. */
  2875. { Lisp_Object w1 = qvalue(make_undefined_symbol("loadable-packages*"));
  2876. Lisp_Object w2;
  2877. int n;
  2878. char *v;
  2879. n = 0;
  2880. for (w2=w1; consp(w2); w2=qcdr(w2)) n++; /* How many? */
  2881. loadable_packages = (char **)malloc((n+1)*sizeof(char *));
  2882. if (loadable_packages != NULL)
  2883. { n = 0;
  2884. for (w2=w1; consp(w2); w2=qcdr(w2))
  2885. { Lisp_Object w3 = qcar(w2);
  2886. int n1;
  2887. if (is_symbol(w3)) w3 = qpname(w3);
  2888. if (!is_vector(w3) ||
  2889. type_of_header(vechdr(w3)) != TYPE_STRING) break;
  2890. n1 = length_of_header(vechdr(w3))-4;
  2891. v = (char *)malloc(n1+1);
  2892. if (v == NULL) break;
  2893. memcpy(v, &celt(w3, 0), n1);
  2894. v[n1] = 0;
  2895. loadable_packages[n++] = v;
  2896. }
  2897. qsort(loadable_packages, n, sizeof(char *), alpha0);
  2898. loadable_packages[n] = NULL;
  2899. }
  2900. w1 = qvalue(make_undefined_symbol("switches*"));
  2901. n = 0;
  2902. for (w2=w1; consp(w2); w2=qcdr(w2)) n++; /* How many? */
  2903. switches = (char **)malloc((n+1)*sizeof(char *));
  2904. if (switches != NULL)
  2905. { n = 0;
  2906. for (w2=w1; consp(w2); w2=qcdr(w2))
  2907. { Lisp_Object w3 = qcar(w2), w4;
  2908. char sname[64];
  2909. int n1;
  2910. if (is_symbol(w3)) w3 = qpname(w3);
  2911. if (!is_vector(w3) ||
  2912. type_of_header(vechdr(w3)) != TYPE_STRING) break;
  2913. n1 = length_of_header(vechdr(w3))-4;
  2914. if (n1 > 60) break;
  2915. sprintf(sname, "*%.*s", n1, &celt(w3, 0));
  2916. w4 = make_undefined_symbol(sname);
  2917. v = (char *)malloc(n1+2);
  2918. if (v == NULL) break;
  2919. /*
  2920. * The first character records the current state of the switch.
  2921. */
  2922. if (qvalue(w4) == nil) v[0] = 'n';
  2923. else v[0] = 'y';
  2924. memcpy(v+1, &celt(w3, 0), n1);
  2925. v[n1+1] = 0;
  2926. switches[n++] = v;
  2927. }
  2928. qsort(switches, n, sizeof(char *), alpha1);
  2929. switches[n] = NULL;
  2930. }
  2931. }
  2932. #endif
  2933. #endif
  2934. #ifdef COMMON
  2935. CP = saved_package;
  2936. #endif
  2937. }
  2938. unsigned char registration_data[REGISTRATION_SIZE];
  2939. CSLbool MD5_busy;
  2940. unsigned char unpredictable[256];
  2941. static int n_unpredictable = 0;
  2942. static CSLbool unpredictable_pending = 0;
  2943. void inject_randomness(int n)
  2944. {
  2945. unpredictable[n_unpredictable++] ^= (n % 255);
  2946. if (n_unpredictable >= 256)
  2947. { n_unpredictable = 0;
  2948. unpredictable_pending = YES;
  2949. }
  2950. if (unpredictable_pending & !MD5_busy)
  2951. { MD5_Init();
  2952. MD5_Update(unpredictable, sizeof(unpredictable));
  2953. MD5_Final(unpredictable);
  2954. unpredictable_pending = NO;
  2955. }
  2956. }
  2957. /*
  2958. * For some of what follows I think I need to show that I have considered
  2959. * the issue of export regulations.
  2960. *
  2961. * What I have here is MD5 (and when and if I feel keen SHA-1). I observe
  2962. * that MD5, SHA-1 and DSA are made available as part of Sun's Java
  2963. * Development Kit in the version that can be downloade freely from their
  2964. * servers. They have a separate Java Cryptography Extension within which
  2965. * they keep things that are subject to USA export regulations. I take this
  2966. * as encouragement to believe that these three algorithms are not subject
  2967. * to USA export limits. I believe such limits to be supersets (ie more
  2968. * restrictive) than ones that apply in the UK and so feel happy about
  2969. * including the implementations that I do here. Specifically, although I
  2970. * have extracts from the SSL code which as a whole might give trouble if
  2971. * importen to the USA and the re-exported I only have the message digest
  2972. * bits that should not be so encumbered. I am aware that MD5 is now
  2973. * considered weakish with SHA-1 the improved replacement, but will take the
  2974. * view that I was not aiming for real security on anything anyway!
  2975. */
  2976. /*
  2977. * MD5 message digest code, adapted from Eric Young's version,
  2978. * for which the copyright and disclaimer notices follow. Observe that
  2979. * this code can be adapted and re-used subject to these terms being
  2980. * retained.
  2981. */
  2982. /* crypto/md/md5.c and support files */
  2983. /* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
  2984. * All rights reserved.
  2985. *
  2986. * This package is an SSL implementation written
  2987. * by Eric Young (eay@mincom.oz.au).
  2988. * The implementation was written so as to conform with Netscapes SSL.
  2989. *
  2990. * This library is free for commercial and non-commercial use as long as
  2991. * the following conditions are aheared to. The following conditions
  2992. * apply to all code found in this distribution, be it the RC4, RSA,
  2993. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  2994. * included with this distribution is covered by the same copyright terms
  2995. * except that the holder is Tim Hudson (tjh@mincom.oz.au).
  2996. *
  2997. * Copyright remains Eric Young's, and as such any Copyright notices in
  2998. * the code are not to be removed.
  2999. * If this package is used in a product, Eric Young should be given attribution
  3000. * as the author of the parts of the library used.
  3001. * This can be in the form of a textual message at program startup or
  3002. * in documentation (online or textual) provided with the package.
  3003. *
  3004. * Redistribution and use in source and binary forms, with or without
  3005. * modification, are permitted provided that the following conditions
  3006. * are met:
  3007. * 1. Redistributions of source code must retain the copyright
  3008. * notice, this list of conditions and the following disclaimer.
  3009. * 2. Redistributions in binary form must reproduce the above copyright
  3010. * notice, this list of conditions and the following disclaimer in the
  3011. * documentation and/or other materials provided with the distribution.
  3012. * 3. All advertising materials mentioning features or use of this software
  3013. * must display the following acknowledgement:
  3014. * "This product includes cryptographic software written by
  3015. * Eric Young (eay@mincom.oz.au)"
  3016. * The word 'cryptographic' can be left out if the rouines from the library
  3017. * being used are not cryptographic related :-).
  3018. * 4. If you include any Windows specific code (or a derivative thereof) from
  3019. * the apps directory (application code) you must include an acknowledgement:
  3020. * "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
  3021. *
  3022. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  3023. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  3024. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  3025. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  3026. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  3027. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  3028. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  3029. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  3030. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  3031. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  3032. * SUCH DAMAGE.
  3033. *
  3034. * The licence and distribution terms for any publically available version or
  3035. * derivative of this code cannot be changed. i.e. this code cannot simply be
  3036. * copied and put under another distribution licence
  3037. * [including the GNU Public Licence.]
  3038. */
  3039. /*
  3040. * End of Eric Young's copyright and disclaimer notice.
  3041. *
  3042. * The changes made by A C Norman remove some optimisation to leave shorter
  3043. * code (I will not be using this in speed-critical applications) and
  3044. * adjusting the style and layout to agree with other Codemist utilities.
  3045. */
  3046. #define MD5_CBLOCK 64
  3047. #define MD5_LBLOCK 16
  3048. static unsigned32 MD5_A, MD5_B, MD5_C, MD5_D;
  3049. static unsigned32 MD5_Nl;
  3050. static int MD5_num;
  3051. static unsigned32 MD5_data[MD5_CBLOCK];
  3052. #define F(x,y,z) ((((y) ^ (z)) & (x)) ^ (z))
  3053. #define G(x,y,z) ((((x) ^ (y)) & (z)) ^ (y))
  3054. #define H(x,y,z) ((x) ^ (y) ^ (z))
  3055. #define I(x,y,z) (((x) | (~(z))) ^ (y))
  3056. #define ROTATE(a,n) (((a)<<(n))|((a)>>(32-(n))))
  3057. #define R0(a,b,c,d,k,s,t) { \
  3058. a+=((k)+(t)+F((b),(c),(d))); \
  3059. a=ROTATE(a,s); \
  3060. a+=b; }
  3061. #define R1(a,b,c,d,k,s,t) { \
  3062. a+=((k)+(t)+G((b),(c),(d))); \
  3063. a=ROTATE(a,s); \
  3064. a+=b; }
  3065. #define R2(a,b,c,d,k,s,t) { \
  3066. a+=((k)+(t)+H((b),(c),(d))); \
  3067. a=ROTATE(a,s); \
  3068. a+=b; }
  3069. #define R3(a,b,c,d,k,s,t) { \
  3070. a+=((k)+(t)+I((b),(c),(d))); \
  3071. a=ROTATE(a,s); \
  3072. a+=b; }
  3073. /*
  3074. * Implemented from RFC1321 The MD5 Message-Digest Algorithm
  3075. */
  3076. void MD5_Init()
  3077. {
  3078. MD5_busy = YES;
  3079. MD5_A = 0x67452301;
  3080. MD5_B = 0xefcdab89;
  3081. MD5_C = 0x98badcfe;
  3082. MD5_D = 0x10325476;
  3083. MD5_Nl = 0;
  3084. MD5_num = 0;
  3085. }
  3086. static unsigned char byte_order_test[4] = {1, 0, 0, 0};
  3087. static void md5_block()
  3088. {
  3089. unsigned32 A=MD5_A, B=MD5_B, C=MD5_C, D=MD5_D;
  3090. int i;
  3091. /*
  3092. * Here I re-write the buffer so that it now behaves as if it is
  3093. * an array of 32-bit words in native computer representation. On
  3094. * many machines the code here will have no effect at all apart from
  3095. * consuming a little time. I do a little test first to see if
  3096. * it is really needed.
  3097. */
  3098. unsigned32 *p = MD5_data;
  3099. unsigned char *q = (unsigned char *)p;
  3100. if (((unsigned32 *)byte_order_test)[0] != 1)
  3101. { for (i=0; i<MD5_LBLOCK; i++)
  3102. { unsigned32 w = *q++;
  3103. w |= *q++ << 8;
  3104. w |= *q++ << 16;
  3105. w |= *q++ << 24;
  3106. *p++ = w;
  3107. }
  3108. }
  3109. p = MD5_data;
  3110. /* Round 0 */
  3111. R0(A,B,C,D,p[ 0], 7,0xd76aa478); R0(D,A,B,C,p[ 1],12,0xe8c7b756);
  3112. R0(C,D,A,B,p[ 2],17,0x242070db); R0(B,C,D,A,p[ 3],22,0xc1bdceee);
  3113. R0(A,B,C,D,p[ 4], 7,0xf57c0faf); R0(D,A,B,C,p[ 5],12,0x4787c62a);
  3114. R0(C,D,A,B,p[ 6],17,0xa8304613); R0(B,C,D,A,p[ 7],22,0xfd469501);
  3115. R0(A,B,C,D,p[ 8], 7,0x698098d8); R0(D,A,B,C,p[ 9],12,0x8b44f7af);
  3116. R0(C,D,A,B,p[10],17,0xffff5bb1); R0(B,C,D,A,p[11],22,0x895cd7be);
  3117. R0(A,B,C,D,p[12], 7,0x6b901122); R0(D,A,B,C,p[13],12,0xfd987193);
  3118. R0(C,D,A,B,p[14],17,0xa679438e); R0(B,C,D,A,p[15],22,0x49b40821);
  3119. /* Round 1 */
  3120. R1(A,B,C,D,p[ 1], 5,0xf61e2562); R1(D,A,B,C,p[ 6], 9,0xc040b340);
  3121. R1(C,D,A,B,p[11],14,0x265e5a51); R1(B,C,D,A,p[ 0],20,0xe9b6c7aa);
  3122. R1(A,B,C,D,p[ 5], 5,0xd62f105d); R1(D,A,B,C,p[10], 9,0x02441453);
  3123. R1(C,D,A,B,p[15],14,0xd8a1e681); R1(B,C,D,A,p[ 4],20,0xe7d3fbc8);
  3124. R1(A,B,C,D,p[ 9], 5,0x21e1cde6); R1(D,A,B,C,p[14], 9,0xc33707d6);
  3125. R1(C,D,A,B,p[ 3],14,0xf4d50d87); R1(B,C,D,A,p[ 8],20,0x455a14ed);
  3126. R1(A,B,C,D,p[13], 5,0xa9e3e905); R1(D,A,B,C,p[ 2], 9,0xfcefa3f8);
  3127. R1(C,D,A,B,p[ 7],14,0x676f02d9); R1(B,C,D,A,p[12],20,0x8d2a4c8a);
  3128. /* Round 2 */
  3129. R2(A,B,C,D,p[ 5], 4,0xfffa3942); R2(D,A,B,C,p[ 8],11,0x8771f681);
  3130. R2(C,D,A,B,p[11],16,0x6d9d6122); R2(B,C,D,A,p[14],23,0xfde5380c);
  3131. R2(A,B,C,D,p[ 1], 4,0xa4beea44); R2(D,A,B,C,p[ 4],11,0x4bdecfa9);
  3132. R2(C,D,A,B,p[ 7],16,0xf6bb4b60); R2(B,C,D,A,p[10],23,0xbebfbc70);
  3133. R2(A,B,C,D,p[13], 4,0x289b7ec6); R2(D,A,B,C,p[ 0],11,0xeaa127fa);
  3134. R2(C,D,A,B,p[ 3],16,0xd4ef3085); R2(B,C,D,A,p[ 6],23,0x04881d05);
  3135. R2(A,B,C,D,p[ 9], 4,0xd9d4d039); R2(D,A,B,C,p[12],11,0xe6db99e5);
  3136. R2(C,D,A,B,p[15],16,0x1fa27cf8); R2(B,C,D,A,p[ 2],23,0xc4ac5665);
  3137. /* Round 3 */
  3138. R3(A,B,C,D,p[ 0], 6,0xf4292244); R3(D,A,B,C,p[ 7],10,0x432aff97);
  3139. R3(C,D,A,B,p[14],15,0xab9423a7); R3(B,C,D,A,p[ 5],21,0xfc93a039);
  3140. R3(A,B,C,D,p[12], 6,0x655b59c3); R3(D,A,B,C,p[ 3],10,0x8f0ccc92);
  3141. R3(C,D,A,B,p[10],15,0xffeff47d); R3(B,C,D,A,p[ 1],21,0x85845dd1);
  3142. R3(A,B,C,D,p[ 8], 6,0x6fa87e4f); R3(D,A,B,C,p[15],10,0xfe2ce6e0);
  3143. R3(C,D,A,B,p[ 6],15,0xa3014314); R3(B,C,D,A,p[13],21,0x4e0811a1);
  3144. R3(A,B,C,D,p[ 4], 6,0xf7537e82); R3(D,A,B,C,p[11],10,0xbd3af235);
  3145. R3(C,D,A,B,p[ 2],15,0x2ad7d2bb); R3(B,C,D,A,p[ 9],21,0xeb86d391);
  3146. MD5_A += A;
  3147. MD5_B += B;
  3148. MD5_C += C;
  3149. MD5_D += D;
  3150. }
  3151. void MD5_Update(unsigned char *data, int len)
  3152. {
  3153. unsigned char *p = (unsigned char *)MD5_data;
  3154. /*
  3155. * The full MD5 procedure allows for encoding strings of up to
  3156. * around 2^64 bits. I will restrict myself to 2^32 so I can just ignore
  3157. * the high word of the bit-count.
  3158. */
  3159. MD5_Nl += len<<3; /* Counts in BITS not BYTES here */
  3160. while (len != 0)
  3161. { p[MD5_num++] = *data++;
  3162. len--;
  3163. if (MD5_num == MD5_CBLOCK)
  3164. { md5_block();
  3165. MD5_num = 0;
  3166. }
  3167. }
  3168. }
  3169. void MD5_Final(unsigned char *md)
  3170. {
  3171. unsigned32 l = MD5_Nl;
  3172. unsigned char *p = (unsigned char *)MD5_data;
  3173. p[MD5_num++] = 0x80;
  3174. if (MD5_num >= MD5_CBLOCK-8)
  3175. { while (MD5_num < MD5_CBLOCK) p[MD5_num++] = 0;
  3176. md5_block();
  3177. MD5_num = 0;
  3178. }
  3179. while (MD5_num < MD5_CBLOCK-8) p[MD5_num++] = 0;
  3180. p[MD5_num++] = l;
  3181. p[MD5_num++] = l>>8;
  3182. p[MD5_num++] = l>>16;
  3183. p[MD5_num++] = l>>24;
  3184. p[MD5_num++] = 0;
  3185. p[MD5_num++] = 0;
  3186. p[MD5_num++] = 0;
  3187. p[MD5_num++] = 0;
  3188. md5_block();
  3189. p = md;
  3190. l = MD5_A; *p++ = l; *p++ = l>>8; *p++ = l>>16; *p++ = l>>24;
  3191. l = MD5_B; *p++ = l; *p++ = l>>8; *p++ = l>>16; *p++ = l>>24;
  3192. l = MD5_C; *p++ = l; *p++ = l>>8; *p++ = l>>16; *p++ = l>>24;
  3193. l = MD5_D; *p++ = l; *p++ = l>>8; *p++ = l>>16; *p++ = l>>24;
  3194. MD5_busy = NO;
  3195. }
  3196. unsigned char *MD5(unsigned char *d, int n, unsigned char *md)
  3197. {
  3198. if (n < 0) n = strlen((char *)d);
  3199. MD5_Init();
  3200. MD5_Update(d, n);
  3201. MD5_Final(md);
  3202. return md;
  3203. }
  3204. #ifdef STAND_ALONE_TESTING_OF_MD5_CODE
  3205. int main(int argc, char *argv[])
  3206. {
  3207. int i;
  3208. unsigned char mm[16];
  3209. MD5("", 0, mm);
  3210. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3211. printf("\n");
  3212. MD5("a", 1, mm);
  3213. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3214. printf("\n");
  3215. MD5("abc", 3, mm);
  3216. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3217. printf("\n");
  3218. MD5("message digest", -1, mm);
  3219. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3220. printf("\n");
  3221. MD5("abcdefghijklmnopqrstuvwxyz", -1, mm);
  3222. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3223. printf("\n");
  3224. MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", -1, mm);
  3225. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3226. printf("\n");
  3227. MD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890", -1, mm);
  3228. for (i=0; i<16; i++) printf("%.2x", mm[i] & 0xff);
  3229. printf("\n");
  3230. return 0;
  3231. }
  3232. #endif
  3233. /*
  3234. * This is the end of the Eric Young code - what follows is Codemist
  3235. * original code again.
  3236. *
  3237. *
  3238. *
  3239. * The next bit is for an experiment in controlling access to image files
  3240. * etc. It is solely intended for use in implementing this access control
  3241. * and is not made available as something that a CSL/Reduce user can access
  3242. * directly. It favours high speed above other things, and much of its
  3243. * security in use will be based on nobody having a real incentive to
  3244. * poke at it since CSL-based images will not be expected to be of
  3245. * sufficient value to justify the effort.
  3246. */
  3247. int crypt_active;
  3248. unsigned char *crypt_buffer;
  3249. int crypt_count;
  3250. /*
  3251. * The following code was generated by running the program "gencry.c",
  3252. * within which you can find the comments that explain what is going on. The
  3253. * macro TIME_TEST could be defined to make this file more of a self-
  3254. * contained test of its performance, but to do that you probably need
  3255. * to look at the raw output from gencry.c.
  3256. *
  3257. * word length = 32
  3258. * shift register length = 65
  3259. * tap at position 18
  3260. * shuffle-buffer size = 4096
  3261. */
  3262. #ifdef TIME_TEST
  3263. #include <stdio.h>
  3264. #include <time.h>
  3265. #define N 10000000 /* parameters for time test */
  3266. #define NSTARTS 4000
  3267. #define NTINY 50000000
  3268. #define KEY "Arthurs's sample key"
  3269. typedef unsigned int unsigned32;
  3270. #endif /* TIME_TEST */
  3271. static unsigned32 lf[65], mix[4096];
  3272. #define R(x) ((x) >> 20)
  3273. #define S(x) ((x) >> 18)
  3274. #define T(x) ((x) << 13)
  3275. /*
  3276. * static unsigned char byte_order_test[] =
  3277. * {1, 0, 0, 0, 0, 0, 0, 0};
  3278. */
  3279. #define CRYPT_BLOCK_SIZE 128
  3280. void crypt_get_block(unsigned char block[CRYPT_BLOCK_SIZE])
  3281. {
  3282. unsigned32 *b = (unsigned32 *)block;
  3283. int n;
  3284. lf[0] -= lf[18]; lf[1] ^= lf[19];
  3285. lf[2] -= lf[20]; lf[3] += lf[21];
  3286. lf[4] += lf[22]; lf[5] -= lf[23];
  3287. lf[6] ^= lf[24]; lf[7] -= lf[25];
  3288. lf[8] += lf[26]; lf[9] ^= lf[27];
  3289. lf[10] -= lf[28]; lf[11] -= lf[29];
  3290. lf[12] += lf[30]; lf[13] += lf[31];
  3291. lf[14] -= lf[32]; lf[15] ^= lf[33];
  3292. lf[16] -= lf[34]; lf[17] += lf[35];
  3293. lf[18] += lf[36]; lf[19] += lf[37];
  3294. lf[20] -= lf[38]; lf[21] -= lf[39];
  3295. lf[22] ^= lf[40]; lf[23] += lf[41];
  3296. lf[24] -= lf[42]; lf[25] -= lf[43];
  3297. lf[26] += lf[44]; lf[27] += lf[45];
  3298. lf[28] -= lf[46]; lf[29] ^= lf[47];
  3299. lf[30] -= lf[48]; lf[31] += lf[49];
  3300. lf[32] -= lf[50]; lf[33] ^= lf[51];
  3301. lf[34] -= lf[52]; lf[35] ^= lf[53];
  3302. lf[36] += lf[54]; lf[37] += lf[55];
  3303. lf[38] ^= lf[56]; lf[39] ^= lf[57];
  3304. lf[40] += lf[58]; lf[41] -= lf[59];
  3305. lf[42] ^= lf[60]; lf[43] += lf[61];
  3306. lf[44] += lf[62]; lf[45] ^= lf[63];
  3307. lf[46] ^= lf[64]; lf[47] -= lf[0];
  3308. lf[48] ^= lf[1]; lf[49] ^= lf[2];
  3309. lf[50] ^= lf[3]; lf[51] ^= lf[4];
  3310. lf[52] ^= lf[5]; lf[53] ^= lf[6];
  3311. lf[54] += lf[7]; lf[55] -= lf[8];
  3312. lf[56] -= lf[9]; lf[57] ^= lf[10];
  3313. lf[58] -= lf[11]; lf[59] -= lf[12];
  3314. lf[60] ^= lf[13]; lf[61] += lf[14];
  3315. lf[62] ^= lf[15]; lf[63] -= lf[16];
  3316. lf[64] -= lf[17];
  3317. n = R(lf[0]); b[0] = mix[n]; mix[n] = (lf[54] + S(lf[29])) ^ T(lf[5]);
  3318. n = R(lf[1]); b[1] = mix[n]; mix[n] = ~(lf[39] + S(lf[47])) + T(lf[15]);
  3319. n = R(lf[2]); b[2] = mix[n]; mix[n] = (lf[25] + S(lf[14])) + T(lf[38]);
  3320. n = R(lf[4]); b[3] = mix[n]; mix[n] = ~(lf[48] - S(lf[40])) ^ T(lf[10]);
  3321. n = R(lf[5]); b[4] = mix[n]; mix[n] = (lf[44] - S(lf[55])) - T(lf[49]);
  3322. n = R(lf[6]); b[5] = mix[n]; mix[n] = ~(lf[9] ^ S(lf[37])) + T(lf[50]);
  3323. n = R(lf[8]); b[6] = mix[n]; mix[n] = (lf[64] ^ S(lf[51])) + T(lf[8]);
  3324. n = R(lf[9]); b[7] = mix[n]; mix[n] = ~(lf[11] - S(lf[35])) - T(lf[21]);
  3325. n = R(lf[10]); b[8] = mix[n]; mix[n] = (lf[20] ^ S(lf[21])) ^ T(lf[3]);
  3326. n = R(lf[12]); b[9] = mix[n]; mix[n] = ~(lf[6] ^ S(lf[31])) - T(lf[61]);
  3327. n = R(lf[13]); b[10] = mix[n]; mix[n] = (lf[3] - S(lf[16])) ^ T(lf[16]);
  3328. n = R(lf[14]); b[11] = mix[n]; mix[n] = ~(lf[17] - S(lf[53])) - T(lf[2]);
  3329. n = R(lf[16]); b[12] = mix[n]; mix[n] = (lf[27] + S(lf[42])) - T(lf[33]);
  3330. n = R(lf[17]); b[13] = mix[n]; mix[n] = ~(lf[28] + S(lf[63])) - T(lf[46]);
  3331. n = R(lf[18]); b[14] = mix[n]; mix[n] = (lf[10] - S(lf[46])) + T(lf[35]);
  3332. n = R(lf[20]); b[15] = mix[n]; mix[n] = ~(lf[53] - S(lf[10])) - T(lf[27]);
  3333. n = R(lf[21]); b[16] = mix[n]; mix[n] = (lf[4] + S(lf[18])) - T(lf[7]);
  3334. n = R(lf[22]); b[17] = mix[n]; mix[n] = ~(lf[43] + S(lf[64])) ^ T(lf[45]);
  3335. n = R(lf[24]); b[18] = mix[n]; mix[n] = (lf[14] + S(lf[26])) + T(lf[44]);
  3336. n = R(lf[25]); b[19] = mix[n]; mix[n] = ~(lf[23] ^ S(lf[38])) + T(lf[58]);
  3337. n = R(lf[26]); b[20] = mix[n]; mix[n] = (lf[47] + S(lf[59])) ^ T(lf[47]);
  3338. n = R(lf[28]); b[21] = mix[n]; mix[n] = ~(lf[63] - S(lf[36])) - T(lf[57]);
  3339. n = R(lf[29]); b[22] = mix[n]; mix[n] = (lf[56] + S(lf[4])) + T(lf[19]);
  3340. n = R(lf[30]); b[23] = mix[n]; mix[n] = ~(lf[42] - S(lf[52])) - T(lf[56]);
  3341. n = R(lf[32]); b[24] = mix[n]; mix[n] = (lf[37] + S(lf[3])) - T(lf[63]);
  3342. n = R(lf[33]); b[25] = mix[n]; mix[n] = ~(lf[32] + S(lf[1])) - T(lf[12]);
  3343. n = R(lf[34]); b[26] = mix[n]; mix[n] = (lf[62] - S(lf[39])) - T(lf[31]);
  3344. n = R(lf[36]); b[27] = mix[n]; mix[n] = ~(lf[2] ^ S(lf[44])) ^ T(lf[18]);
  3345. n = R(lf[37]); b[28] = mix[n]; mix[n] = (lf[24] ^ S(lf[50])) ^ T(lf[55]);
  3346. n = R(lf[38]); b[29] = mix[n]; mix[n] = ~(lf[22] + S(lf[27])) - T(lf[32]);
  3347. n = R(lf[40]); b[30] = mix[n]; mix[n] = (lf[51] + S(lf[33])) + T(lf[0]);
  3348. n = R(lf[41]); b[31] = mix[n]; mix[n] = ~(lf[52] ^ S(lf[19])) - T(lf[26]);
  3349. n = R(lf[42]); mix[n] = (lf[5] ^ S(lf[41])) + T(lf[28]);
  3350. n = R(lf[44]); mix[n] = ~(lf[30] ^ S(lf[15])) - T(lf[30]);
  3351. n = R(lf[45]); mix[n] = (lf[45] + S(lf[24])) ^ T(lf[51]);
  3352. n = R(lf[46]); mix[n] = ~(lf[13] + S(lf[49])) - T(lf[11]);
  3353. n = R(lf[48]); mix[n] = (lf[16] + S(lf[11])) - T(lf[39]);
  3354. n = R(lf[49]); mix[n] = ~(lf[57] - S(lf[43])) - T(lf[60]);
  3355. n = R(lf[50]); mix[n] = (lf[49] + S(lf[48])) ^ T(lf[25]);
  3356. n = R(lf[52]); mix[n] = ~(lf[34] - S(lf[22])) ^ T(lf[23]);
  3357. n = R(lf[53]); mix[n] = (lf[18] + S(lf[6])) + T(lf[1]);
  3358. n = R(lf[54]); mix[n] = ~(lf[29] + S(lf[61])) - T(lf[64]);
  3359. n = R(lf[56]); mix[n] = (lf[59] ^ S(lf[45])) - T(lf[41]);
  3360. n = R(lf[57]); mix[n] = ~(lf[36] - S(lf[32])) + T(lf[37]);
  3361. n = R(lf[58]); mix[n] = (lf[40] + S(lf[60])) + T(lf[14]);
  3362. n = R(lf[60]); mix[n] = ~(lf[1] + S(lf[56])) ^ T(lf[36]);
  3363. n = R(lf[61]); mix[n] = (lf[8] ^ S(lf[5])) ^ T(lf[17]);
  3364. n = R(lf[62]); mix[n] = ~(lf[31] ^ S(lf[17])) ^ T(lf[52]);
  3365. /* The test this way around favours Intel etc byte order */
  3366. if (((unsigned int *)byte_order_test)[0] != 1)
  3367. { int i;
  3368. for (i=0; i<32; i++)
  3369. { unsigned32 w = b[i];
  3370. unsigned32 b0, b1, b2, b3;
  3371. b0 = (w >> 24) & 0xffU;
  3372. b1 = (w >> 8) & 0xff00U;
  3373. b2 = (w << 8) & 0xff0000U;
  3374. b3 = (w << 24) & 0xff000000U;
  3375. b[i] = b0 | b1 | b2 | b3;
  3376. }
  3377. }
  3378. return;
  3379. }
  3380. void crypt_init(char *key)
  3381. {
  3382. char *pk = key;
  3383. unsigned char junk[CRYPT_BLOCK_SIZE];
  3384. int i, j;
  3385. unsigned32 w = 0;
  3386. for (i=0; i<260; i++)
  3387. { int k = *pk++;
  3388. if (k == 0) pk = key; /* Cycle key (inc. terminating 0) */
  3389. w = (w << 8) | (k & 0xff);
  3390. if ((i % 4) == 3) lf[i/4] = w;
  3391. }
  3392. for (i=0; i<4096; i++) mix[i] = 0;
  3393. for (i=0; i<8; i++)
  3394. { for (j=0; j<65; j++)
  3395. lf[j] = (lf[j] << 10) | (lf[j] >> 22);
  3396. lf[0] |= 1;
  3397. for (j=0; j<64; j++)
  3398. crypt_get_block(junk);
  3399. }
  3400. for (i=0; i<4096;)
  3401. { int j;
  3402. crypt_get_block(junk);
  3403. for (j=0; j<32; j++)
  3404. { unsigned32 r = junk[4*j];
  3405. r = (r << 8) | junk[4*j+1];
  3406. r = (r << 8) | junk[4*j+2];
  3407. r = (r << 8) | junk[4*j+3];
  3408. if (r == 0) continue;
  3409. mix[i++] ^= junk[j];
  3410. if (i == 4096) break;
  3411. }
  3412. }
  3413. for (i=0; i<192; i++)
  3414. crypt_get_block(junk);
  3415. return;
  3416. }
  3417. #ifdef TIME_TEST
  3418. /*
  3419. * The main program here does not do anything of real interest. It
  3420. * runs both the key-setup and the main loop lots of times and reports
  3421. * how long it all takes.
  3422. *
  3423. * Here is some sample output from a Pentium-II 400Mhz system
  3424. *
  3425. * [02faf080] 7.60 nanoseconds to do tiny loop
  3426. * 1.25 milliseconds to startup
  3427. * rate = 104.86 megabytes per second
  3428. * 79 a7 e1 52 2e 84 09 ce d0 3d 45 b2 52 2d b6 c7
  3429. * 9b ee 57 25 68 58 b7 44 42 51 1c c7 de 69 0f 89
  3430. * 98 6c cd 45 e0 a1 d4 04 a3 be 3d 5f 93 64 c9 d9
  3431. * b9 47 28 59 d0 99 5a 35 56 fd 89 e6 48 4f a4 88
  3432. * 7e dd 31 76 2b 8e 96 fa d0 6f d7 30 9c 3c 01 97
  3433. * 8a 54 93 c0 02 1d 26 df 31 2b 7b 92 56 51 fa 47
  3434. * 92 13 39 47 45 d2 b5 33 2b f6 cc 62 ec 73 00 40
  3435. * 66 ab 37 f5 1d 21 3a a9 b8 da 35 ac 04 f1 3b 53
  3436. *
  3437. */
  3438. int main(int argc, char *argv[])
  3439. {
  3440. clock_t c0, c1;
  3441. unsigned char r[CRYPT_BLOCK_SIZE];
  3442. int i, j = 0;
  3443. double rate;
  3444. c0 = clock();
  3445. for (i=0; i<(NTINY+1); i++) j ^= i;
  3446. c1 = clock();
  3447. printf("[%.8x] %.2f nanoseconds to do tiny loop\n", j,
  3448. 1.0e9*(double)(c1-c0)/((double)CLOCKS_PER_SEC*(double)(NTINY+1)));
  3449. c0 = clock();
  3450. for (i=0; i<NSTARTS; i++) crypt_init(KEY);
  3451. c1 = clock();
  3452. printf("%.2f milliseconds to startup\n",
  3453. 1000.0*(double)(c1-c0)/((double)CLOCKS_PER_SEC*(double)NSTARTS));
  3454. c0 = clock();
  3455. for (i=0; i<N; i++) crypt_get_block(r);
  3456. c1 = clock();
  3457. rate = (double)N*(double)CRYPT_BLOCK_SIZE*(double)CLOCKS_PER_SEC/
  3458. ((double)(c1-c0)*1.0e6);
  3459. printf("rate = %.2f megabytes per second\n", rate);
  3460. for (i=0; i<128; i++)
  3461. { printf("%.2x ", r[i]);
  3462. if ((i % 16) == 15) printf("\n");
  3463. }
  3464. return 0;
  3465. }
  3466. #endif /* TIME_TEST */
  3467. #undef R
  3468. #undef S
  3469. #undef T
  3470. /* End of generated code... */
  3471. static void get_checksum(const setup_type *p)
  3472. {
  3473. while (p->name!=NULL) p++;
  3474. if (p->one != NULL && p->two != NULL)
  3475. { unsigned char *w = (unsigned char *)p->two;
  3476. MD5_Update(w, strlen((char *)w));
  3477. }
  3478. }
  3479. void get_user_files_checksum(unsigned char *b)
  3480. {
  3481. MD5_Init();
  3482. get_checksum(u01_setup);
  3483. get_checksum(u02_setup);
  3484. get_checksum(u03_setup);
  3485. get_checksum(u04_setup);
  3486. get_checksum(u05_setup);
  3487. get_checksum(u06_setup);
  3488. get_checksum(u07_setup);
  3489. get_checksum(u08_setup);
  3490. get_checksum(u09_setup);
  3491. get_checksum(u10_setup);
  3492. get_checksum(u11_setup);
  3493. get_checksum(u12_setup);
  3494. MD5_Final(b);
  3495. }
  3496. char *crypt_keys[CRYPT_KEYS];
  3497. void setup(int restartp, double store_size)
  3498. {
  3499. int i;
  3500. Lisp_Object nil;
  3501. #ifdef TIME_TEST_CRYPTO
  3502. /* *********** Now I will time my encryption stuff... ************* */
  3503. clock_t t0 = clock(), t1;
  3504. #define TRIALS 2000
  3505. for (i=0; i<TRIALS; i++) crypt_init("Arthur\'s secret key");
  3506. t1 = clock();
  3507. term_printf("Time for crypt_init = %.2g milliseconds\n",
  3508. 1.0e3*((double)(t1-t0))/(double)CLOCKS_PER_SEC/(double)TRIALS);
  3509. #define TRIALS1 1000000
  3510. { unsigned char buffer[CRYPT_BLOCK];
  3511. t0 = clock();
  3512. for (i=0; i<TRIALS1; i++) crypt_get_block(buffer);
  3513. t1 = clock();
  3514. }
  3515. { double Mbytes = (double)TRIALS1*(double)CRYPT_BLOCK/1000000.0;
  3516. double seconds = (double)(t1-t0)/(double)CLOCKS_PER_SEC;
  3517. term_printf("Time to do 1 Mbyte = %.2g seconds\n", seconds/Mbytes);
  3518. term_printf("Ie %.4g Mbytes per sec\n", Mbytes/seconds);
  3519. }
  3520. /* **************** end of temp gunk ***************** */
  3521. #endif
  3522. crypt_active = -1;
  3523. #ifdef DEBUG
  3524. /*
  3525. * Just a sanity check so that if I add entries in this table but to
  3526. * not change the recorded size I will be warned.
  3527. */
  3528. if ((entry_table_size+1)*sizeof(entry_point) != sizeof(entries_table))
  3529. { term_printf("entry_table_size badly set up in externs.h\n");
  3530. my_exit(EXIT_FAILURE);
  3531. }
  3532. if (sizeof(int32) != 4 ||
  3533. sizeof(unsigned32) != 4 ||
  3534. #ifdef SIXTY_FOUR_BIT
  3535. sizeof(int64) != 8 ||
  3536. sizeof(unsigned64) != 8 ||
  3537. #endif
  3538. #ifndef ILP64
  3539. sizeof(int16) != 2 ||
  3540. sizeof(unsigned16) != 2 ||
  3541. #endif
  3542. sizeof(int8) != 1 ||
  3543. sizeof(unsigned8) != 1)
  3544. { term_printf("Some datatype size is wrongly set up in tags.h\n");
  3545. my_exit(EXIT_FAILURE);
  3546. }
  3547. #endif
  3548. if (restartp & 2) init_heap_segments(store_size);
  3549. restartp &= 1;
  3550. nil = C_nil;
  3551. #ifdef TIDY_UP_MEMORY_AT_START
  3552. /*
  3553. * The following should not be needed, feature on big machines could be
  3554. * expensive. The code is left in case it helps with repeatability in
  3555. * the face of accesses to uninitialised locations (ie BUGS)
  3556. */
  3557. for (i=0; i<pages_count; i++)
  3558. memset(pages[i], 0, (size_t)CSL_PAGE_SIZE+8);
  3559. memset(stacksegment, 0, (size_t)stack_segsize*CSL_PAGE_SIZE+8);
  3560. memset(nilsegment, 0, (size_t)NIL_SEGMENT_SIZE);
  3561. #endif
  3562. stack = stackbase;
  3563. exit_tag = exit_value = nil;
  3564. exit_reason = UNWIND_NULL;
  3565. if (restartp & 1)
  3566. { char junkbuf[120];
  3567. char filename[LONGEST_LEGAL_FILENAME];
  3568. if (IopenRoot(filename, 0))
  3569. { term_printf("\n+++ Image file \"%s\" can not be read\n",
  3570. filename);
  3571. my_exit(EXIT_FAILURE);
  3572. }
  3573. /*
  3574. * I read input via a buffer of size FREAD_BUFFER_SIZE, which I pre-fill
  3575. * at this stage before I even try to read anything
  3576. */
  3577. fread_ptr = (unsigned char *)stack;
  3578. fread_count = Iread(fread_ptr, FREAD_BUFFER_SIZE);
  3579. /*
  3580. * I can adjust here (automatically) for whatever compression threshold
  3581. * had been active when the image file was created.
  3582. */
  3583. compression_worth_while = 128;
  3584. crypt_active = -1;
  3585. Cfread(junkbuf, 112);
  3586. { int fg = junkbuf[111];
  3587. while (fg != 0) compression_worth_while <<= 1, fg--;
  3588. fg = junkbuf[110];
  3589. while (fg != 0) crypt_active++, fg--;
  3590. if (crypt_active >= 0 &&
  3591. crypt_active < CRYPT_KEYS &&
  3592. crypt_keys[crypt_active] != NULL)
  3593. { crypt_init(crypt_keys[crypt_active]);
  3594. if ((crypt_buffer =
  3595. (unsigned char *)malloc(CRYPT_BLOCK))
  3596. == NULL) crypt_active = -1; /* And will then fail */
  3597. crypt_count = 0;
  3598. }
  3599. }
  3600. if (init_flags & INIT_VERBOSE)
  3601. { term_printf("Created: %.25s\n", &junkbuf[64]);
  3602. /* Time dump was taken */
  3603. }
  3604. { unsigned char chk[16];
  3605. get_user_files_checksum(chk);
  3606. for (i=0; i<16; i++)
  3607. { if (chk[i] != (junkbuf[90+i] & 0xff))
  3608. { term_printf(
  3609. "\n+++ Image file belongs with a different version\n");
  3610. term_printf(
  3611. " of the executable file (incompatible code\n");
  3612. term_printf(
  3613. " has been optimised into C and incorporated)\n");
  3614. term_printf(
  3615. " Unable to use this image file, so stopping\n");
  3616. my_exit(EXIT_FAILURE);
  3617. }
  3618. }
  3619. }
  3620. /*
  3621. * To make things more responsive for the user I will display a
  3622. * banner rather early (before reading the bulk of the image file).
  3623. * The banner that I will display is one provided to be by PRESERVE.
  3624. */
  3625. { Ihandle save;
  3626. char b[64];
  3627. int i;
  3628. Icontext(&save);
  3629. #define BANNER_CODE (-1002)
  3630. if (IopenRoot(filename, BANNER_CODE)) b[0] = 0;
  3631. else
  3632. { for (i=0; i<64; i++) b[i] = Igetc();
  3633. IcloseInput(NO);
  3634. }
  3635. Irestore_context(save);
  3636. /*
  3637. * A banner set via startup-banner takes precedence over one from preserve.
  3638. */
  3639. if (b[0] != 0)
  3640. { term_printf("%s\n", b);
  3641. ensure_screen();
  3642. }
  3643. else if (junkbuf[0] != 0)
  3644. { term_printf("%s\n", junkbuf);
  3645. ensure_screen();
  3646. }
  3647. }
  3648. #ifdef PREVIOUS_ATTEMPT_AT_AUTHENTICATION
  3649. { char username[48];
  3650. unsigned char sig[16];
  3651. unsigned32 hash;
  3652. int c1;
  3653. unsigned char *s = &registration_data[4];
  3654. MD5_Update(s, 48);
  3655. MD5_Final(sig);
  3656. if (memcmp(sig, &s[48], 16) != 0)
  3657. { term_printf("************************************\n");
  3658. term_printf("* Registration data seems corrupt *\n");
  3659. term_printf("* Please check with your supplier. *\n");
  3660. term_printf("************************************\n");
  3661. /*
  3662. * If I were really feeling confident - or possibly mean - I would
  3663. * just exit from the system here declaring that my consistency check
  3664. * had failed and that hence there was a presumption that somebody had
  3665. * tried to patch or otherwise bodge my registration-name code. At least for
  3666. * a few weeks I will not go that far, and I will expect that the annoying
  3667. * message above will do quite enough. The real security that I can enforce
  3668. * is pretty low anyway, and so overall I believe that the policy adopted
  3669. * here is probably adequate.
  3670. */
  3671. }
  3672. else
  3673. { hash = *s++;
  3674. hash |= *s++<<8;
  3675. hash |= *s++<<16;
  3676. hash |= *s++<<24;
  3677. for (c1=0;c1<44;c1++)
  3678. { hash = 69069*hash + 314159;
  3679. username[c1] = *s++ ^ (hash >> 16);
  3680. }
  3681. while (c1 > 0 && username[--c1] == ' ') username[c1] = 0;
  3682. term_printf("Registered to: %s\n", username);
  3683. }
  3684. }
  3685. #endif /* AUTHORIZATION */
  3686. /*
  3687. * From here on if crypt_active is >= 0 I will be decoding an encrypted
  3688. * image file.
  3689. */
  3690. Cfread(junkbuf, 8);
  3691. Cfread((char *)nil, sizeof(Lisp_Object)*last_nil_offset);
  3692. copy_out_of_nilseg(YES);
  3693. #ifndef COMMON
  3694. qheader(nil) = TAG_ODDS+TYPE_SYMBOL+SYM_SPECIAL_VAR;/* BEFORE nil... */
  3695. #endif
  3696. if ((byteflip & 0xffff0000U) == 0x56780000U)
  3697. { flip_needed = NO;
  3698. old_fp_rep = (int)(byteflip & FP_MASK);
  3699. old_page_bits = (int)((byteflip >> 8) & 0x1f);
  3700. }
  3701. else if ((byteflip & 0x0000ffffU) == 0x00007856U)
  3702. { flip_needed = YES;
  3703. old_fp_rep = (int)(flip_bytes_fn(byteflip) & FP_MASK);
  3704. old_page_bits = (int)((flip_bytes_fn(byteflip) >> 8) & 0x1f);
  3705. }
  3706. else
  3707. { term_printf("\n+++ The checkpoint file is corrupt\n");
  3708. /*
  3709. * Note: I use different numbers to check byte-ordering on segmented feature
  3710. * non-segmented systems, since the heap image formats are not compatible.
  3711. * A result will be that use of the wrong sort of image will lead to a
  3712. * "checkpoint file corrupt" message rather than a more serious shambles.
  3713. */
  3714. my_exit(EXIT_FAILURE);
  3715. }
  3716. if (old_page_bits == 0) old_page_bits = 16; /* Old default value */
  3717. /*
  3718. * I could in fact recover in the case that old_page_bits < PAGE_BITS, since
  3719. * I could just map the old small pages into the new big ones with a little
  3720. * padding where needed. I will not do that JUST yet. In general it will
  3721. * not be possible to load an image with large pages into a CSL that only
  3722. * has small ones - eg there might be some vector that just would not fit
  3723. * in the small page size. Even discounting that worry rearranging the
  3724. * heap to allow for the discontinuities at the smaller page granularity would
  3725. * be pretty painful. Again in the limit something very much akin to the
  3726. * normal garbage collector could probably do it if it ever became really
  3727. * necessary.
  3728. */
  3729. if (old_page_bits != PAGE_BITS)
  3730. { term_printf("\n+++ The checkpoint file was made on a machine\n");
  3731. term_printf("where CSL had been configured with a different page\n");
  3732. term_printf("size. It is not usable with this version.\n");
  3733. my_exit(EXIT_FAILURE);
  3734. }
  3735. /* The saved value of NIL is not needed in this case */
  3736. }
  3737. else
  3738. {
  3739. for (i=first_nil_offset; i<last_nil_offset; i++)
  3740. ((Lisp_Object *)nil)[i] = nil;
  3741. copy_out_of_nilseg(NO);
  3742. }
  3743. stacklimit = &stack[stack_segsize*CSL_PAGE_SIZE/4-200];
  3744. /* allow some slop at end */
  3745. byteflip = 0x56780000 |
  3746. ((int32)current_fp_rep & ~FP_WORD_ORDER) |
  3747. (((int32)PAGE_BITS) << 8);
  3748. native_pages_changed = 0;
  3749. if (restartp) warm_setup();
  3750. else cold_setup();
  3751. if (init_flags & INIT_QUIET) Lverbos(nil, fixnum_of_int(1));
  3752. if (init_flags & INIT_VERBOSE) Lverbos(nil, fixnum_of_int(3));
  3753. #ifndef HOLD_BACK_MEMORY
  3754. /*
  3755. * Here I grab more memory (if I am allowed to) until the proportion of the
  3756. * heap active at the end of garbage collection is less than 1/2. If the
  3757. * attempt to grab more memory fails I clear the bit in init_flags that
  3758. * allows me to try to expand, so I will not waste time again. If
  3759. * HOLD_BACK_MEMORY was asserted (for machines where grabbing all seemingly
  3760. * available memory may cause a crash) I do not try this operation. The
  3761. * aim of keeping the heap less than half full is an heuristic and could be
  3762. * adjusted on the basis of experience with this code.
  3763. */
  3764. if (init_flags & INIT_EXPANDABLE)
  3765. { int32 more = heap_pages_count + vheap_pages_count +
  3766. bps_pages_count + native_pages_count;
  3767. more = 3 *more - pages_count;
  3768. while (more-- > 0)
  3769. { void *page = (void *)my_malloc_1((size_t)(CSL_PAGE_SIZE + 8));
  3770. /*
  3771. * CF the code in gc.c -- I can still use my_malloc_1 here, which makes this
  3772. * code just a tiny bit safer.
  3773. */
  3774. int32 pun = (int32)page;
  3775. int32 pun1 = (int32)((char *)page + CSL_PAGE_SIZE + 8);
  3776. if ((pun ^ pun1) < 0) page = NULL;
  3777. #ifdef ADDRESS_SIGN_UNKNOWN
  3778. if ((pun + address_sign) < 0) page = NULL;
  3779. #else
  3780. #ifdef ADDRESSES_HAVE_TOP_BIT_SET
  3781. if (pun > 0) page = NULL;
  3782. #else
  3783. if (pun < 0) page = NULL;
  3784. #endif
  3785. #endif
  3786. if (page == NULL)
  3787. { init_flags &= ~INIT_EXPANDABLE;
  3788. break;
  3789. }
  3790. else pages[pages_count++] = page;
  3791. }
  3792. }
  3793. #endif
  3794. {
  3795. int32 w = 0;
  3796. #ifndef NO_COPYING_GC
  3797. /*
  3798. * I will make the first garbage collection a copying one if the heap is
  3799. * at most 25% full, or a sliding one if it is more full than that.
  3800. */
  3801. w = heap_pages_count + vheap_pages_count +
  3802. bps_pages_count + native_pages_count;
  3803. gc_method = (pages_count > 3*w);
  3804. #endif
  3805. /*
  3806. * The total store allocated is that used plus that free, including the
  3807. * page set aside for the Lisp stack.
  3808. */
  3809. if (init_flags & INIT_VERBOSE)
  3810. term_printf("Memory allocation: %ld bytes\n",
  3811. (long)CSL_PAGE_SIZE*(pages_count+w+1));
  3812. }
  3813. #ifdef MEMORY_TRACE
  3814. memory_comment(15);
  3815. #endif
  3816. return;
  3817. }
  3818. void copy_into_nilseg(int fg)
  3819. {
  3820. int i;
  3821. Lisp_Object nil = C_nil;
  3822. #ifdef NILSEG_EXTERNS
  3823. if (fg) /* move non list bases too */
  3824. { ((unsigned32 *)nil)[12] = byteflip;
  3825. ((Lisp_Object *)nil)[13] = codefringe;
  3826. ((Lisp_Object volatile *)nil)[14] = codelimit;
  3827. /*
  3828. * The messing around here is to ensure that on 64-bit architectures
  3829. * stacklimit is kept properly aligned.
  3830. */
  3831. #ifdef COMMON
  3832. *(Lisp_Object * volatile *)&((Lisp_Object *)nil)[16] = stacklimit;
  3833. #else
  3834. *(Lisp_Object * volatile *)&((Lisp_Object *)nil)[15] = stacklimit;
  3835. #endif
  3836. ((Lisp_Object *)nil)[18] = fringe;
  3837. ((Lisp_Object volatile *)nil)[19] = heaplimit;
  3838. ((Lisp_Object volatile *)nil)[20] = vheaplimit;
  3839. ((Lisp_Object *)nil)[21] = vfringe;
  3840. ((unsigned32 *)nil)[22] = miscflags;
  3841. ((int32 *)nil)[24] = nwork;
  3842. ((int32 *)nil)[25] = exit_reason;
  3843. ((int32 *)nil)[26] = exit_count;
  3844. ((unsigned32 *)nil)[27] = gensym_ser;
  3845. ((unsigned32 *)nil)[28] = print_precision;
  3846. ((int32 *)nil)[29] = current_modulus;
  3847. ((int32 *)nil)[30] = fastget_size;
  3848. ((int32 *)nil)[31] = package_bits;
  3849. }
  3850. /*
  3851. * Entries 50 and 51 are used for chains of hash tables, and so get
  3852. * very special individual treatment.
  3853. */
  3854. ((Lisp_Object *)nil)[52] = current_package;
  3855. ((Lisp_Object *)nil)[53] = B_reg;
  3856. ((Lisp_Object *)nil)[54] = codevec;
  3857. ((Lisp_Object *)nil)[55] = litvec;
  3858. ((Lisp_Object *)nil)[56] = exit_tag;
  3859. ((Lisp_Object *)nil)[57] = exit_value;
  3860. ((Lisp_Object *)nil)[58] = catch_tags;
  3861. ((Lisp_Object *)nil)[59] = lisp_package;
  3862. ((Lisp_Object *)nil)[60] = boffo;
  3863. ((Lisp_Object *)nil)[61] = charvec;
  3864. ((Lisp_Object *)nil)[62] = sys_hash_table;
  3865. ((Lisp_Object *)nil)[63] = help_index;
  3866. ((Lisp_Object *)nil)[64] = gensym_base;
  3867. ((Lisp_Object *)nil)[65] = err_table;
  3868. ((Lisp_Object *)nil)[66] = supervisor;
  3869. ((Lisp_Object *)nil)[67] = startfn;
  3870. ((Lisp_Object *)nil)[68] = faslvec;
  3871. ((Lisp_Object *)nil)[69] = tracedfn;
  3872. ((Lisp_Object *)nil)[70] = prompt_thing;
  3873. ((Lisp_Object *)nil)[71] = faslgensyms;
  3874. ((Lisp_Object *)nil)[72] = cl_symbols;
  3875. ((Lisp_Object *)nil)[73] = active_stream;
  3876. ((Lisp_Object *)nil)[74] = current_module;
  3877. ((Lisp_Object *)nil)[90] = append_symbol;
  3878. ((Lisp_Object *)nil)[91] = applyhook;
  3879. ((Lisp_Object *)nil)[92] = cfunarg;
  3880. ((Lisp_Object *)nil)[93] = comma_at_symbol;
  3881. ((Lisp_Object *)nil)[94] = comma_symbol;
  3882. ((Lisp_Object *)nil)[95] = compiler_symbol;
  3883. ((Lisp_Object *)nil)[96] = comp_symbol;
  3884. ((Lisp_Object *)nil)[97] = cons_symbol;
  3885. ((Lisp_Object *)nil)[98] = echo_symbol;
  3886. ((Lisp_Object *)nil)[99] = emsg_star;
  3887. ((Lisp_Object *)nil)[100] = evalhook;
  3888. ((Lisp_Object *)nil)[101] = eval_symbol;
  3889. ((Lisp_Object *)nil)[102] = expr_symbol;
  3890. ((Lisp_Object *)nil)[103] = features_symbol;
  3891. ((Lisp_Object *)nil)[104] = fexpr_symbol;
  3892. ((Lisp_Object *)nil)[105] = funarg;
  3893. ((Lisp_Object *)nil)[106] = function_symbol;
  3894. ((Lisp_Object *)nil)[107] = lambda;
  3895. ((Lisp_Object *)nil)[108] = lisp_true;
  3896. ((Lisp_Object *)nil)[109] = lower_symbol;
  3897. ((Lisp_Object *)nil)[110] = macroexpand_hook;
  3898. ((Lisp_Object *)nil)[111] = macro_symbol;
  3899. ((Lisp_Object *)nil)[112] = opt_key;
  3900. ((Lisp_Object *)nil)[113] = prinl_symbol;
  3901. ((Lisp_Object *)nil)[114] = progn_symbol;
  3902. ((Lisp_Object *)nil)[115] = quote_symbol;
  3903. ((Lisp_Object *)nil)[116] = raise_symbol;
  3904. ((Lisp_Object *)nil)[117] = redef_msg;
  3905. ((Lisp_Object *)nil)[118] = rest_key;
  3906. ((Lisp_Object *)nil)[119] = savedef;
  3907. ((Lisp_Object *)nil)[120] = string_char_sym;
  3908. ((Lisp_Object *)nil)[121] = unset_var;
  3909. ((Lisp_Object *)nil)[122] = work_symbol;
  3910. ((Lisp_Object *)nil)[123] = lex_words;
  3911. ((Lisp_Object *)nil)[124] = get_counts;
  3912. ((Lisp_Object *)nil)[125] = fastget_names;
  3913. ((Lisp_Object *)nil)[126] = input_libraries;
  3914. ((Lisp_Object *)nil)[127] = output_library;
  3915. ((Lisp_Object *)nil)[128] = current_file;
  3916. ((Lisp_Object *)nil)[129] = break_function;
  3917. ((Lisp_Object *)nil)[130] = lisp_work_stream;
  3918. ((Lisp_Object *)nil)[131] = lisp_standard_output;
  3919. ((Lisp_Object *)nil)[132] = lisp_standard_input;
  3920. ((Lisp_Object *)nil)[133] = lisp_debug_io;
  3921. ((Lisp_Object *)nil)[134] = lisp_error_output;
  3922. ((Lisp_Object *)nil)[135] = lisp_query_io;
  3923. ((Lisp_Object *)nil)[136] = lisp_terminal_io;
  3924. ((Lisp_Object *)nil)[137] = lisp_trace_output;
  3925. ((Lisp_Object *)nil)[138] = standard_output;
  3926. ((Lisp_Object *)nil)[139] = standard_input;
  3927. ((Lisp_Object *)nil)[140] = debug_io;
  3928. ((Lisp_Object *)nil)[141] = error_output;
  3929. ((Lisp_Object *)nil)[142] = query_io;
  3930. ((Lisp_Object *)nil)[143] = terminal_io;
  3931. ((Lisp_Object *)nil)[144] = trace_output;
  3932. ((Lisp_Object *)nil)[145] = fasl_stream;
  3933. ((Lisp_Object *)nil)[146] = native_code;
  3934. ((Lisp_Object *)nil)[147] = native_symbol;
  3935. ((Lisp_Object *)nil)[148] = traceprint_symbol;
  3936. ((Lisp_Object *)nil)[149] = loadsource_symbol;
  3937. ((Lisp_Object *)nil)[150] = hankaku_symbol;
  3938. #ifdef COMMON
  3939. ((Lisp_Object *)nil)[170] = keyword_package;
  3940. ((Lisp_Object *)nil)[171] = all_packages;
  3941. ((Lisp_Object *)nil)[172] = package_symbol;
  3942. ((Lisp_Object *)nil)[173] = internal_symbol;
  3943. ((Lisp_Object *)nil)[174] = external_symbol;
  3944. ((Lisp_Object *)nil)[175] = inherited_symbol;
  3945. ((Lisp_Object *)nil)[176] = key_key;
  3946. ((Lisp_Object *)nil)[177] = allow_other_keys;
  3947. ((Lisp_Object *)nil)[178] = aux_key;
  3948. ((Lisp_Object *)nil)[179] = format_symbol;
  3949. ((Lisp_Object *)nil)[180] = expand_def_symbol;
  3950. ((Lisp_Object *)nil)[181] = allow_key_key;
  3951. ((Lisp_Object *)nil)[182] = declare_symbol;
  3952. ((Lisp_Object *)nil)[183] = special_symbol;
  3953. #endif
  3954. for (i=0; i<=50; i++)
  3955. ((Lisp_Object *)nil)[work_0_offset+i] = workbase[i];
  3956. #endif /* NILSEG_EXTERNS */
  3957. ((Lisp_Object *)nil)[190] = user_base_0;
  3958. ((Lisp_Object *)nil)[191] = user_base_1;
  3959. ((Lisp_Object *)nil)[192] = user_base_2;
  3960. ((Lisp_Object *)nil)[193] = user_base_3;
  3961. ((Lisp_Object *)nil)[194] = user_base_4;
  3962. ((Lisp_Object *)nil)[195] = user_base_5;
  3963. ((Lisp_Object *)nil)[196] = user_base_6;
  3964. ((Lisp_Object *)nil)[197] = user_base_7;
  3965. ((Lisp_Object *)nil)[198] = user_base_8;
  3966. ((Lisp_Object *)nil)[199] = user_base_9;
  3967. }
  3968. void copy_out_of_nilseg(int fg)
  3969. {
  3970. int i;
  3971. Lisp_Object nil = C_nil;
  3972. #ifdef NILSEG_EXTERNS
  3973. if (fg)
  3974. {
  3975. byteflip = ((unsigned32 *)nil)[12];
  3976. codefringe = ((Lisp_Object *)nil)[13];
  3977. codelimit = ((Lisp_Object volatile *)nil)[14];
  3978. #ifdef COMMON
  3979. stacklimit = *(Lisp_Object *volatile *)&((Lisp_Object *)nil)[16];
  3980. #else
  3981. stacklimit = *(Lisp_Object *volatile *)&((Lisp_Object *)nil)[15];
  3982. #endif
  3983. fringe = ((Lisp_Object *)nil)[18];
  3984. heaplimit = ((Lisp_Object volatile *)nil)[19];
  3985. vheaplimit = ((Lisp_Object volatile *)nil)[20];
  3986. vfringe = ((Lisp_Object *)nil)[21];
  3987. miscflags = ((unsigned32 *)nil)[22];
  3988. nwork = ((int32 *)nil)[24];
  3989. exit_reason = ((int32 *)nil)[25];
  3990. exit_count = ((int32 *)nil)[26];
  3991. gensym_ser = ((unsigned32 *)nil)[27];
  3992. print_precision = ((unsigned32 *)nil)[28];
  3993. current_modulus = ((int32 *)nil)[29];
  3994. fastget_size = ((int32 *)nil)[30];
  3995. package_bits = ((int32 *)nil)[31];
  3996. }
  3997. current_package = ((Lisp_Object *)nil)[52];
  3998. B_reg = ((Lisp_Object *)nil)[53];
  3999. codevec = ((Lisp_Object *)nil)[54];
  4000. litvec = ((Lisp_Object *)nil)[55];
  4001. exit_tag = ((Lisp_Object *)nil)[56];
  4002. exit_value = ((Lisp_Object *)nil)[57];
  4003. catch_tags = ((Lisp_Object *)nil)[58];
  4004. lisp_package = ((Lisp_Object *)nil)[59];
  4005. boffo = ((Lisp_Object *)nil)[60];
  4006. charvec = ((Lisp_Object *)nil)[61];
  4007. sys_hash_table = ((Lisp_Object *)nil)[62];
  4008. help_index = ((Lisp_Object *)nil)[63];
  4009. gensym_base = ((Lisp_Object *)nil)[64];
  4010. err_table = ((Lisp_Object *)nil)[65];
  4011. supervisor = ((Lisp_Object *)nil)[66];
  4012. startfn = ((Lisp_Object *)nil)[67];
  4013. faslvec = ((Lisp_Object *)nil)[68];
  4014. tracedfn = ((Lisp_Object *)nil)[69];
  4015. prompt_thing = ((Lisp_Object *)nil)[70];
  4016. faslgensyms = ((Lisp_Object *)nil)[71];
  4017. cl_symbols = ((Lisp_Object *)nil)[72];
  4018. active_stream = ((Lisp_Object *)nil)[73];
  4019. current_module = ((Lisp_Object *)nil)[74];
  4020. append_symbol = ((Lisp_Object *)nil)[90];
  4021. applyhook = ((Lisp_Object *)nil)[91];
  4022. cfunarg = ((Lisp_Object *)nil)[92];
  4023. comma_at_symbol = ((Lisp_Object *)nil)[93];
  4024. comma_symbol = ((Lisp_Object *)nil)[94];
  4025. compiler_symbol = ((Lisp_Object *)nil)[95];
  4026. comp_symbol = ((Lisp_Object *)nil)[96];
  4027. cons_symbol = ((Lisp_Object *)nil)[97];
  4028. echo_symbol = ((Lisp_Object *)nil)[98];
  4029. emsg_star = ((Lisp_Object *)nil)[99];
  4030. evalhook = ((Lisp_Object *)nil)[100];
  4031. eval_symbol = ((Lisp_Object *)nil)[101];
  4032. expr_symbol = ((Lisp_Object *)nil)[102];
  4033. features_symbol = ((Lisp_Object *)nil)[103];
  4034. fexpr_symbol = ((Lisp_Object *)nil)[104];
  4035. funarg = ((Lisp_Object *)nil)[105];
  4036. function_symbol = ((Lisp_Object *)nil)[106];
  4037. lambda = ((Lisp_Object *)nil)[107];
  4038. lisp_true = ((Lisp_Object *)nil)[108];
  4039. lower_symbol = ((Lisp_Object *)nil)[109];
  4040. macroexpand_hook = ((Lisp_Object *)nil)[110];
  4041. macro_symbol = ((Lisp_Object *)nil)[111];
  4042. opt_key = ((Lisp_Object *)nil)[112];
  4043. prinl_symbol = ((Lisp_Object *)nil)[113];
  4044. progn_symbol = ((Lisp_Object *)nil)[114];
  4045. quote_symbol = ((Lisp_Object *)nil)[115];
  4046. raise_symbol = ((Lisp_Object *)nil)[116];
  4047. redef_msg = ((Lisp_Object *)nil)[117];
  4048. rest_key = ((Lisp_Object *)nil)[118];
  4049. savedef = ((Lisp_Object *)nil)[119];
  4050. string_char_sym = ((Lisp_Object *)nil)[120];
  4051. unset_var = ((Lisp_Object *)nil)[121];
  4052. work_symbol = ((Lisp_Object *)nil)[122];
  4053. lex_words = ((Lisp_Object *)nil)[123];
  4054. get_counts = ((Lisp_Object *)nil)[124];
  4055. fastget_names = ((Lisp_Object *)nil)[125];
  4056. input_libraries = ((Lisp_Object *)nil)[126];
  4057. output_library = ((Lisp_Object *)nil)[127];
  4058. current_file = ((Lisp_Object *)nil)[128];
  4059. break_function = ((Lisp_Object *)nil)[129];
  4060. lisp_work_stream = ((Lisp_Object *)nil)[130];
  4061. lisp_standard_output = ((Lisp_Object *)nil)[131];
  4062. lisp_standard_input = ((Lisp_Object *)nil)[132];
  4063. lisp_debug_io = ((Lisp_Object *)nil)[133];
  4064. lisp_error_output = ((Lisp_Object *)nil)[134];
  4065. lisp_query_io = ((Lisp_Object *)nil)[135];
  4066. lisp_terminal_io = ((Lisp_Object *)nil)[136];
  4067. lisp_trace_output = ((Lisp_Object *)nil)[137];
  4068. standard_output = ((Lisp_Object *)nil)[138];
  4069. standard_input = ((Lisp_Object *)nil)[139];
  4070. debug_io = ((Lisp_Object *)nil)[140];
  4071. error_output = ((Lisp_Object *)nil)[141];
  4072. query_io = ((Lisp_Object *)nil)[142];
  4073. terminal_io = ((Lisp_Object *)nil)[143];
  4074. trace_output = ((Lisp_Object *)nil)[144];
  4075. fasl_stream = ((Lisp_Object *)nil)[145];
  4076. native_code = ((Lisp_Object *)nil)[146];
  4077. native_symbol = ((Lisp_Object *)nil)[147];
  4078. traceprint_symbol = ((Lisp_Object *)nil)[148];
  4079. loadsource_symbol = ((Lisp_Object *)nil)[149];
  4080. hankaku_symbol = ((Lisp_Object *)nil)[150];
  4081. #ifdef COMMON
  4082. keyword_package = ((Lisp_Object *)nil)[170];
  4083. all_packages = ((Lisp_Object *)nil)[171];
  4084. package_symbol = ((Lisp_Object *)nil)[172];
  4085. internal_symbol = ((Lisp_Object *)nil)[173];
  4086. external_symbol = ((Lisp_Object *)nil)[174];
  4087. inherited_symbol = ((Lisp_Object *)nil)[175];
  4088. key_key = ((Lisp_Object *)nil)[176];
  4089. allow_other_keys = ((Lisp_Object *)nil)[177];
  4090. aux_key = ((Lisp_Object *)nil)[178];
  4091. format_symbol = ((Lisp_Object *)nil)[179];
  4092. expand_def_symbol = ((Lisp_Object *)nil)[180];
  4093. allow_key_key = ((Lisp_Object *)nil)[181];
  4094. declare_symbol = ((Lisp_Object *)nil)[182];
  4095. special_symbol = ((Lisp_Object *)nil)[183];
  4096. #endif
  4097. for (i = 0; i<=50; i++)
  4098. workbase[i] = ((Lisp_Object *)nil)[work_0_offset+i];
  4099. #endif /* NILSEG_EXTERNS */
  4100. user_base_0 = ((Lisp_Object *)nil)[190];
  4101. user_base_1 = ((Lisp_Object *)nil)[191];
  4102. user_base_2 = ((Lisp_Object *)nil)[192];
  4103. user_base_3 = ((Lisp_Object *)nil)[193];
  4104. user_base_4 = ((Lisp_Object *)nil)[194];
  4105. user_base_5 = ((Lisp_Object *)nil)[195];
  4106. user_base_6 = ((Lisp_Object *)nil)[196];
  4107. user_base_7 = ((Lisp_Object *)nil)[197];
  4108. user_base_8 = ((Lisp_Object *)nil)[198];
  4109. user_base_9 = ((Lisp_Object *)nil)[199];
  4110. }
  4111. /* end of restart.c */