bytecomp.el 185 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657
  1. ;;; bytecomp.el --- compilation of Lisp code into byte code -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1985-1987, 1992, 1994, 1998, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Jamie Zawinski <jwz@lucid.com>
  5. ;; Hallvard Furuseth <hbf@ulrik.uio.no>
  6. ;; Maintainer: FSF
  7. ;; Keywords: lisp
  8. ;; Package: emacs
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
  22. ;; of p-code (`lapcode') which takes up less space and can be interpreted
  23. ;; faster. [`LAP' == `Lisp Assembly Program'.]
  24. ;; The user entry points are byte-compile-file and byte-recompile-directory.
  25. ;;; Code:
  26. ;; ========================================================================
  27. ;; Entry points:
  28. ;; byte-recompile-directory, byte-compile-file,
  29. ;; byte-recompile-file,
  30. ;; batch-byte-compile, batch-byte-recompile-directory,
  31. ;; byte-compile, compile-defun,
  32. ;; display-call-tree
  33. ;; (byte-compile-buffer and byte-compile-and-load-file were turned off
  34. ;; because they are not terribly useful and get in the way of completion.)
  35. ;; This version of the byte compiler has the following improvements:
  36. ;; + optimization of compiled code:
  37. ;; - removal of unreachable code;
  38. ;; - removal of calls to side-effectless functions whose return-value
  39. ;; is unused;
  40. ;; - compile-time evaluation of safe constant forms, such as (consp nil)
  41. ;; and (ash 1 6);
  42. ;; - open-coding of literal lambdas;
  43. ;; - peephole optimization of emitted code;
  44. ;; - trivial functions are left uncompiled for speed.
  45. ;; + support for inline functions;
  46. ;; + compile-time evaluation of arbitrary expressions;
  47. ;; + compile-time warning messages for:
  48. ;; - functions being redefined with incompatible arglists;
  49. ;; - functions being redefined as macros, or vice-versa;
  50. ;; - functions or macros defined multiple times in the same file;
  51. ;; - functions being called with the incorrect number of arguments;
  52. ;; - functions being called which are not defined globally, in the
  53. ;; file, or as autoloads;
  54. ;; - assignment and reference of undeclared free variables;
  55. ;; - various syntax errors;
  56. ;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
  57. ;; + correct compilation of top-level uses of macros;
  58. ;; + the ability to generate a histogram of functions called.
  59. ;; User customization variables: M-x customize-group bytecomp
  60. ;; New Features:
  61. ;;
  62. ;; o The form `defsubst' is just like `defun', except that the function
  63. ;; generated will be open-coded in compiled code which uses it. This
  64. ;; means that no function call will be generated, it will simply be
  65. ;; spliced in. Lisp functions calls are very slow, so this can be a
  66. ;; big win.
  67. ;;
  68. ;; You can generally accomplish the same thing with `defmacro', but in
  69. ;; that case, the defined procedure can't be used as an argument to
  70. ;; mapcar, etc.
  71. ;;
  72. ;; o You can also open-code one particular call to a function without
  73. ;; open-coding all calls. Use the 'inline' form to do this, like so:
  74. ;;
  75. ;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
  76. ;; or...
  77. ;; (inline ;; `foo' and `baz' will be
  78. ;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
  79. ;; (baz 0))
  80. ;;
  81. ;; o It is possible to open-code a function in the same file it is defined
  82. ;; in without having to load that file before compiling it. The
  83. ;; byte-compiler has been modified to remember function definitions in
  84. ;; the compilation environment in the same way that it remembers macro
  85. ;; definitions.
  86. ;;
  87. ;; o Forms like ((lambda ...) ...) are open-coded.
  88. ;;
  89. ;; o The form `eval-when-compile' is like progn, except that the body
  90. ;; is evaluated at compile-time. When it appears at top-level, this
  91. ;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
  92. ;; When it does not appear at top-level, it is similar to the
  93. ;; Common Lisp #. reader macro (but not in interpreted code).
  94. ;;
  95. ;; o The form `eval-and-compile' is similar to eval-when-compile, but
  96. ;; the whole form is evalled both at compile-time and at run-time.
  97. ;;
  98. ;; o The command compile-defun is analogous to eval-defun.
  99. ;;
  100. ;; o If you run byte-compile-file on a filename which is visited in a
  101. ;; buffer, and that buffer is modified, you are asked whether you want
  102. ;; to save the buffer before compiling.
  103. ;;
  104. ;; o byte-compiled files now start with the string `;ELC'.
  105. ;; Some versions of `file' can be customized to recognize that.
  106. (require 'backquote)
  107. (require 'macroexp)
  108. (require 'cconv)
  109. (eval-when-compile (require 'cl))
  110. (or (fboundp 'defsubst)
  111. ;; This really ought to be loaded already!
  112. (load "byte-run"))
  113. ;; The feature of compiling in a specific target Emacs version
  114. ;; has been turned off because compile time options are a bad idea.
  115. (defgroup bytecomp nil
  116. "Emacs Lisp byte-compiler."
  117. :group 'lisp)
  118. (defcustom emacs-lisp-file-regexp "\\.el\\'"
  119. "Regexp which matches Emacs Lisp source files.
  120. If you change this, you might want to set `byte-compile-dest-file-function'."
  121. :group 'bytecomp
  122. :type 'regexp)
  123. (defcustom byte-compile-dest-file-function nil
  124. "Function for the function `byte-compile-dest-file' to call.
  125. It should take one argument, the name of an Emacs Lisp source
  126. file name, and return the name of the compiled file."
  127. :group 'bytecomp
  128. :type '(choice (const nil) function)
  129. :version "23.2")
  130. ;; This enables file name handlers such as jka-compr
  131. ;; to remove parts of the file name that should not be copied
  132. ;; through to the output file name.
  133. (defun byte-compiler-base-file-name (filename)
  134. (let ((handler (find-file-name-handler filename
  135. 'byte-compiler-base-file-name)))
  136. (if handler
  137. (funcall handler 'byte-compiler-base-file-name filename)
  138. filename)))
  139. (or (fboundp 'byte-compile-dest-file)
  140. ;; The user may want to redefine this along with emacs-lisp-file-regexp,
  141. ;; so only define it if it is undefined.
  142. ;; Note - redefining this function is obsolete as of 23.2.
  143. ;; Customize byte-compile-dest-file-function instead.
  144. (defun byte-compile-dest-file (filename)
  145. "Convert an Emacs Lisp source file name to a compiled file name.
  146. If `byte-compile-dest-file-function' is non-nil, uses that
  147. function to do the work. Otherwise, if FILENAME matches
  148. `emacs-lisp-file-regexp' (by default, files with the extension `.el'),
  149. adds `c' to it; otherwise adds `.elc'."
  150. (if byte-compile-dest-file-function
  151. (funcall byte-compile-dest-file-function filename)
  152. (setq filename (file-name-sans-versions
  153. (byte-compiler-base-file-name filename)))
  154. (cond ((string-match emacs-lisp-file-regexp filename)
  155. (concat (substring filename 0 (match-beginning 0)) ".elc"))
  156. (t (concat filename ".elc"))))))
  157. ;; This can be the 'byte-compile property of any symbol.
  158. (autoload 'byte-compile-inline-expand "byte-opt")
  159. ;; This is the entry point to the lapcode optimizer pass1.
  160. (autoload 'byte-optimize-form "byte-opt")
  161. ;; This is the entry point to the lapcode optimizer pass2.
  162. (autoload 'byte-optimize-lapcode "byte-opt")
  163. (autoload 'byte-compile-unfold-lambda "byte-opt")
  164. ;; This is the entry point to the decompiler, which is used by the
  165. ;; disassembler. The disassembler just requires 'byte-compile, but
  166. ;; that doesn't define this function, so this seems to be a reasonable
  167. ;; thing to do.
  168. (autoload 'byte-decompile-bytecode "byte-opt")
  169. (defcustom byte-compile-verbose
  170. (and (not noninteractive) (> baud-rate search-slow-speed))
  171. "Non-nil means print messages describing progress of byte-compiler."
  172. :group 'bytecomp
  173. :type 'boolean)
  174. (defcustom byte-optimize t
  175. "Enable optimization in the byte compiler.
  176. Possible values are:
  177. nil - no optimization
  178. t - all optimizations
  179. `source' - source-level optimizations only
  180. `byte' - code-level optimizations only"
  181. :group 'bytecomp
  182. :type '(choice (const :tag "none" nil)
  183. (const :tag "all" t)
  184. (const :tag "source-level" source)
  185. (const :tag "byte-level" byte)))
  186. (defcustom byte-compile-delete-errors nil
  187. "If non-nil, the optimizer may delete forms that may signal an error.
  188. This includes variable references and calls to functions such as `car'."
  189. :group 'bytecomp
  190. :type 'boolean)
  191. (defvar byte-compile-dynamic nil
  192. "If non-nil, compile function bodies so they load lazily.
  193. They are hidden in comments in the compiled file,
  194. and each one is brought into core when the
  195. function is called.
  196. To enable this option, make it a file-local variable
  197. in the source file you want it to apply to.
  198. For example, add -*-byte-compile-dynamic: t;-*- on the first line.
  199. When this option is true, if you load the compiled file and then move it,
  200. the functions you loaded will not be able to run.")
  201. ;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
  202. (defvar byte-compile-disable-print-circle nil
  203. "If non-nil, disable `print-circle' on printing a byte-compiled code.")
  204. (make-obsolete-variable 'byte-compile-disable-print-circle nil "24.1")
  205. ;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
  206. (defcustom byte-compile-dynamic-docstrings t
  207. "If non-nil, compile doc strings for lazy access.
  208. We bury the doc strings of functions and variables inside comments in
  209. the file, and bring them into core only when they are actually needed.
  210. When this option is true, if you load the compiled file and then move it,
  211. you won't be able to find the documentation of anything in that file.
  212. To disable this option for a certain file, make it a file-local variable
  213. in the source file. For example, add this to the first line:
  214. -*-byte-compile-dynamic-docstrings:nil;-*-
  215. You can also set the variable globally.
  216. This option is enabled by default because it reduces Emacs memory usage."
  217. :group 'bytecomp
  218. :type 'boolean)
  219. ;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
  220. (defconst byte-compile-log-buffer "*Compile-Log*"
  221. "Name of the byte-compiler's log buffer.")
  222. (defcustom byte-optimize-log nil
  223. "If non-nil, the byte-compiler will log its optimizations.
  224. If this is 'source, then only source-level optimizations will be logged.
  225. If it is 'byte, then only byte-level optimizations will be logged.
  226. The information is logged to `byte-compile-log-buffer'."
  227. :group 'bytecomp
  228. :type '(choice (const :tag "none" nil)
  229. (const :tag "all" t)
  230. (const :tag "source-level" source)
  231. (const :tag "byte-level" byte)))
  232. (defcustom byte-compile-error-on-warn nil
  233. "If true, the byte-compiler reports warnings with `error'."
  234. :group 'bytecomp
  235. :type 'boolean)
  236. (defconst byte-compile-warning-types
  237. '(redefine callargs free-vars unresolved
  238. obsolete noruntime cl-functions interactive-only
  239. make-local mapcar constants suspicious lexical)
  240. "The list of warning types used when `byte-compile-warnings' is t.")
  241. (defcustom byte-compile-warnings t
  242. "List of warnings that the byte-compiler should issue (t for all).
  243. Elements of the list may be:
  244. free-vars references to variables not in the current lexical scope.
  245. unresolved calls to unknown functions.
  246. callargs function calls with args that don't match the definition.
  247. redefine function name redefined from a macro to ordinary function or vice
  248. versa, or redefined to take a different number of arguments.
  249. obsolete obsolete variables and functions.
  250. noruntime functions that may not be defined at runtime (typically
  251. defined only under `eval-when-compile').
  252. cl-functions calls to runtime functions from the CL package (as
  253. distinguished from macros and aliases).
  254. interactive-only
  255. commands that normally shouldn't be called from Lisp code.
  256. make-local calls to make-variable-buffer-local that may be incorrect.
  257. mapcar mapcar called for effect.
  258. constants let-binding of, or assignment to, constants/nonvariables.
  259. suspicious constructs that usually don't do what the coder wanted.
  260. If the list begins with `not', then the remaining elements specify warnings to
  261. suppress. For example, (not mapcar) will suppress warnings about mapcar."
  262. :group 'bytecomp
  263. :type `(choice (const :tag "All" t)
  264. (set :menu-tag "Some"
  265. ,@(mapcar (lambda (x) `(const ,x))
  266. byte-compile-warning-types))))
  267. ;;;###autoload
  268. (put 'byte-compile-warnings 'safe-local-variable
  269. (lambda (v)
  270. (or (symbolp v)
  271. (null (delq nil (mapcar (lambda (x) (not (symbolp x))) v))))))
  272. (defun byte-compile-warning-enabled-p (warning)
  273. "Return non-nil if WARNING is enabled, according to `byte-compile-warnings'."
  274. (or (eq byte-compile-warnings t)
  275. (if (eq (car byte-compile-warnings) 'not)
  276. (not (memq warning byte-compile-warnings))
  277. (memq warning byte-compile-warnings))))
  278. ;;;###autoload
  279. (defun byte-compile-disable-warning (warning)
  280. "Change `byte-compile-warnings' to disable WARNING.
  281. If `byte-compile-warnings' is t, set it to `(not WARNING)'.
  282. Otherwise, if the first element is `not', add WARNING, else remove it.
  283. Normally you should let-bind `byte-compile-warnings' before calling this,
  284. else the global value will be modified."
  285. (setq byte-compile-warnings
  286. (cond ((eq byte-compile-warnings t)
  287. (list 'not warning))
  288. ((eq (car byte-compile-warnings) 'not)
  289. (if (memq warning byte-compile-warnings)
  290. byte-compile-warnings
  291. (append byte-compile-warnings (list warning))))
  292. (t
  293. (delq warning byte-compile-warnings)))))
  294. ;;;###autoload
  295. (defun byte-compile-enable-warning (warning)
  296. "Change `byte-compile-warnings' to enable WARNING.
  297. If `byte-compile-warnings' is `t', do nothing. Otherwise, if the
  298. first element is `not', remove WARNING, else add it.
  299. Normally you should let-bind `byte-compile-warnings' before calling this,
  300. else the global value will be modified."
  301. (or (eq byte-compile-warnings t)
  302. (setq byte-compile-warnings
  303. (cond ((eq (car byte-compile-warnings) 'not)
  304. (delq warning byte-compile-warnings))
  305. ((memq warning byte-compile-warnings)
  306. byte-compile-warnings)
  307. (t
  308. (append byte-compile-warnings (list warning)))))))
  309. (defvar byte-compile-interactive-only-functions
  310. '(beginning-of-buffer end-of-buffer replace-string replace-regexp
  311. insert-file insert-buffer insert-file-literally previous-line next-line
  312. goto-line comint-run delete-backward-char toggle-read-only)
  313. "List of commands that are not meant to be called from Lisp.")
  314. (defvar byte-compile-not-obsolete-vars nil
  315. "List of variables that shouldn't be reported as obsolete.")
  316. (defvar byte-compile-global-not-obsolete-vars nil
  317. "Global list of variables that shouldn't be reported as obsolete.")
  318. (defvar byte-compile-not-obsolete-funcs nil
  319. "List of functions that shouldn't be reported as obsolete.")
  320. (defcustom byte-compile-generate-call-tree nil
  321. "Non-nil means collect call-graph information when compiling.
  322. This records which functions were called and from where.
  323. If the value is t, compilation displays the call graph when it finishes.
  324. If the value is neither t nor nil, compilation asks you whether to display
  325. the graph.
  326. The call tree only lists functions called, not macros used. Those functions
  327. which the byte-code interpreter knows about directly (eq, cons, etc.) are
  328. not reported.
  329. The call tree also lists those functions which are not known to be called
  330. \(that is, to which no calls have been compiled). Functions which can be
  331. invoked interactively are excluded from this list."
  332. :group 'bytecomp
  333. :type '(choice (const :tag "Yes" t) (const :tag "No" nil)
  334. (other :tag "Ask" lambda)))
  335. (defvar byte-compile-call-tree nil
  336. "Alist of functions and their call tree.
  337. Each element looks like
  338. \(FUNCTION CALLERS CALLS\)
  339. where CALLERS is a list of functions that call FUNCTION, and CALLS
  340. is a list of functions for which calls were generated while compiling
  341. FUNCTION.")
  342. (defcustom byte-compile-call-tree-sort 'name
  343. "If non-nil, sort the call tree.
  344. The values `name', `callers', `calls', `calls+callers'
  345. specify different fields to sort on."
  346. :group 'bytecomp
  347. :type '(choice (const name) (const callers) (const calls)
  348. (const calls+callers) (const nil)))
  349. (defvar byte-compile-debug nil)
  350. (defvar byte-compile-constants nil
  351. "List of all constants encountered during compilation of this form.")
  352. (defvar byte-compile-variables nil
  353. "List of all variables encountered during compilation of this form.")
  354. (defvar byte-compile-bound-variables nil
  355. "List of dynamic variables bound in the context of the current form.
  356. This list lives partly on the stack.")
  357. (defvar byte-compile-const-variables nil
  358. "List of variables declared as constants during compilation of this file.")
  359. (defvar byte-compile-free-references)
  360. (defvar byte-compile-free-assignments)
  361. (defvar byte-compiler-error-flag)
  362. (defconst byte-compile-initial-macro-environment
  363. '(
  364. ;; (byte-compiler-options . (lambda (&rest forms)
  365. ;; (apply 'byte-compiler-options-handler forms)))
  366. (declare-function . byte-compile-macroexpand-declare-function)
  367. (eval-when-compile . (lambda (&rest body)
  368. (list
  369. 'quote
  370. (byte-compile-eval
  371. (byte-compile-top-level
  372. (byte-compile-preprocess (cons 'progn body)))))))
  373. (eval-and-compile . (lambda (&rest body)
  374. (byte-compile-eval-before-compile (cons 'progn body))
  375. (cons 'progn body))))
  376. "The default macro-environment passed to macroexpand by the compiler.
  377. Placing a macro here will cause a macro to have different semantics when
  378. expanded by the compiler as when expanded by the interpreter.")
  379. (defvar byte-compile-macro-environment byte-compile-initial-macro-environment
  380. "Alist of macros defined in the file being compiled.
  381. Each element looks like (MACRONAME . DEFINITION). It is
  382. \(MACRONAME . nil) when a macro is redefined as a function.")
  383. (defvar byte-compile-function-environment nil
  384. "Alist of functions defined in the file being compiled.
  385. This is so we can inline them when necessary.
  386. Each element looks like (FUNCTIONNAME . DEFINITION). It is
  387. \(FUNCTIONNAME . nil) when a function is redefined as a macro.
  388. It is \(FUNCTIONNAME . t) when all we know is that it was defined,
  389. and we don't know the definition. For an autoloaded function, DEFINITION
  390. has the form (autoload . FILENAME).")
  391. (defvar byte-compile-unresolved-functions nil
  392. "Alist of undefined functions to which calls have been compiled.
  393. This variable is only significant whilst compiling an entire buffer.
  394. Used for warnings when a function is not known to be defined or is later
  395. defined with incorrect args.")
  396. (defvar byte-compile-noruntime-functions nil
  397. "Alist of functions called that may not be defined when the compiled code is run.
  398. Used for warnings about calling a function that is defined during compilation
  399. but won't necessarily be defined when the compiled file is loaded.")
  400. ;; Variables for lexical binding
  401. (defvar byte-compile--lexical-environment nil
  402. "The current lexical environment.")
  403. (defvar byte-compile-tag-number 0)
  404. (defvar byte-compile-output nil
  405. "Alist describing contents to put in byte code string.
  406. Each element is (INDEX . VALUE)")
  407. (defvar byte-compile-depth 0 "Current depth of execution stack.")
  408. (defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
  409. ;;; The byte codes; this information is duplicated in bytecomp.c
  410. (defvar byte-code-vector nil
  411. "An array containing byte-code names indexed by byte-code values.")
  412. (defvar byte-stack+-info nil
  413. "An array with the stack adjustment for each byte-code.")
  414. (defmacro byte-defop (opcode stack-adjust opname &optional docstring)
  415. ;; This is a speed-hack for building the byte-code-vector at compile-time.
  416. ;; We fill in the vector at macroexpand-time, and then after the last call
  417. ;; to byte-defop, we write the vector out as a constant instead of writing
  418. ;; out a bunch of calls to aset.
  419. ;; Actually, we don't fill in the vector itself, because that could make
  420. ;; it problematic to compile big changes to this compiler; we store the
  421. ;; values on its plist, and remove them later in -extrude.
  422. (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
  423. (put 'byte-code-vector 'tmp-compile-time-value
  424. (make-vector 256 nil))))
  425. (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
  426. (put 'byte-stack+-info 'tmp-compile-time-value
  427. (make-vector 256 nil)))))
  428. (aset v1 opcode opname)
  429. (aset v2 opcode stack-adjust))
  430. (if docstring
  431. (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
  432. (list 'defconst opname opcode)))
  433. (defmacro byte-extrude-byte-code-vectors ()
  434. (prog1 (list 'setq 'byte-code-vector
  435. (get 'byte-code-vector 'tmp-compile-time-value)
  436. 'byte-stack+-info
  437. (get 'byte-stack+-info 'tmp-compile-time-value))
  438. (put 'byte-code-vector 'tmp-compile-time-value nil)
  439. (put 'byte-stack+-info 'tmp-compile-time-value nil)))
  440. ;; These opcodes are special in that they pack their argument into the
  441. ;; opcode word.
  442. ;;
  443. (byte-defop 0 1 byte-stack-ref "for stack reference")
  444. (byte-defop 8 1 byte-varref "for variable reference")
  445. (byte-defop 16 -1 byte-varset "for setting a variable")
  446. (byte-defop 24 -1 byte-varbind "for binding a variable")
  447. (byte-defop 32 0 byte-call "for calling a function")
  448. (byte-defop 40 0 byte-unbind "for unbinding special bindings")
  449. ;; codes 8-47 are consumed by the preceding opcodes
  450. ;; unused: 48-55
  451. (byte-defop 56 -1 byte-nth)
  452. (byte-defop 57 0 byte-symbolp)
  453. (byte-defop 58 0 byte-consp)
  454. (byte-defop 59 0 byte-stringp)
  455. (byte-defop 60 0 byte-listp)
  456. (byte-defop 61 -1 byte-eq)
  457. (byte-defop 62 -1 byte-memq)
  458. (byte-defop 63 0 byte-not)
  459. (byte-defop 64 0 byte-car)
  460. (byte-defop 65 0 byte-cdr)
  461. (byte-defop 66 -1 byte-cons)
  462. (byte-defop 67 0 byte-list1)
  463. (byte-defop 68 -1 byte-list2)
  464. (byte-defop 69 -2 byte-list3)
  465. (byte-defop 70 -3 byte-list4)
  466. (byte-defop 71 0 byte-length)
  467. (byte-defop 72 -1 byte-aref)
  468. (byte-defop 73 -2 byte-aset)
  469. (byte-defop 74 0 byte-symbol-value)
  470. (byte-defop 75 0 byte-symbol-function) ; this was commented out
  471. (byte-defop 76 -1 byte-set)
  472. (byte-defop 77 -1 byte-fset) ; this was commented out
  473. (byte-defop 78 -1 byte-get)
  474. (byte-defop 79 -2 byte-substring)
  475. (byte-defop 80 -1 byte-concat2)
  476. (byte-defop 81 -2 byte-concat3)
  477. (byte-defop 82 -3 byte-concat4)
  478. (byte-defop 83 0 byte-sub1)
  479. (byte-defop 84 0 byte-add1)
  480. (byte-defop 85 -1 byte-eqlsign)
  481. (byte-defop 86 -1 byte-gtr)
  482. (byte-defop 87 -1 byte-lss)
  483. (byte-defop 88 -1 byte-leq)
  484. (byte-defop 89 -1 byte-geq)
  485. (byte-defop 90 -1 byte-diff)
  486. (byte-defop 91 0 byte-negate)
  487. (byte-defop 92 -1 byte-plus)
  488. (byte-defop 93 -1 byte-max)
  489. (byte-defop 94 -1 byte-min)
  490. (byte-defop 95 -1 byte-mult) ; v19 only
  491. (byte-defop 96 1 byte-point)
  492. (byte-defop 98 0 byte-goto-char)
  493. (byte-defop 99 0 byte-insert)
  494. (byte-defop 100 1 byte-point-max)
  495. (byte-defop 101 1 byte-point-min)
  496. (byte-defop 102 0 byte-char-after)
  497. (byte-defop 103 1 byte-following-char)
  498. (byte-defop 104 1 byte-preceding-char)
  499. (byte-defop 105 1 byte-current-column)
  500. (byte-defop 106 0 byte-indent-to)
  501. (byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
  502. (byte-defop 108 1 byte-eolp)
  503. (byte-defop 109 1 byte-eobp)
  504. (byte-defop 110 1 byte-bolp)
  505. (byte-defop 111 1 byte-bobp)
  506. (byte-defop 112 1 byte-current-buffer)
  507. (byte-defop 113 0 byte-set-buffer)
  508. (byte-defop 114 0 byte-save-current-buffer
  509. "To make a binding to record the current buffer")
  510. (byte-defop 115 0 byte-set-mark-OBSOLETE)
  511. (byte-defop 116 1 byte-interactive-p-OBSOLETE)
  512. ;; These ops are new to v19
  513. (byte-defop 117 0 byte-forward-char)
  514. (byte-defop 118 0 byte-forward-word)
  515. (byte-defop 119 -1 byte-skip-chars-forward)
  516. (byte-defop 120 -1 byte-skip-chars-backward)
  517. (byte-defop 121 0 byte-forward-line)
  518. (byte-defop 122 0 byte-char-syntax)
  519. (byte-defop 123 -1 byte-buffer-substring)
  520. (byte-defop 124 -1 byte-delete-region)
  521. (byte-defop 125 -1 byte-narrow-to-region)
  522. (byte-defop 126 1 byte-widen)
  523. (byte-defop 127 0 byte-end-of-line)
  524. ;; unused: 128
  525. ;; These store their argument in the next two bytes
  526. (byte-defop 129 1 byte-constant2
  527. "for reference to a constant with vector index >= byte-constant-limit")
  528. (byte-defop 130 0 byte-goto "for unconditional jump")
  529. (byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
  530. (byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
  531. (byte-defop 133 -1 byte-goto-if-nil-else-pop
  532. "to examine top-of-stack, jump and don't pop it if it's nil,
  533. otherwise pop it")
  534. (byte-defop 134 -1 byte-goto-if-not-nil-else-pop
  535. "to examine top-of-stack, jump and don't pop it if it's non nil,
  536. otherwise pop it")
  537. (byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
  538. (byte-defop 136 -1 byte-discard "to discard one value from stack")
  539. (byte-defop 137 1 byte-dup "to duplicate the top of the stack")
  540. (byte-defop 138 0 byte-save-excursion
  541. "to make a binding to record the buffer, point and mark")
  542. (byte-defop 139 0 byte-save-window-excursion-OBSOLETE
  543. "to make a binding to record entire window configuration")
  544. (byte-defop 140 0 byte-save-restriction
  545. "to make a binding to record the current buffer clipping restrictions")
  546. (byte-defop 141 -1 byte-catch
  547. "for catch. Takes, on stack, the tag and an expression for the body")
  548. (byte-defop 142 -1 byte-unwind-protect
  549. "for unwind-protect. Takes, on stack, an expression for the unwind-action")
  550. ;; For condition-case. Takes, on stack, the variable to bind,
  551. ;; an expression for the body, and a list of clauses.
  552. (byte-defop 143 -2 byte-condition-case)
  553. (byte-defop 144 0 byte-temp-output-buffer-setup-OBSOLETE)
  554. (byte-defop 145 -1 byte-temp-output-buffer-show-OBSOLETE)
  555. ;; these ops are new to v19
  556. ;; To unbind back to the beginning of this frame.
  557. ;; Not used yet, but will be needed for tail-recursion elimination.
  558. (byte-defop 146 0 byte-unbind-all)
  559. ;; these ops are new to v19
  560. (byte-defop 147 -2 byte-set-marker)
  561. (byte-defop 148 0 byte-match-beginning)
  562. (byte-defop 149 0 byte-match-end)
  563. (byte-defop 150 0 byte-upcase)
  564. (byte-defop 151 0 byte-downcase)
  565. (byte-defop 152 -1 byte-string=)
  566. (byte-defop 153 -1 byte-string<)
  567. (byte-defop 154 -1 byte-equal)
  568. (byte-defop 155 -1 byte-nthcdr)
  569. (byte-defop 156 -1 byte-elt)
  570. (byte-defop 157 -1 byte-member)
  571. (byte-defop 158 -1 byte-assq)
  572. (byte-defop 159 0 byte-nreverse)
  573. (byte-defop 160 -1 byte-setcar)
  574. (byte-defop 161 -1 byte-setcdr)
  575. (byte-defop 162 0 byte-car-safe)
  576. (byte-defop 163 0 byte-cdr-safe)
  577. (byte-defop 164 -1 byte-nconc)
  578. (byte-defop 165 -1 byte-quo)
  579. (byte-defop 166 -1 byte-rem)
  580. (byte-defop 167 0 byte-numberp)
  581. (byte-defop 168 0 byte-integerp)
  582. ;; unused: 169-174
  583. (byte-defop 175 nil byte-listN)
  584. (byte-defop 176 nil byte-concatN)
  585. (byte-defop 177 nil byte-insertN)
  586. (byte-defop 178 -1 byte-stack-set) ; Stack offset in following one byte.
  587. (byte-defop 179 -1 byte-stack-set2) ; Stack offset in following two bytes.
  588. ;; If (following one byte & 0x80) == 0
  589. ;; discard (following one byte & 0x7F) stack entries
  590. ;; else
  591. ;; discard (following one byte & 0x7F) stack entries _underneath_ TOS
  592. ;; (that is, if the operand = 0x83, ... X Y Z T => ... T)
  593. (byte-defop 182 nil byte-discardN)
  594. ;; `byte-discardN-preserve-tos' is a pseudo-op that gets turned into
  595. ;; `byte-discardN' with the high bit in the operand set (by
  596. ;; `byte-compile-lapcode').
  597. (defconst byte-discardN-preserve-tos byte-discardN)
  598. ;; unused: 182-191
  599. (byte-defop 192 1 byte-constant "for reference to a constant")
  600. ;; codes 193-255 are consumed by byte-constant.
  601. (defconst byte-constant-limit 64
  602. "Exclusive maximum index usable in the `byte-constant' opcode.")
  603. (defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
  604. byte-goto-if-nil-else-pop
  605. byte-goto-if-not-nil-else-pop)
  606. "List of byte-codes whose offset is a pc.")
  607. (defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
  608. (byte-extrude-byte-code-vectors)
  609. ;;; lapcode generator
  610. ;;
  611. ;; the byte-compiler now does source -> lapcode -> bytecode instead of
  612. ;; source -> bytecode, because it's a lot easier to make optimizations
  613. ;; on lapcode than on bytecode.
  614. ;;
  615. ;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
  616. ;; where instruction is a symbol naming a byte-code instruction,
  617. ;; and parameter is an argument to that instruction, if any.
  618. ;;
  619. ;; The instruction can be the pseudo-op TAG, which means that this position
  620. ;; in the instruction stream is a target of a goto. (car PARAMETER) will be
  621. ;; the PC for this location, and the whole instruction "(TAG pc)" will be the
  622. ;; parameter for some goto op.
  623. ;;
  624. ;; If the operation is varbind, varref, varset or push-constant, then the
  625. ;; parameter is (variable/constant . index_in_constant_vector).
  626. ;;
  627. ;; First, the source code is macroexpanded and optimized in various ways.
  628. ;; Then the resultant code is compiled into lapcode. Another set of
  629. ;; optimizations are then run over the lapcode. Then the variables and
  630. ;; constants referenced by the lapcode are collected and placed in the
  631. ;; constants-vector. (This happens now so that variables referenced by dead
  632. ;; code don't consume space.) And finally, the lapcode is transformed into
  633. ;; compacted byte-code.
  634. ;;
  635. ;; A distinction is made between variables and constants because the variable-
  636. ;; referencing instructions are more sensitive to the variables being near the
  637. ;; front of the constants-vector than the constant-referencing instructions.
  638. ;; Also, this lets us notice references to free variables.
  639. (defmacro byte-compile-push-bytecodes (&rest args)
  640. "Push BYTE... onto BYTES, and increment PC by the number of bytes pushed.
  641. ARGS is of the form (BYTE... BYTES PC), where BYTES and PC are variable names.
  642. BYTES and PC are updated after evaluating all the arguments."
  643. (let ((byte-exprs (butlast args 2))
  644. (bytes-var (car (last args 2)))
  645. (pc-var (car (last args))))
  646. `(setq ,bytes-var ,(if (null (cdr byte-exprs))
  647. `(progn (assert (<= 0 ,(car byte-exprs)))
  648. (cons ,@byte-exprs ,bytes-var))
  649. `(nconc (list ,@(reverse byte-exprs)) ,bytes-var))
  650. ,pc-var (+ ,(length byte-exprs) ,pc-var))))
  651. (defmacro byte-compile-push-bytecode-const2 (opcode const2 bytes pc)
  652. "Push OPCODE and the two-byte constant CONST2 onto BYTES, and add 3 to PC.
  653. CONST2 may be evaluated multiple times."
  654. `(byte-compile-push-bytecodes ,opcode (logand ,const2 255) (lsh ,const2 -8)
  655. ,bytes ,pc))
  656. (defun byte-compile-lapcode (lap)
  657. "Turns lapcode into bytecode. The lapcode is destroyed."
  658. ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
  659. (let ((pc 0) ; Program counter
  660. op off ; Operation & offset
  661. opcode ; numeric value of OP
  662. (bytes '()) ; Put the output bytes here
  663. (patchlist nil)) ; List of gotos to patch
  664. (dolist (lap-entry lap)
  665. (setq op (car lap-entry)
  666. off (cdr lap-entry))
  667. (cond
  668. ((not (symbolp op))
  669. (error "Non-symbolic opcode `%s'" op))
  670. ((eq op 'TAG)
  671. (setcar off pc))
  672. (t
  673. (setq opcode
  674. (if (eq op 'byte-discardN-preserve-tos)
  675. ;; byte-discardN-preserve-tos is a pseudo op, which
  676. ;; is actually the same as byte-discardN
  677. ;; with a modified argument.
  678. byte-discardN
  679. (symbol-value op)))
  680. (cond ((memq op byte-goto-ops)
  681. ;; goto
  682. (byte-compile-push-bytecodes opcode nil (cdr off) bytes pc)
  683. (push bytes patchlist))
  684. ((or (and (consp off)
  685. ;; Variable or constant reference
  686. (progn
  687. (setq off (cdr off))
  688. (eq op 'byte-constant)))
  689. (and (eq op 'byte-constant)
  690. (integerp off)))
  691. ;; constant ref
  692. (if (< off byte-constant-limit)
  693. (byte-compile-push-bytecodes (+ byte-constant off)
  694. bytes pc)
  695. (byte-compile-push-bytecode-const2 byte-constant2 off
  696. bytes pc)))
  697. ((and (= opcode byte-stack-set)
  698. (> off 255))
  699. ;; Use the two-byte version of byte-stack-set if the
  700. ;; offset is too large for the normal version.
  701. (byte-compile-push-bytecode-const2 byte-stack-set2 off
  702. bytes pc))
  703. ((and (>= opcode byte-listN)
  704. (< opcode byte-discardN))
  705. ;; These insns all put their operand into one extra byte.
  706. (byte-compile-push-bytecodes opcode off bytes pc))
  707. ((= opcode byte-discardN)
  708. ;; byte-discardN is weird in that it encodes a flag in the
  709. ;; top bit of its one-byte argument. If the argument is
  710. ;; too large to fit in 7 bits, the opcode can be repeated.
  711. (let ((flag (if (eq op 'byte-discardN-preserve-tos) #x80 0)))
  712. (while (> off #x7f)
  713. (byte-compile-push-bytecodes opcode (logior #x7f flag)
  714. bytes pc)
  715. (setq off (- off #x7f)))
  716. (byte-compile-push-bytecodes opcode (logior off flag)
  717. bytes pc)))
  718. ((null off)
  719. ;; opcode that doesn't use OFF
  720. (byte-compile-push-bytecodes opcode bytes pc))
  721. ((and (eq opcode byte-stack-ref) (eq off 0))
  722. ;; (stack-ref 0) is really just another name for `dup'.
  723. (debug) ;FIXME: When would this happen?
  724. (byte-compile-push-bytecodes byte-dup bytes pc))
  725. ;; The following three cases are for the special
  726. ;; insns that encode their operand into 0, 1, or 2
  727. ;; extra bytes depending on its magnitude.
  728. ((< off 6)
  729. (byte-compile-push-bytecodes (+ opcode off) bytes pc))
  730. ((< off 256)
  731. (byte-compile-push-bytecodes (+ opcode 6) off bytes pc))
  732. (t
  733. (byte-compile-push-bytecode-const2 (+ opcode 7) off
  734. bytes pc))))))
  735. ;;(if (not (= pc (length bytes)))
  736. ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
  737. ;; Patch tag PCs into absolute jumps.
  738. (dolist (bytes-tail patchlist)
  739. (setq pc (caar bytes-tail)) ; Pick PC from goto's tag.
  740. (setcar (cdr bytes-tail) (logand pc 255))
  741. (setcar bytes-tail (lsh pc -8))
  742. ;; FIXME: Replace this by some workaround.
  743. (if (> (car bytes-tail) 255) (error "Bytecode overflow")))
  744. (apply 'unibyte-string (nreverse bytes))))
  745. ;;; compile-time evaluation
  746. (defun byte-compile-cl-file-p (file)
  747. "Return non-nil if FILE is one of the CL files."
  748. (and (stringp file)
  749. (string-match "^cl\\>" (file-name-nondirectory file))))
  750. (defun byte-compile-eval (form)
  751. "Eval FORM and mark the functions defined therein.
  752. Each function's symbol gets added to `byte-compile-noruntime-functions'."
  753. (let ((hist-orig load-history)
  754. (hist-nil-orig current-load-list))
  755. (prog1 (eval form lexical-binding)
  756. (when (byte-compile-warning-enabled-p 'noruntime)
  757. (let ((hist-new load-history)
  758. (hist-nil-new current-load-list))
  759. ;; Go through load-history, look for newly loaded files
  760. ;; and mark all the functions defined therein.
  761. (while (and hist-new (not (eq hist-new hist-orig)))
  762. (let ((xs (pop hist-new))
  763. old-autoloads)
  764. ;; Make sure the file was not already loaded before.
  765. (unless (or (assoc (car xs) hist-orig)
  766. ;; Don't give both the "noruntime" and
  767. ;; "cl-functions" warning for the same function.
  768. ;; FIXME This seems incorrect - these are two
  769. ;; independent warnings. For example, you may be
  770. ;; choosing to see the cl warnings but ignore them.
  771. ;; You probably don't want to ignore noruntime in the
  772. ;; same way.
  773. (and (byte-compile-warning-enabled-p 'cl-functions)
  774. (byte-compile-cl-file-p (car xs))))
  775. (dolist (s xs)
  776. (cond
  777. ((symbolp s)
  778. (unless (memq s old-autoloads)
  779. (push s byte-compile-noruntime-functions)))
  780. ((and (consp s) (eq t (car s)))
  781. (push (cdr s) old-autoloads))
  782. ((and (consp s) (eq 'autoload (car s)))
  783. (push (cdr s) byte-compile-noruntime-functions)))))))
  784. ;; Go through current-load-list for the locally defined funs.
  785. (let (old-autoloads)
  786. (while (and hist-nil-new (not (eq hist-nil-new hist-nil-orig)))
  787. (let ((s (pop hist-nil-new)))
  788. (when (and (symbolp s) (not (memq s old-autoloads)))
  789. (push s byte-compile-noruntime-functions))
  790. (when (and (consp s) (eq t (car s)))
  791. (push (cdr s) old-autoloads)))))))
  792. (when (byte-compile-warning-enabled-p 'cl-functions)
  793. (let ((hist-new load-history))
  794. ;; Go through load-history, looking for the cl files.
  795. ;; Since new files are added at the start of load-history,
  796. ;; we scan the new history until the tail matches the old.
  797. (while (and (not byte-compile-cl-functions)
  798. hist-new (not (eq hist-new hist-orig)))
  799. ;; We used to check if the file had already been loaded,
  800. ;; but it is better to check non-nil byte-compile-cl-functions.
  801. (and (byte-compile-cl-file-p (car (pop hist-new)))
  802. (byte-compile-find-cl-functions))))))))
  803. (defun byte-compile-eval-before-compile (form)
  804. "Evaluate FORM for `eval-and-compile'."
  805. (let ((hist-nil-orig current-load-list))
  806. (prog1 (eval form lexical-binding)
  807. ;; (eval-and-compile (require 'cl) turns off warnings for cl functions.
  808. ;; FIXME Why does it do that - just as a hack?
  809. ;; There are other ways to do this nowadays.
  810. (let ((tem current-load-list))
  811. (while (not (eq tem hist-nil-orig))
  812. (when (equal (car tem) '(require . cl))
  813. (byte-compile-disable-warning 'cl-functions))
  814. (setq tem (cdr tem)))))))
  815. ;;; byte compiler messages
  816. (defvar byte-compile-current-form nil)
  817. (defvar byte-compile-dest-file nil)
  818. (defvar byte-compile-current-file nil)
  819. (defvar byte-compile-current-group nil)
  820. (defvar byte-compile-current-buffer nil)
  821. ;; Log something that isn't a warning.
  822. (defmacro byte-compile-log (format-string &rest args)
  823. `(and
  824. byte-optimize
  825. (memq byte-optimize-log '(t source))
  826. (let ((print-escape-newlines t)
  827. (print-level 4)
  828. (print-length 4))
  829. (byte-compile-log-1
  830. (format
  831. ,format-string
  832. ,@(mapcar
  833. (lambda (x) (if (symbolp x) (list 'prin1-to-string x) x))
  834. args))))))
  835. ;; Log something that isn't a warning.
  836. (defun byte-compile-log-1 (string)
  837. (with-current-buffer byte-compile-log-buffer
  838. (let ((inhibit-read-only t))
  839. (goto-char (point-max))
  840. (byte-compile-warning-prefix nil nil)
  841. (cond (noninteractive
  842. (message " %s" string))
  843. (t
  844. (insert (format "%s\n" string)))))))
  845. (defvar byte-compile-read-position nil
  846. "Character position we began the last `read' from.")
  847. (defvar byte-compile-last-position nil
  848. "Last known character position in the input.")
  849. ;; copied from gnus-util.el
  850. (defsubst byte-compile-delete-first (elt list)
  851. (if (eq (car list) elt)
  852. (cdr list)
  853. (let ((total list))
  854. (while (and (cdr list)
  855. (not (eq (cadr list) elt)))
  856. (setq list (cdr list)))
  857. (when (cdr list)
  858. (setcdr list (cddr list)))
  859. total)))
  860. ;; The purpose of this function is to iterate through the
  861. ;; `read-symbol-positions-list'. Each time we process, say, a
  862. ;; function definition (`defun') we remove `defun' from
  863. ;; `read-symbol-positions-list', and set `byte-compile-last-position'
  864. ;; to that symbol's character position. Similarly, if we encounter a
  865. ;; variable reference, like in (1+ foo), we remove `foo' from the
  866. ;; list. If our current position is after the symbol's position, we
  867. ;; assume we've already passed that point, and look for the next
  868. ;; occurrence of the symbol.
  869. ;;
  870. ;; This function should not be called twice for the same occurrence of
  871. ;; a symbol, and it should not be called for symbols generated by the
  872. ;; byte compiler itself; because rather than just fail looking up the
  873. ;; symbol, we may find an occurrence of the symbol further ahead, and
  874. ;; then `byte-compile-last-position' as advanced too far.
  875. ;;
  876. ;; So your're probably asking yourself: Isn't this function a
  877. ;; gross hack? And the answer, of course, would be yes.
  878. (defun byte-compile-set-symbol-position (sym &optional allow-previous)
  879. (when byte-compile-read-position
  880. (let (last entry)
  881. (while (progn
  882. (setq last byte-compile-last-position
  883. entry (assq sym read-symbol-positions-list))
  884. (when entry
  885. (setq byte-compile-last-position
  886. (+ byte-compile-read-position (cdr entry))
  887. read-symbol-positions-list
  888. (byte-compile-delete-first
  889. entry read-symbol-positions-list)))
  890. (or (and allow-previous
  891. (not (= last byte-compile-last-position)))
  892. (> last byte-compile-last-position)))))))
  893. (defvar byte-compile-last-warned-form nil)
  894. (defvar byte-compile-last-logged-file nil)
  895. ;; This is used as warning-prefix for the compiler.
  896. ;; It is always called with the warnings buffer current.
  897. (defun byte-compile-warning-prefix (level entry)
  898. (let* ((inhibit-read-only t)
  899. (dir default-directory)
  900. (file (cond ((stringp byte-compile-current-file)
  901. (format "%s:" (file-relative-name
  902. byte-compile-current-file dir)))
  903. ((bufferp byte-compile-current-file)
  904. (format "Buffer %s:"
  905. (buffer-name byte-compile-current-file)))
  906. (t "")))
  907. (pos (if (and byte-compile-current-file
  908. (integerp byte-compile-read-position))
  909. (with-current-buffer byte-compile-current-buffer
  910. (format "%d:%d:"
  911. (save-excursion
  912. (goto-char byte-compile-last-position)
  913. (1+ (count-lines (point-min) (point-at-bol))))
  914. (save-excursion
  915. (goto-char byte-compile-last-position)
  916. (1+ (current-column)))))
  917. ""))
  918. (form (if (eq byte-compile-current-form :end) "end of data"
  919. (or byte-compile-current-form "toplevel form"))))
  920. (when (or (and byte-compile-current-file
  921. (not (equal byte-compile-current-file
  922. byte-compile-last-logged-file)))
  923. (and byte-compile-current-form
  924. (not (eq byte-compile-current-form
  925. byte-compile-last-warned-form))))
  926. (insert (format "\nIn %s:\n" form)))
  927. (when level
  928. (insert (format "%s%s" file pos))))
  929. (setq byte-compile-last-logged-file byte-compile-current-file
  930. byte-compile-last-warned-form byte-compile-current-form)
  931. entry)
  932. ;; This no-op function is used as the value of warning-series
  933. ;; to tell inner calls to displaying-byte-compile-warnings
  934. ;; not to bind warning-series.
  935. (defun byte-compile-warning-series (&rest _ignore)
  936. nil)
  937. ;; (compile-mode) will cause this to be loaded.
  938. (declare-function compilation-forget-errors "compile" ())
  939. ;; Log the start of a file in `byte-compile-log-buffer', and mark it as done.
  940. ;; Return the position of the start of the page in the log buffer.
  941. ;; But do nothing in batch mode.
  942. (defun byte-compile-log-file ()
  943. (and (not (equal byte-compile-current-file byte-compile-last-logged-file))
  944. (not noninteractive)
  945. (with-current-buffer (get-buffer-create byte-compile-log-buffer)
  946. (goto-char (point-max))
  947. (let* ((inhibit-read-only t)
  948. (dir (and byte-compile-current-file
  949. (file-name-directory byte-compile-current-file)))
  950. (was-same (equal default-directory dir))
  951. pt)
  952. (when dir
  953. (unless was-same
  954. (insert (format "Leaving directory `%s'\n" default-directory))))
  955. (unless (bolp)
  956. (insert "\n"))
  957. (setq pt (point-marker))
  958. (if byte-compile-current-file
  959. (insert "\f\nCompiling "
  960. (if (stringp byte-compile-current-file)
  961. (concat "file " byte-compile-current-file)
  962. (concat "buffer "
  963. (buffer-name byte-compile-current-file)))
  964. " at " (current-time-string) "\n")
  965. (insert "\f\nCompiling no file at " (current-time-string) "\n"))
  966. (when dir
  967. (setq default-directory dir)
  968. (unless was-same
  969. (insert (format "Entering directory `%s'\n"
  970. default-directory))))
  971. (setq byte-compile-last-logged-file byte-compile-current-file
  972. byte-compile-last-warned-form nil)
  973. ;; Do this after setting default-directory.
  974. (unless (derived-mode-p 'compilation-mode) (compilation-mode))
  975. (compilation-forget-errors)
  976. pt))))
  977. ;; Log a message STRING in `byte-compile-log-buffer'.
  978. ;; Also log the current function and file if not already done.
  979. (defun byte-compile-log-warning (string &optional fill level)
  980. (let ((warning-prefix-function 'byte-compile-warning-prefix)
  981. (warning-type-format "")
  982. (warning-fill-prefix (if fill " "))
  983. (inhibit-read-only t))
  984. (display-warning 'bytecomp string level byte-compile-log-buffer)))
  985. (defun byte-compile-warn (format &rest args)
  986. "Issue a byte compiler warning; use (format FORMAT ARGS...) for message."
  987. (setq format (apply 'format format args))
  988. (if byte-compile-error-on-warn
  989. (error "%s" format) ; byte-compile-file catches and logs it
  990. (byte-compile-log-warning format t :warning)))
  991. (defun byte-compile-warn-obsolete (symbol)
  992. "Warn that SYMBOL (a variable or function) is obsolete."
  993. (when (byte-compile-warning-enabled-p 'obsolete)
  994. (let* ((funcp (get symbol 'byte-obsolete-info))
  995. (obsolete (or funcp (get symbol 'byte-obsolete-variable)))
  996. (instead (car obsolete))
  997. (asof (nth 2 obsolete)))
  998. (unless (and funcp (memq symbol byte-compile-not-obsolete-funcs))
  999. (byte-compile-warn "`%s' is an obsolete %s%s%s" symbol
  1000. (if funcp "function" "variable")
  1001. (if asof (concat " (as of " asof ")") "")
  1002. (cond ((stringp instead)
  1003. (concat "; " instead))
  1004. (instead
  1005. (format "; use `%s' instead." instead))
  1006. (t ".")))))))
  1007. (defun byte-compile-report-error (error-info)
  1008. "Report Lisp error in compilation. ERROR-INFO is the error data."
  1009. (setq byte-compiler-error-flag t)
  1010. (byte-compile-log-warning
  1011. (error-message-string error-info)
  1012. nil :error))
  1013. ;;; sanity-checking arglists
  1014. (defun byte-compile-fdefinition (name macro-p)
  1015. ;; If a function has an entry saying (FUNCTION . t).
  1016. ;; that means we know it is defined but we don't know how.
  1017. ;; If a function has an entry saying (FUNCTION . nil),
  1018. ;; that means treat it as not defined.
  1019. (let* ((list (if macro-p
  1020. byte-compile-macro-environment
  1021. byte-compile-function-environment))
  1022. (env (cdr (assq name list))))
  1023. (or env
  1024. (let ((fn name))
  1025. (while (and (symbolp fn)
  1026. (fboundp fn)
  1027. (or (symbolp (symbol-function fn))
  1028. (consp (symbol-function fn))
  1029. (and (not macro-p)
  1030. (byte-code-function-p (symbol-function fn)))))
  1031. (setq fn (symbol-function fn)))
  1032. (let ((advertised (gethash (if (and (symbolp fn) (fboundp fn))
  1033. ;; Could be a subr.
  1034. (symbol-function fn)
  1035. fn)
  1036. advertised-signature-table t)))
  1037. (cond
  1038. ((listp advertised)
  1039. (if macro-p
  1040. `(macro lambda ,advertised)
  1041. `(lambda ,advertised)))
  1042. ((and (not macro-p) (byte-code-function-p fn)) fn)
  1043. ((not (consp fn)) nil)
  1044. ((eq 'macro (car fn)) (cdr fn))
  1045. (macro-p nil)
  1046. ((eq 'autoload (car fn)) nil)
  1047. (t fn)))))))
  1048. (defun byte-compile-arglist-signature (arglist)
  1049. (if (integerp arglist)
  1050. ;; New style byte-code arglist.
  1051. (cons (logand arglist 127) ;Mandatory.
  1052. (if (zerop (logand arglist 128)) ;No &rest.
  1053. (lsh arglist -8))) ;Nonrest.
  1054. ;; Old style byte-code, or interpreted function.
  1055. (let ((args 0)
  1056. opts
  1057. restp)
  1058. (while arglist
  1059. (cond ((eq (car arglist) '&optional)
  1060. (or opts (setq opts 0)))
  1061. ((eq (car arglist) '&rest)
  1062. (if (cdr arglist)
  1063. (setq restp t
  1064. arglist nil)))
  1065. (t
  1066. (if opts
  1067. (setq opts (1+ opts))
  1068. (setq args (1+ args)))))
  1069. (setq arglist (cdr arglist)))
  1070. (cons args (if restp nil (if opts (+ args opts) args))))))
  1071. (defun byte-compile-arglist-signatures-congruent-p (old new)
  1072. (not (or
  1073. (> (car new) (car old)) ; requires more args now
  1074. (and (null (cdr old)) ; took rest-args, doesn't any more
  1075. (cdr new))
  1076. (and (cdr new) (cdr old) ; can't take as many args now
  1077. (< (cdr new) (cdr old)))
  1078. )))
  1079. (defun byte-compile-arglist-signature-string (signature)
  1080. (cond ((null (cdr signature))
  1081. (format "%d+" (car signature)))
  1082. ((= (car signature) (cdr signature))
  1083. (format "%d" (car signature)))
  1084. (t (format "%d-%d" (car signature) (cdr signature)))))
  1085. ;; Warn if the form is calling a function with the wrong number of arguments.
  1086. (defun byte-compile-callargs-warn (form)
  1087. (let* ((def (or (byte-compile-fdefinition (car form) nil)
  1088. (byte-compile-fdefinition (car form) t)))
  1089. (sig (if (and def (not (eq def t)))
  1090. (progn
  1091. (and (eq (car-safe def) 'macro)
  1092. (eq (car-safe (cdr-safe def)) 'lambda)
  1093. (setq def (cdr def)))
  1094. (byte-compile-arglist-signature
  1095. (if (memq (car-safe def) '(declared lambda))
  1096. (nth 1 def)
  1097. (if (byte-code-function-p def)
  1098. (aref def 0)
  1099. '(&rest def)))))
  1100. (if (and (fboundp (car form))
  1101. (subrp (symbol-function (car form))))
  1102. (subr-arity (symbol-function (car form))))))
  1103. (ncall (length (cdr form))))
  1104. ;; Check many or unevalled from subr-arity.
  1105. (if (and (cdr-safe sig)
  1106. (not (numberp (cdr sig))))
  1107. (setcdr sig nil))
  1108. (if sig
  1109. (when (or (< ncall (car sig))
  1110. (and (cdr sig) (> ncall (cdr sig))))
  1111. (byte-compile-set-symbol-position (car form))
  1112. (byte-compile-warn
  1113. "%s called with %d argument%s, but %s %s"
  1114. (car form) ncall
  1115. (if (= 1 ncall) "" "s")
  1116. (if (< ncall (car sig))
  1117. "requires"
  1118. "accepts only")
  1119. (byte-compile-arglist-signature-string sig))))
  1120. (byte-compile-format-warn form)
  1121. ;; Check to see if the function will be available at runtime
  1122. ;; and/or remember its arity if it's unknown.
  1123. (or (and (or def (fboundp (car form))) ; might be a subr or autoload.
  1124. (not (memq (car form) byte-compile-noruntime-functions)))
  1125. (eq (car form) byte-compile-current-form) ; ## this doesn't work
  1126. ; with recursion.
  1127. ;; It's a currently-undefined function.
  1128. ;; Remember number of args in call.
  1129. (let ((cons (assq (car form) byte-compile-unresolved-functions))
  1130. (n (length (cdr form))))
  1131. (if cons
  1132. (or (memq n (cdr cons))
  1133. (setcdr cons (cons n (cdr cons))))
  1134. (push (list (car form) n)
  1135. byte-compile-unresolved-functions))))))
  1136. (defun byte-compile-format-warn (form)
  1137. "Warn if FORM is `format'-like with inconsistent args.
  1138. Applies if head of FORM is a symbol with non-nil property
  1139. `byte-compile-format-like' and first arg is a constant string.
  1140. Then check the number of format fields matches the number of
  1141. extra args."
  1142. (when (and (symbolp (car form))
  1143. (stringp (nth 1 form))
  1144. (get (car form) 'byte-compile-format-like))
  1145. (let ((nfields (with-temp-buffer
  1146. (insert (nth 1 form))
  1147. (goto-char (point-min))
  1148. (let ((n 0))
  1149. (while (re-search-forward "%." nil t)
  1150. (unless (eq ?% (char-after (1+ (match-beginning 0))))
  1151. (setq n (1+ n))))
  1152. n)))
  1153. (nargs (- (length form) 2)))
  1154. (unless (= nargs nfields)
  1155. (byte-compile-warn
  1156. "`%s' called with %d args to fill %d format field(s)" (car form)
  1157. nargs nfields)))))
  1158. (dolist (elt '(format message error))
  1159. (put elt 'byte-compile-format-like t))
  1160. ;; Warn if a custom definition fails to specify :group.
  1161. (defun byte-compile-nogroup-warn (form)
  1162. (if (and (memq (car form) '(custom-declare-face custom-declare-variable))
  1163. byte-compile-current-group)
  1164. ;; The group will be provided implicitly.
  1165. nil
  1166. (let ((keyword-args (cdr (cdr (cdr (cdr form)))))
  1167. (name (cadr form)))
  1168. (or (not (eq (car-safe name) 'quote))
  1169. (and (eq (car form) 'custom-declare-group)
  1170. (equal name ''emacs))
  1171. (plist-get keyword-args :group)
  1172. (not (and (consp name) (eq (car name) 'quote)))
  1173. (byte-compile-warn
  1174. "%s for `%s' fails to specify containing group"
  1175. (cdr (assq (car form)
  1176. '((custom-declare-group . defgroup)
  1177. (custom-declare-face . defface)
  1178. (custom-declare-variable . defcustom))))
  1179. (cadr name)))
  1180. ;; Update the current group, if needed.
  1181. (if (and byte-compile-current-file ;Only when compiling a whole file.
  1182. (eq (car form) 'custom-declare-group)
  1183. (eq (car-safe name) 'quote))
  1184. (setq byte-compile-current-group (cadr name))))))
  1185. ;; Warn if the function or macro is being redefined with a different
  1186. ;; number of arguments.
  1187. (defun byte-compile-arglist-warn (form macrop)
  1188. (let* ((name (nth 1 form))
  1189. (old (byte-compile-fdefinition name macrop))
  1190. (initial (and macrop
  1191. (cdr (assq name
  1192. byte-compile-initial-macro-environment)))))
  1193. ;; Assumes an element of b-c-i-macro-env that is a symbol points
  1194. ;; to a defined function. (Bug#8646)
  1195. (and initial (symbolp initial)
  1196. (setq old (byte-compile-fdefinition initial nil)))
  1197. (if (and old (not (eq old t)))
  1198. (progn
  1199. (and (eq 'macro (car-safe old))
  1200. (eq 'lambda (car-safe (cdr-safe old)))
  1201. (setq old (cdr old)))
  1202. (let ((sig1 (byte-compile-arglist-signature
  1203. (pcase old
  1204. (`(lambda ,args . ,_) args)
  1205. (`(closure ,_ ,args . ,_) args)
  1206. ((pred byte-code-function-p) (aref old 0))
  1207. (t '(&rest def)))))
  1208. (sig2 (byte-compile-arglist-signature (nth 2 form))))
  1209. (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2)
  1210. (byte-compile-set-symbol-position name)
  1211. (byte-compile-warn
  1212. "%s %s used to take %s %s, now takes %s"
  1213. (if (eq (car form) 'defun) "function" "macro")
  1214. name
  1215. (byte-compile-arglist-signature-string sig1)
  1216. (if (equal sig1 '(1 . 1)) "argument" "arguments")
  1217. (byte-compile-arglist-signature-string sig2)))))
  1218. ;; This is the first definition. See if previous calls are compatible.
  1219. (let ((calls (assq name byte-compile-unresolved-functions))
  1220. nums sig min max)
  1221. (when calls
  1222. (when (and (symbolp name)
  1223. (eq (get name 'byte-optimizer)
  1224. 'byte-compile-inline-expand))
  1225. (byte-compile-warn "defsubst `%s' was used before it was defined"
  1226. name))
  1227. (setq sig (byte-compile-arglist-signature (nth 2 form))
  1228. nums (sort (copy-sequence (cdr calls)) (function <))
  1229. min (car nums)
  1230. max (car (nreverse nums)))
  1231. (when (or (< min (car sig))
  1232. (and (cdr sig) (> max (cdr sig))))
  1233. (byte-compile-set-symbol-position name)
  1234. (byte-compile-warn
  1235. "%s being defined to take %s%s, but was previously called with %s"
  1236. name
  1237. (byte-compile-arglist-signature-string sig)
  1238. (if (equal sig '(1 . 1)) " arg" " args")
  1239. (byte-compile-arglist-signature-string (cons min max))))
  1240. (setq byte-compile-unresolved-functions
  1241. (delq calls byte-compile-unresolved-functions)))))))
  1242. (defvar byte-compile-cl-functions nil
  1243. "List of functions defined in CL.")
  1244. ;; Can't just add this to cl-load-hook, because that runs just before
  1245. ;; the forms from cl.el get added to load-history.
  1246. (defun byte-compile-find-cl-functions ()
  1247. (unless byte-compile-cl-functions
  1248. (dolist (elt load-history)
  1249. (and (byte-compile-cl-file-p (car elt))
  1250. (dolist (e (cdr elt))
  1251. ;; Includes the cl-foo functions that cl autoloads.
  1252. (when (memq (car-safe e) '(autoload defun))
  1253. (push (cdr e) byte-compile-cl-functions)))))))
  1254. (defun byte-compile-cl-warn (form)
  1255. "Warn if FORM is a call of a function from the CL package."
  1256. (let ((func (car-safe form)))
  1257. (if (and byte-compile-cl-functions
  1258. (memq func byte-compile-cl-functions)
  1259. ;; Aliases which won't have been expanded at this point.
  1260. ;; These aren't all aliases of subrs, so not trivial to
  1261. ;; avoid hardwiring the list.
  1262. (not (memq func
  1263. '(cl-block-wrapper cl-block-throw
  1264. multiple-value-call nth-value
  1265. copy-seq first second rest endp cl-member
  1266. ;; These are included in generated code
  1267. ;; that can't be called except at compile time
  1268. ;; or unless cl is loaded anyway.
  1269. cl-defsubst-expand cl-struct-setf-expander
  1270. ;; These would sometimes be warned about
  1271. ;; but such warnings are never useful,
  1272. ;; so don't warn about them.
  1273. macroexpand cl-macroexpand-all
  1274. cl-compiling-file))))
  1275. (byte-compile-warn "function `%s' from cl package called at runtime"
  1276. func)))
  1277. form)
  1278. (defun byte-compile-print-syms (str1 strn syms)
  1279. (when syms
  1280. (byte-compile-set-symbol-position (car syms) t))
  1281. (cond ((and (cdr syms) (not noninteractive))
  1282. (let* ((str strn)
  1283. (L (length str))
  1284. s)
  1285. (while syms
  1286. (setq s (symbol-name (pop syms))
  1287. L (+ L (length s) 2))
  1288. (if (< L (1- fill-column))
  1289. (setq str (concat str " " s (and syms ",")))
  1290. (setq str (concat str "\n " s (and syms ","))
  1291. L (+ (length s) 4))))
  1292. (byte-compile-warn "%s" str)))
  1293. ((cdr syms)
  1294. (byte-compile-warn "%s %s"
  1295. strn
  1296. (mapconcat #'symbol-name syms ", ")))
  1297. (syms
  1298. (byte-compile-warn str1 (car syms)))))
  1299. ;; If we have compiled any calls to functions which are not known to be
  1300. ;; defined, issue a warning enumerating them.
  1301. ;; `unresolved' in the list `byte-compile-warnings' disables this.
  1302. (defun byte-compile-warn-about-unresolved-functions ()
  1303. (when (byte-compile-warning-enabled-p 'unresolved)
  1304. (let ((byte-compile-current-form :end)
  1305. (noruntime nil)
  1306. (unresolved nil))
  1307. ;; Separate the functions that will not be available at runtime
  1308. ;; from the truly unresolved ones.
  1309. (dolist (f byte-compile-unresolved-functions)
  1310. (setq f (car f))
  1311. (if (fboundp f) (push f noruntime) (push f unresolved)))
  1312. ;; Complain about the no-run-time functions
  1313. (byte-compile-print-syms
  1314. "the function `%s' might not be defined at runtime."
  1315. "the following functions might not be defined at runtime:"
  1316. noruntime)
  1317. ;; Complain about the unresolved functions
  1318. (byte-compile-print-syms
  1319. "the function `%s' is not known to be defined."
  1320. "the following functions are not known to be defined:"
  1321. unresolved)))
  1322. nil)
  1323. (defsubst byte-compile-const-symbol-p (symbol &optional any-value)
  1324. "Non-nil if SYMBOL is constant.
  1325. If ANY-VALUE is nil, only return non-nil if the value of the symbol is the
  1326. symbol itself."
  1327. (or (memq symbol '(nil t))
  1328. (keywordp symbol)
  1329. (if any-value
  1330. (or (memq symbol byte-compile-const-variables)
  1331. ;; FIXME: We should provide a less intrusive way to find out
  1332. ;; if a variable is "constant".
  1333. (and (boundp symbol)
  1334. (condition-case nil
  1335. (progn (set symbol (symbol-value symbol)) nil)
  1336. (setting-constant t)))))))
  1337. (defmacro byte-compile-constp (form)
  1338. "Return non-nil if FORM is a constant."
  1339. `(cond ((consp ,form) (eq (car ,form) 'quote))
  1340. ((not (symbolp ,form)))
  1341. ((byte-compile-const-symbol-p ,form))))
  1342. (defmacro byte-compile-close-variables (&rest body)
  1343. (declare (debug t))
  1344. (cons 'let
  1345. (cons '(;;
  1346. ;; Close over these variables to encapsulate the
  1347. ;; compilation state
  1348. ;;
  1349. (byte-compile-macro-environment
  1350. ;; Copy it because the compiler may patch into the
  1351. ;; macroenvironment.
  1352. (copy-alist byte-compile-initial-macro-environment))
  1353. (byte-compile-function-environment nil)
  1354. (byte-compile-bound-variables nil)
  1355. (byte-compile-const-variables nil)
  1356. (byte-compile-free-references nil)
  1357. (byte-compile-free-assignments nil)
  1358. ;;
  1359. ;; Close over these variables so that `byte-compiler-options'
  1360. ;; can change them on a per-file basis.
  1361. ;;
  1362. (byte-compile-verbose byte-compile-verbose)
  1363. (byte-optimize byte-optimize)
  1364. (byte-compile-dynamic byte-compile-dynamic)
  1365. (byte-compile-dynamic-docstrings
  1366. byte-compile-dynamic-docstrings)
  1367. ;; (byte-compile-generate-emacs19-bytecodes
  1368. ;; byte-compile-generate-emacs19-bytecodes)
  1369. (byte-compile-warnings byte-compile-warnings)
  1370. )
  1371. body)))
  1372. (defmacro displaying-byte-compile-warnings (&rest body)
  1373. (declare (debug t))
  1374. `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body))
  1375. (warning-series-started
  1376. (and (markerp warning-series)
  1377. (eq (marker-buffer warning-series)
  1378. (get-buffer byte-compile-log-buffer)))))
  1379. (byte-compile-find-cl-functions)
  1380. (if (or (eq warning-series 'byte-compile-warning-series)
  1381. warning-series-started)
  1382. ;; warning-series does come from compilation,
  1383. ;; so don't bind it, but maybe do set it.
  1384. (let (tem)
  1385. ;; Log the file name. Record position of that text.
  1386. (setq tem (byte-compile-log-file))
  1387. (unless warning-series-started
  1388. (setq warning-series (or tem 'byte-compile-warning-series)))
  1389. (if byte-compile-debug
  1390. (funcall --displaying-byte-compile-warnings-fn)
  1391. (condition-case error-info
  1392. (funcall --displaying-byte-compile-warnings-fn)
  1393. (error (byte-compile-report-error error-info)))))
  1394. ;; warning-series does not come from compilation, so bind it.
  1395. (let ((warning-series
  1396. ;; Log the file name. Record position of that text.
  1397. (or (byte-compile-log-file) 'byte-compile-warning-series)))
  1398. (if byte-compile-debug
  1399. (funcall --displaying-byte-compile-warnings-fn)
  1400. (condition-case error-info
  1401. (funcall --displaying-byte-compile-warnings-fn)
  1402. (error (byte-compile-report-error error-info))))))))
  1403. ;;;###autoload
  1404. (defun byte-force-recompile (directory)
  1405. "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
  1406. Files in subdirectories of DIRECTORY are processed also."
  1407. (interactive "DByte force recompile (directory): ")
  1408. (byte-recompile-directory directory nil t))
  1409. ;;;###autoload
  1410. (defun byte-recompile-directory (directory &optional arg force)
  1411. "Recompile every `.el' file in DIRECTORY that needs recompilation.
  1412. This happens when a `.elc' file exists but is older than the `.el' file.
  1413. Files in subdirectories of DIRECTORY are processed also.
  1414. If the `.elc' file does not exist, normally this function *does not*
  1415. compile the corresponding `.el' file. However, if the prefix argument
  1416. ARG is 0, that means do compile all those files. A nonzero
  1417. ARG means ask the user, for each such `.el' file, whether to
  1418. compile it. A nonzero ARG also means ask about each subdirectory
  1419. before scanning it.
  1420. If the third argument FORCE is non-nil, recompile every `.el' file
  1421. that already has a `.elc' file."
  1422. (interactive "DByte recompile directory: \nP")
  1423. (if arg (setq arg (prefix-numeric-value arg)))
  1424. (if noninteractive
  1425. nil
  1426. (save-some-buffers)
  1427. (force-mode-line-update))
  1428. (with-current-buffer (get-buffer-create byte-compile-log-buffer)
  1429. (setq default-directory (expand-file-name directory))
  1430. ;; compilation-mode copies value of default-directory.
  1431. (unless (eq major-mode 'compilation-mode)
  1432. (compilation-mode))
  1433. (let ((directories (list default-directory))
  1434. (default-directory default-directory)
  1435. (skip-count 0)
  1436. (fail-count 0)
  1437. (file-count 0)
  1438. (dir-count 0)
  1439. last-dir)
  1440. (displaying-byte-compile-warnings
  1441. (while directories
  1442. (setq directory (car directories))
  1443. (message "Checking %s..." directory)
  1444. (dolist (file (directory-files directory))
  1445. (let ((source (expand-file-name file directory)))
  1446. (if (and (not (member file '("RCS" "CVS")))
  1447. (not (eq ?\. (aref file 0)))
  1448. (file-directory-p source)
  1449. (not (file-symlink-p source)))
  1450. ;; This file is a subdirectory. Handle them differently.
  1451. (when (or (null arg) (eq 0 arg)
  1452. (y-or-n-p (concat "Check " source "? ")))
  1453. (setq directories (nconc directories (list source))))
  1454. ;; It is an ordinary file. Decide whether to compile it.
  1455. (if (and (string-match emacs-lisp-file-regexp source)
  1456. (file-readable-p source)
  1457. (not (auto-save-file-name-p source))
  1458. (not (string-equal dir-locals-file
  1459. (file-name-nondirectory source))))
  1460. (progn (case (byte-recompile-file source force arg)
  1461. (no-byte-compile (setq skip-count (1+ skip-count)))
  1462. ((t) (setq file-count (1+ file-count)))
  1463. ((nil) (setq fail-count (1+ fail-count))))
  1464. (or noninteractive
  1465. (message "Checking %s..." directory))
  1466. (if (not (eq last-dir directory))
  1467. (setq last-dir directory
  1468. dir-count (1+ dir-count)))
  1469. )))))
  1470. (setq directories (cdr directories))))
  1471. (message "Done (Total of %d file%s compiled%s%s%s)"
  1472. file-count (if (= file-count 1) "" "s")
  1473. (if (> fail-count 0) (format ", %d failed" fail-count) "")
  1474. (if (> skip-count 0) (format ", %d skipped" skip-count) "")
  1475. (if (> dir-count 1)
  1476. (format " in %d directories" dir-count) "")))))
  1477. (defvar no-byte-compile nil
  1478. "Non-nil to prevent byte-compiling of Emacs Lisp code.
  1479. This is normally set in local file variables at the end of the elisp file:
  1480. \;; Local Variables:\n;; no-byte-compile: t\n;; End: ") ;Backslash for compile-main.
  1481. ;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
  1482. (defun byte-recompile-file (filename &optional force arg load)
  1483. "Recompile FILENAME file if it needs recompilation.
  1484. This happens when its `.elc' file is older than itself.
  1485. If the `.elc' file exists and is up-to-date, normally this
  1486. function *does not* compile FILENAME. However, if the
  1487. prefix argument FORCE is set, that means do compile
  1488. FILENAME even if the destination already exists and is
  1489. up-to-date.
  1490. If the `.elc' file does not exist, normally this function *does
  1491. not* compile FILENAME. If ARG is 0, that means
  1492. compile the file even if it has never been compiled before.
  1493. A nonzero ARG means ask the user.
  1494. If LOAD is set, `load' the file after compiling.
  1495. The value returned is the value returned by `byte-compile-file',
  1496. or 'no-byte-compile if the file did not need recompilation."
  1497. (interactive
  1498. (let ((file buffer-file-name)
  1499. (file-name nil)
  1500. (file-dir nil))
  1501. (and file
  1502. (derived-mode-p 'emacs-lisp-mode)
  1503. (setq file-name (file-name-nondirectory file)
  1504. file-dir (file-name-directory file)))
  1505. (list (read-file-name (if current-prefix-arg
  1506. "Byte compile file: "
  1507. "Byte recompile file: ")
  1508. file-dir file-name nil)
  1509. current-prefix-arg)))
  1510. (let ((dest (byte-compile-dest-file filename))
  1511. ;; Expand now so we get the current buffer's defaults
  1512. (filename (expand-file-name filename)))
  1513. (if (if (file-exists-p dest)
  1514. ;; File was already compiled
  1515. ;; Compile if forced to, or filename newer
  1516. (or force
  1517. (file-newer-than-file-p filename dest))
  1518. (and arg
  1519. (or (eq 0 arg)
  1520. (y-or-n-p (concat "Compile "
  1521. filename "? ")))))
  1522. (progn
  1523. (if (and noninteractive (not byte-compile-verbose))
  1524. (message "Compiling %s..." filename))
  1525. (byte-compile-file filename load))
  1526. (when load (load filename))
  1527. 'no-byte-compile)))
  1528. ;;;###autoload
  1529. (defun byte-compile-file (filename &optional load)
  1530. "Compile a file of Lisp code named FILENAME into a file of byte code.
  1531. The output file's name is generated by passing FILENAME to the
  1532. function `byte-compile-dest-file' (which see).
  1533. With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
  1534. The value is non-nil if there were no errors, nil if errors."
  1535. ;; (interactive "fByte compile file: \nP")
  1536. (interactive
  1537. (let ((file buffer-file-name)
  1538. (file-name nil)
  1539. (file-dir nil))
  1540. (and file
  1541. (derived-mode-p 'emacs-lisp-mode)
  1542. (setq file-name (file-name-nondirectory file)
  1543. file-dir (file-name-directory file)))
  1544. (list (read-file-name (if current-prefix-arg
  1545. "Byte compile and load file: "
  1546. "Byte compile file: ")
  1547. file-dir file-name nil)
  1548. current-prefix-arg)))
  1549. ;; Expand now so we get the current buffer's defaults
  1550. (setq filename (expand-file-name filename))
  1551. ;; If we're compiling a file that's in a buffer and is modified, offer
  1552. ;; to save it first.
  1553. (or noninteractive
  1554. (let ((b (get-file-buffer (expand-file-name filename))))
  1555. (if (and b (buffer-modified-p b)
  1556. (y-or-n-p (format "Save buffer %s first? " (buffer-name b))))
  1557. (with-current-buffer b (save-buffer)))))
  1558. ;; Force logging of the file name for each file compiled.
  1559. (setq byte-compile-last-logged-file nil)
  1560. (let ((byte-compile-current-file filename)
  1561. (byte-compile-current-group nil)
  1562. (set-auto-coding-for-load t)
  1563. target-file input-buffer output-buffer
  1564. byte-compile-dest-file)
  1565. (setq target-file (byte-compile-dest-file filename))
  1566. (setq byte-compile-dest-file target-file)
  1567. (with-current-buffer
  1568. (setq input-buffer (get-buffer-create " *Compiler Input*"))
  1569. (erase-buffer)
  1570. (setq buffer-file-coding-system nil)
  1571. ;; Always compile an Emacs Lisp file as multibyte
  1572. ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
  1573. (set-buffer-multibyte t)
  1574. (insert-file-contents filename)
  1575. ;; Mimic the way after-insert-file-set-coding can make the
  1576. ;; buffer unibyte when visiting this file.
  1577. (when (or (eq last-coding-system-used 'no-conversion)
  1578. (eq (coding-system-type last-coding-system-used) 5))
  1579. ;; For coding systems no-conversion and raw-text...,
  1580. ;; edit the buffer as unibyte.
  1581. (set-buffer-multibyte nil))
  1582. ;; Run hooks including the uncompression hook.
  1583. ;; If they change the file name, then change it for the output also.
  1584. (letf ((buffer-file-name filename)
  1585. ((default-value 'major-mode) 'emacs-lisp-mode)
  1586. ;; Ignore unsafe local variables.
  1587. ;; We only care about a few of them for our purposes.
  1588. (enable-local-variables :safe)
  1589. (enable-local-eval nil))
  1590. ;; Arg of t means don't alter enable-local-variables.
  1591. (normal-mode t)
  1592. ;; There may be a file local variable setting (bug#10419).
  1593. (setq buffer-read-only nil
  1594. filename buffer-file-name))
  1595. ;; Set the default directory, in case an eval-when-compile uses it.
  1596. (setq default-directory (file-name-directory filename)))
  1597. ;; Check if the file's local variables explicitly specify not to
  1598. ;; compile this file.
  1599. (if (with-current-buffer input-buffer no-byte-compile)
  1600. (progn
  1601. ;; (message "%s not compiled because of `no-byte-compile: %s'"
  1602. ;; (file-relative-name filename)
  1603. ;; (with-current-buffer input-buffer no-byte-compile))
  1604. (when (file-exists-p target-file)
  1605. (message "%s deleted because of `no-byte-compile: %s'"
  1606. (file-relative-name target-file)
  1607. (buffer-local-value 'no-byte-compile input-buffer))
  1608. (condition-case nil (delete-file target-file) (error nil)))
  1609. ;; We successfully didn't compile this file.
  1610. 'no-byte-compile)
  1611. (when byte-compile-verbose
  1612. (message "Compiling %s..." filename))
  1613. (setq byte-compiler-error-flag nil)
  1614. ;; It is important that input-buffer not be current at this call,
  1615. ;; so that the value of point set in input-buffer
  1616. ;; within byte-compile-from-buffer lingers in that buffer.
  1617. (setq output-buffer
  1618. (save-current-buffer
  1619. (byte-compile-from-buffer input-buffer)))
  1620. (if byte-compiler-error-flag
  1621. nil
  1622. (when byte-compile-verbose
  1623. (message "Compiling %s...done" filename))
  1624. (kill-buffer input-buffer)
  1625. (with-current-buffer output-buffer
  1626. (goto-char (point-max))
  1627. (insert "\n") ; aaah, unix.
  1628. (if (file-writable-p target-file)
  1629. ;; We must disable any code conversion here.
  1630. (let* ((coding-system-for-write 'no-conversion)
  1631. ;; Write to a tempfile so that if another Emacs
  1632. ;; process is trying to load target-file (eg in a
  1633. ;; parallel bootstrap), it does not risk getting a
  1634. ;; half-finished file. (Bug#4196)
  1635. (tempfile (make-temp-name target-file))
  1636. (kill-emacs-hook
  1637. (cons (lambda () (ignore-errors (delete-file tempfile)))
  1638. kill-emacs-hook)))
  1639. (if (memq system-type '(ms-dos 'windows-nt))
  1640. (setq buffer-file-type t))
  1641. (write-region (point-min) (point-max) tempfile nil 1)
  1642. ;; This has the intentional side effect that any
  1643. ;; hard-links to target-file continue to
  1644. ;; point to the old file (this makes it possible
  1645. ;; for installed files to share disk space with
  1646. ;; the build tree, without causing problems when
  1647. ;; emacs-lisp files in the build tree are
  1648. ;; recompiled). Previously this was accomplished by
  1649. ;; deleting target-file before writing it.
  1650. (rename-file tempfile target-file t)
  1651. (message "Wrote %s" target-file))
  1652. ;; This is just to give a better error message than write-region
  1653. (signal 'file-error
  1654. (list "Opening output file"
  1655. (if (file-exists-p target-file)
  1656. "cannot overwrite file"
  1657. "directory not writable or nonexistent")
  1658. target-file)))
  1659. (kill-buffer (current-buffer)))
  1660. (if (and byte-compile-generate-call-tree
  1661. (or (eq t byte-compile-generate-call-tree)
  1662. (y-or-n-p (format "Report call tree for %s? "
  1663. filename))))
  1664. (save-excursion
  1665. (display-call-tree filename)))
  1666. (if load
  1667. (load target-file))
  1668. t))))
  1669. ;;; compiling a single function
  1670. ;;;###autoload
  1671. (defun compile-defun (&optional arg)
  1672. "Compile and evaluate the current top-level form.
  1673. Print the result in the echo area.
  1674. With argument ARG, insert value in current buffer after the form."
  1675. (interactive "P")
  1676. (save-excursion
  1677. (end-of-defun)
  1678. (beginning-of-defun)
  1679. (let* ((byte-compile-current-file nil)
  1680. (byte-compile-current-buffer (current-buffer))
  1681. (byte-compile-read-position (point))
  1682. (byte-compile-last-position byte-compile-read-position)
  1683. (byte-compile-last-warned-form 'nothing)
  1684. (value (eval
  1685. (let ((read-with-symbol-positions (current-buffer))
  1686. (read-symbol-positions-list nil))
  1687. (displaying-byte-compile-warnings
  1688. (byte-compile-sexp (read (current-buffer)))))
  1689. lexical-binding)))
  1690. (cond (arg
  1691. (message "Compiling from buffer... done.")
  1692. (prin1 value (current-buffer))
  1693. (insert "\n"))
  1694. ((message "%s" (prin1-to-string value)))))))
  1695. ;; Dynamically bound in byte-compile-from-buffer.
  1696. ;; NB also used in cl.el and cl-macs.el.
  1697. (defvar byte-compile--outbuffer)
  1698. (defun byte-compile-from-buffer (inbuffer)
  1699. (let (byte-compile--outbuffer
  1700. (byte-compile-current-buffer inbuffer)
  1701. (byte-compile-read-position nil)
  1702. (byte-compile-last-position nil)
  1703. ;; Prevent truncation of flonums and lists as we read and print them
  1704. (float-output-format nil)
  1705. (case-fold-search nil)
  1706. (print-length nil)
  1707. (print-level nil)
  1708. ;; Prevent edebug from interfering when we compile
  1709. ;; and put the output into a file.
  1710. ;; (edebug-all-defs nil)
  1711. ;; (edebug-all-forms nil)
  1712. ;; Simulate entry to byte-compile-top-level
  1713. (byte-compile-constants nil)
  1714. (byte-compile-variables nil)
  1715. (byte-compile-tag-number 0)
  1716. (byte-compile-depth 0)
  1717. (byte-compile-maxdepth 0)
  1718. (byte-compile-output nil)
  1719. ;; This allows us to get the positions of symbols read; it's
  1720. ;; new in Emacs 22.1.
  1721. (read-with-symbol-positions inbuffer)
  1722. (read-symbol-positions-list nil)
  1723. ;; #### This is bound in b-c-close-variables.
  1724. ;; (byte-compile-warnings byte-compile-warnings)
  1725. )
  1726. (byte-compile-close-variables
  1727. (with-current-buffer
  1728. (setq byte-compile--outbuffer
  1729. (get-buffer-create " *Compiler Output*"))
  1730. (set-buffer-multibyte t)
  1731. (erase-buffer)
  1732. ;; (emacs-lisp-mode)
  1733. (setq case-fold-search nil))
  1734. (displaying-byte-compile-warnings
  1735. (with-current-buffer inbuffer
  1736. (and byte-compile-current-file
  1737. (byte-compile-insert-header byte-compile-current-file
  1738. byte-compile--outbuffer))
  1739. (goto-char (point-min))
  1740. ;; Should we always do this? When calling multiple files, it
  1741. ;; would be useful to delay this warning until all have been
  1742. ;; compiled. A: Yes! b-c-u-f might contain dross from a
  1743. ;; previous byte-compile.
  1744. (setq byte-compile-unresolved-functions nil)
  1745. ;; Compile the forms from the input buffer.
  1746. (while (progn
  1747. (while (progn (skip-chars-forward " \t\n\^l")
  1748. (looking-at ";"))
  1749. (forward-line 1))
  1750. (not (eobp)))
  1751. (setq byte-compile-read-position (point)
  1752. byte-compile-last-position byte-compile-read-position)
  1753. (let* ((old-style-backquotes nil)
  1754. (form (read inbuffer)))
  1755. ;; Warn about the use of old-style backquotes.
  1756. (when old-style-backquotes
  1757. (byte-compile-warn "!! The file uses old-style backquotes !!
  1758. This functionality has been obsolete for more than 10 years already
  1759. and will be removed soon. See (elisp)Backquote in the manual."))
  1760. (byte-compile-toplevel-file-form form)))
  1761. ;; Compile pending forms at end of file.
  1762. (byte-compile-flush-pending)
  1763. ;; Make warnings about unresolved functions
  1764. ;; give the end of the file as their position.
  1765. (setq byte-compile-last-position (point-max))
  1766. (byte-compile-warn-about-unresolved-functions))
  1767. ;; Fix up the header at the front of the output
  1768. ;; if the buffer contains multibyte characters.
  1769. (and byte-compile-current-file
  1770. (with-current-buffer byte-compile--outbuffer
  1771. (byte-compile-fix-header byte-compile-current-file)))))
  1772. byte-compile--outbuffer))
  1773. (defun byte-compile-fix-header (_filename)
  1774. "If the current buffer has any multibyte characters, insert a version test."
  1775. (when (< (point-max) (position-bytes (point-max)))
  1776. (goto-char (point-min))
  1777. ;; Find the comment that describes the version condition.
  1778. (search-forward "\n;;; This file uses")
  1779. (narrow-to-region (line-beginning-position) (point-max))
  1780. ;; Find the first line of ballast semicolons.
  1781. (search-forward ";;;;;;;;;;")
  1782. (beginning-of-line)
  1783. (narrow-to-region (point-min) (point))
  1784. (let ((old-header-end (point))
  1785. (minimum-version "23")
  1786. delta)
  1787. (delete-region (point-min) (point-max))
  1788. (insert
  1789. ";;; This file contains utf-8 non-ASCII characters,\n"
  1790. ";;; and so cannot be loaded into Emacs 22 or earlier.\n"
  1791. ;; Have to check if emacs-version is bound so that this works
  1792. ;; in files loaded early in loadup.el.
  1793. "(and (boundp 'emacs-version)\n"
  1794. ;; If there is a name at the end of emacs-version,
  1795. ;; don't try to check the version number.
  1796. " (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
  1797. (format " (string-lessp emacs-version \"%s\")\n" minimum-version)
  1798. ;; Because the header must fit in a fixed width, we cannot
  1799. ;; insert arbitrary-length file names (Bug#11585).
  1800. " (error \"`%s' was compiled for "
  1801. (format "Emacs %s or later\" load-file-name))\n\n" minimum-version))
  1802. ;; Now compensate for any change in size, to make sure all
  1803. ;; positions in the file remain valid.
  1804. (setq delta (- (point-max) old-header-end))
  1805. (goto-char (point-max))
  1806. (widen)
  1807. (delete-char delta))))
  1808. (defun byte-compile-insert-header (filename outbuffer)
  1809. "Insert a header at the start of OUTBUFFER.
  1810. Call from the source buffer."
  1811. (let ((dynamic-docstrings byte-compile-dynamic-docstrings)
  1812. (dynamic byte-compile-dynamic)
  1813. (optimize byte-optimize))
  1814. (with-current-buffer outbuffer
  1815. (goto-char (point-min))
  1816. ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
  1817. ;; that is the file-format version number (18, 19, 20, or 23) as a
  1818. ;; byte, followed by some nulls. The primary motivation for doing
  1819. ;; this is to get some binary characters up in the first line of
  1820. ;; the file so that `diff' will simply say "Binary files differ"
  1821. ;; instead of actually doing a diff of two .elc files. An extra
  1822. ;; benefit is that you can add this to /etc/magic:
  1823. ;; 0 string ;ELC GNU Emacs Lisp compiled file,
  1824. ;; >4 byte x version %d
  1825. (insert
  1826. ";ELC" 23 "\000\000\000\n"
  1827. ";;; Compiled by "
  1828. (or (and (boundp 'user-mail-address) user-mail-address)
  1829. (concat (user-login-name) "@" (system-name)))
  1830. " on " (current-time-string) "\n"
  1831. ";;; from file " filename "\n"
  1832. ";;; in Emacs version " emacs-version "\n"
  1833. ";;; with"
  1834. (cond
  1835. ((eq optimize 'source) " source-level optimization only")
  1836. ((eq optimize 'byte) " byte-level optimization only")
  1837. (optimize " all optimizations")
  1838. (t "out optimization"))
  1839. ".\n"
  1840. (if dynamic ";;; Function definitions are lazy-loaded.\n"
  1841. "")
  1842. "\n;;; This file uses "
  1843. (if dynamic-docstrings
  1844. "dynamic docstrings, first added in Emacs 19.29"
  1845. "opcodes that do not exist in Emacs 18")
  1846. ".\n\n"
  1847. ;; Note that byte-compile-fix-header may change this.
  1848. ";;; This file does not contain utf-8 non-ASCII characters,\n"
  1849. ";;; and so can be loaded in Emacs versions earlier than 23.\n\n"
  1850. ;; Insert semicolons as ballast, so that byte-compile-fix-header
  1851. ;; can delete them so as to keep the buffer positions
  1852. ;; constant for the actual compiled code.
  1853. ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n"
  1854. ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"))))
  1855. (defun byte-compile-output-file-form (form)
  1856. ;; writes the given form to the output buffer, being careful of docstrings
  1857. ;; in defun, defmacro, defvar, defvaralias, defconst, autoload and
  1858. ;; custom-declare-variable because make-docfile is so amazingly stupid.
  1859. ;; defalias calls are output directly by byte-compile-file-form-defmumble;
  1860. ;; it does not pay to first build the defalias in defmumble and then parse
  1861. ;; it here.
  1862. (if (and (memq (car-safe form) '(defun defmacro defvar defvaralias defconst
  1863. autoload custom-declare-variable))
  1864. (stringp (nth 3 form)))
  1865. (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
  1866. (memq (car form)
  1867. '(defvaralias autoload
  1868. custom-declare-variable)))
  1869. (let ((print-escape-newlines t)
  1870. (print-length nil)
  1871. (print-level nil)
  1872. (print-quoted t)
  1873. (print-gensym t)
  1874. (print-circle ; handle circular data structures
  1875. (not byte-compile-disable-print-circle)))
  1876. (princ "\n" byte-compile--outbuffer)
  1877. (prin1 form byte-compile--outbuffer)
  1878. nil)))
  1879. (defvar print-gensym-alist) ;Used before print-circle existed.
  1880. (defvar byte-compile--for-effect)
  1881. (defun byte-compile-output-docform (preface name info form specindex quoted)
  1882. "Print a form with a doc string. INFO is (prefix doc-index postfix).
  1883. If PREFACE and NAME are non-nil, print them too,
  1884. before INFO and the FORM but after the doc string itself.
  1885. If SPECINDEX is non-nil, it is the index in FORM
  1886. of the function bytecode string. In that case,
  1887. we output that argument and the following argument
  1888. \(the constants vector) together, for lazy loading.
  1889. QUOTED says that we have to put a quote before the
  1890. list that represents a doc string reference.
  1891. `defvaralias', `autoload' and `custom-declare-variable' need that."
  1892. ;; We need to examine byte-compile-dynamic-docstrings
  1893. ;; in the input buffer (now current), not in the output buffer.
  1894. (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
  1895. (with-current-buffer byte-compile--outbuffer
  1896. (let (position)
  1897. ;; Insert the doc string, and make it a comment with #@LENGTH.
  1898. (and (>= (nth 1 info) 0)
  1899. dynamic-docstrings
  1900. (progn
  1901. ;; Make the doc string start at beginning of line
  1902. ;; for make-docfile's sake.
  1903. (insert "\n")
  1904. (setq position
  1905. (byte-compile-output-as-comment
  1906. (nth (nth 1 info) form) nil))
  1907. (setq position (- (position-bytes position) (point-min) -1))
  1908. ;; If the doc string starts with * (a user variable),
  1909. ;; negate POSITION.
  1910. (if (and (stringp (nth (nth 1 info) form))
  1911. (> (length (nth (nth 1 info) form)) 0)
  1912. (eq (aref (nth (nth 1 info) form) 0) ?*))
  1913. (setq position (- position)))))
  1914. (if preface
  1915. (progn
  1916. (insert preface)
  1917. (prin1 name byte-compile--outbuffer)))
  1918. (insert (car info))
  1919. (let ((print-escape-newlines t)
  1920. (print-quoted t)
  1921. ;; For compatibility with code before print-circle,
  1922. ;; use a cons cell to say that we want
  1923. ;; print-gensym-alist not to be cleared
  1924. ;; between calls to print functions.
  1925. (print-gensym '(t))
  1926. (print-circle ; handle circular data structures
  1927. (not byte-compile-disable-print-circle))
  1928. print-gensym-alist ; was used before print-circle existed.
  1929. (print-continuous-numbering t)
  1930. print-number-table
  1931. (index 0))
  1932. (prin1 (car form) byte-compile--outbuffer)
  1933. (while (setq form (cdr form))
  1934. (setq index (1+ index))
  1935. (insert " ")
  1936. (cond ((and (numberp specindex) (= index specindex)
  1937. ;; Don't handle the definition dynamically
  1938. ;; if it refers (or might refer)
  1939. ;; to objects already output
  1940. ;; (for instance, gensyms in the arg list).
  1941. (let (non-nil)
  1942. (when (hash-table-p print-number-table)
  1943. (maphash (lambda (_k v) (if v (setq non-nil t)))
  1944. print-number-table))
  1945. (not non-nil)))
  1946. ;; Output the byte code and constants specially
  1947. ;; for lazy dynamic loading.
  1948. (let ((position
  1949. (byte-compile-output-as-comment
  1950. (cons (car form) (nth 1 form))
  1951. t)))
  1952. (setq position (- (position-bytes position)
  1953. (point-min) -1))
  1954. (princ (format "(#$ . %d) nil" position)
  1955. byte-compile--outbuffer)
  1956. (setq form (cdr form))
  1957. (setq index (1+ index))))
  1958. ((= index (nth 1 info))
  1959. (if position
  1960. (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
  1961. position)
  1962. byte-compile--outbuffer)
  1963. (let ((print-escape-newlines nil))
  1964. (goto-char (prog1 (1+ (point))
  1965. (prin1 (car form)
  1966. byte-compile--outbuffer)))
  1967. (insert "\\\n")
  1968. (goto-char (point-max)))))
  1969. (t
  1970. (prin1 (car form) byte-compile--outbuffer)))))
  1971. (insert (nth 2 info)))))
  1972. nil)
  1973. (defun byte-compile-keep-pending (form &optional handler)
  1974. (if (memq byte-optimize '(t source))
  1975. (setq form (byte-optimize-form form t)))
  1976. (if handler
  1977. (let ((byte-compile--for-effect t))
  1978. ;; To avoid consing up monstrously large forms at load time, we split
  1979. ;; the output regularly.
  1980. (and (memq (car-safe form) '(fset defalias))
  1981. (nthcdr 300 byte-compile-output)
  1982. (byte-compile-flush-pending))
  1983. (funcall handler form)
  1984. (if byte-compile--for-effect
  1985. (byte-compile-discard)))
  1986. (byte-compile-form form t))
  1987. nil)
  1988. (defun byte-compile-flush-pending ()
  1989. (if byte-compile-output
  1990. (let ((form (byte-compile-out-toplevel t 'file)))
  1991. (cond ((eq (car-safe form) 'progn)
  1992. (mapc 'byte-compile-output-file-form (cdr form)))
  1993. (form
  1994. (byte-compile-output-file-form form)))
  1995. (setq byte-compile-constants nil
  1996. byte-compile-variables nil
  1997. byte-compile-depth 0
  1998. byte-compile-maxdepth 0
  1999. byte-compile-output nil))))
  2000. (defun byte-compile-preprocess (form &optional _for-effect)
  2001. (setq form (macroexpand-all form byte-compile-macro-environment))
  2002. ;; FIXME: We should run byte-optimize-form here, but it currently does not
  2003. ;; recurse through all the code, so we'd have to fix this first.
  2004. ;; Maybe a good fix would be to merge byte-optimize-form into
  2005. ;; macroexpand-all.
  2006. ;; (if (memq byte-optimize '(t source))
  2007. ;; (setq form (byte-optimize-form form for-effect)))
  2008. (if lexical-binding
  2009. (cconv-closure-convert form)
  2010. form))
  2011. ;; byte-hunk-handlers cannot call this!
  2012. (defun byte-compile-toplevel-file-form (form)
  2013. (let ((byte-compile-current-form nil)) ; close over this for warnings.
  2014. (byte-compile-file-form (byte-compile-preprocess form t))))
  2015. ;; byte-hunk-handlers can call this.
  2016. (defun byte-compile-file-form (form)
  2017. (let (handler)
  2018. (cond ((and (consp form)
  2019. (symbolp (car form))
  2020. (setq handler (get (car form) 'byte-hunk-handler)))
  2021. (cond ((setq form (funcall handler form))
  2022. (byte-compile-flush-pending)
  2023. (byte-compile-output-file-form form))))
  2024. (t
  2025. (byte-compile-keep-pending form)))))
  2026. ;; Functions and variables with doc strings must be output separately,
  2027. ;; so make-docfile can recognize them. Most other things can be output
  2028. ;; as byte-code.
  2029. (put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
  2030. (defun byte-compile-file-form-autoload (form)
  2031. (and (let ((form form))
  2032. (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
  2033. (null form)) ;Constants only
  2034. (eval (nth 5 form)) ;Macro
  2035. (eval form)) ;Define the autoload.
  2036. ;; Avoid undefined function warnings for the autoload.
  2037. (when (and (consp (nth 1 form))
  2038. (eq (car (nth 1 form)) 'quote)
  2039. (consp (cdr (nth 1 form)))
  2040. (symbolp (nth 1 (nth 1 form))))
  2041. (push (cons (nth 1 (nth 1 form))
  2042. (cons 'autoload (cdr (cdr form))))
  2043. byte-compile-function-environment)
  2044. ;; If an autoload occurs _before_ the first call to a function,
  2045. ;; byte-compile-callargs-warn does not add an entry to
  2046. ;; byte-compile-unresolved-functions. Here we mimic the logic
  2047. ;; of byte-compile-callargs-warn so as not to warn if the
  2048. ;; autoload comes _after_ the function call.
  2049. ;; Alternatively, similar logic could go in
  2050. ;; byte-compile-warn-about-unresolved-functions.
  2051. (or (memq (nth 1 (nth 1 form)) byte-compile-noruntime-functions)
  2052. (setq byte-compile-unresolved-functions
  2053. (delq (assq (nth 1 (nth 1 form))
  2054. byte-compile-unresolved-functions)
  2055. byte-compile-unresolved-functions))))
  2056. (if (stringp (nth 3 form))
  2057. form
  2058. ;; No doc string, so we can compile this as a normal form.
  2059. (byte-compile-keep-pending form 'byte-compile-normal-call)))
  2060. (put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
  2061. (put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
  2062. (defun byte-compile-file-form-defvar (form)
  2063. (when (and (symbolp (nth 1 form))
  2064. (not (string-match "[-*/:$]" (symbol-name (nth 1 form))))
  2065. (byte-compile-warning-enabled-p 'lexical))
  2066. (byte-compile-warn "global/dynamic var `%s' lacks a prefix"
  2067. (nth 1 form)))
  2068. (push (nth 1 form) byte-compile-bound-variables)
  2069. (if (eq (car form) 'defconst)
  2070. (push (nth 1 form) byte-compile-const-variables))
  2071. (if (and (null (cddr form)) ;No `value' provided.
  2072. (eq (car form) 'defvar)) ;Just a declaration.
  2073. nil
  2074. (cond ((consp (nth 2 form))
  2075. (setq form (copy-sequence form))
  2076. (setcar (cdr (cdr form))
  2077. (byte-compile-top-level (nth 2 form) nil 'file))))
  2078. form))
  2079. (put 'define-abbrev-table 'byte-hunk-handler
  2080. 'byte-compile-file-form-define-abbrev-table)
  2081. (defun byte-compile-file-form-define-abbrev-table (form)
  2082. (if (eq 'quote (car-safe (car-safe (cdr form))))
  2083. (push (car-safe (cdr (cadr form))) byte-compile-bound-variables))
  2084. (byte-compile-keep-pending form))
  2085. (put 'custom-declare-variable 'byte-hunk-handler
  2086. 'byte-compile-file-form-custom-declare-variable)
  2087. (defun byte-compile-file-form-custom-declare-variable (form)
  2088. (when (byte-compile-warning-enabled-p 'callargs)
  2089. (byte-compile-nogroup-warn form))
  2090. (push (nth 1 (nth 1 form)) byte-compile-bound-variables)
  2091. ;; Don't compile the expression because it may be displayed to the user.
  2092. ;; (when (eq (car-safe (nth 2 form)) 'quote)
  2093. ;; ;; (nth 2 form) is meant to evaluate to an expression, so if we have the
  2094. ;; ;; final value already, we can byte-compile it.
  2095. ;; (setcar (cdr (nth 2 form))
  2096. ;; (byte-compile-top-level (cadr (nth 2 form)) nil 'file)))
  2097. (let ((tail (nthcdr 4 form)))
  2098. (while tail
  2099. (unless (keywordp (car tail)) ;No point optimizing keywords.
  2100. ;; Compile the keyword arguments.
  2101. (setcar tail (byte-compile-top-level (car tail) nil 'file)))
  2102. (setq tail (cdr tail))))
  2103. form)
  2104. (put 'require 'byte-hunk-handler 'byte-compile-file-form-require)
  2105. (defun byte-compile-file-form-require (form)
  2106. (let ((args (mapcar 'eval (cdr form)))
  2107. (hist-orig load-history)
  2108. hist-new)
  2109. (apply 'require args)
  2110. (when (byte-compile-warning-enabled-p 'cl-functions)
  2111. ;; Detect (require 'cl) in a way that works even if cl is already loaded.
  2112. (if (member (car args) '("cl" cl))
  2113. (progn
  2114. (byte-compile-warn "cl package required at runtime")
  2115. (byte-compile-disable-warning 'cl-functions))
  2116. ;; We may have required something that causes cl to be loaded, eg
  2117. ;; the uncompiled version of a file that requires cl when compiling.
  2118. (setq hist-new load-history)
  2119. (while (and (not byte-compile-cl-functions)
  2120. hist-new (not (eq hist-new hist-orig)))
  2121. (and (byte-compile-cl-file-p (car (pop hist-new)))
  2122. (byte-compile-find-cl-functions))))))
  2123. (byte-compile-keep-pending form 'byte-compile-normal-call))
  2124. (put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
  2125. (put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
  2126. (put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
  2127. (defun byte-compile-file-form-progn (form)
  2128. (mapc 'byte-compile-file-form (cdr form))
  2129. ;; Return nil so the forms are not output twice.
  2130. nil)
  2131. (put 'with-no-warnings 'byte-hunk-handler
  2132. 'byte-compile-file-form-with-no-warnings)
  2133. (defun byte-compile-file-form-with-no-warnings (form)
  2134. ;; cf byte-compile-file-form-progn.
  2135. (let (byte-compile-warnings)
  2136. (mapc 'byte-compile-file-form (cdr form))
  2137. nil))
  2138. ;; This handler is not necessary, but it makes the output from dont-compile
  2139. ;; and similar macros cleaner.
  2140. (put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
  2141. (defun byte-compile-file-form-eval (form)
  2142. (if (eq (car-safe (nth 1 form)) 'quote)
  2143. (nth 1 (nth 1 form))
  2144. (byte-compile-keep-pending form)))
  2145. (put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
  2146. (defun byte-compile-file-form-defun (form)
  2147. (byte-compile-file-form-defmumble form nil))
  2148. (put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
  2149. (defun byte-compile-file-form-defmacro (form)
  2150. (byte-compile-file-form-defmumble form t))
  2151. (defun byte-compile-defmacro-declaration (form)
  2152. "Generate code for declarations in macro definitions.
  2153. Remove declarations from the body of the macro definition
  2154. by side-effects."
  2155. (let ((tail (nthcdr 2 form))
  2156. (res '()))
  2157. (when (stringp (car (cdr tail)))
  2158. (setq tail (cdr tail)))
  2159. (while (and (consp (car (cdr tail)))
  2160. (eq (car (car (cdr tail))) 'declare))
  2161. (let ((declaration (car (cdr tail))))
  2162. (setcdr tail (cdr (cdr tail)))
  2163. (push `(if macro-declaration-function
  2164. (funcall macro-declaration-function
  2165. ',(car (cdr form)) ',declaration))
  2166. res)))
  2167. res))
  2168. (defun byte-compile-file-form-defmumble (form macrop)
  2169. (let* ((name (car (cdr form)))
  2170. (this-kind (if macrop 'byte-compile-macro-environment
  2171. 'byte-compile-function-environment))
  2172. (that-kind (if macrop 'byte-compile-function-environment
  2173. 'byte-compile-macro-environment))
  2174. (this-one (assq name (symbol-value this-kind)))
  2175. (that-one (assq name (symbol-value that-kind)))
  2176. (byte-compile-free-references nil)
  2177. (byte-compile-free-assignments nil))
  2178. (byte-compile-set-symbol-position name)
  2179. ;; When a function or macro is defined, add it to the call tree so that
  2180. ;; we can tell when functions are not used.
  2181. (if byte-compile-generate-call-tree
  2182. (or (assq name byte-compile-call-tree)
  2183. (setq byte-compile-call-tree
  2184. (cons (list name nil nil) byte-compile-call-tree))))
  2185. (setq byte-compile-current-form name) ; for warnings
  2186. (if (byte-compile-warning-enabled-p 'redefine)
  2187. (byte-compile-arglist-warn form macrop))
  2188. (if byte-compile-verbose
  2189. (message "Compiling %s... (%s)"
  2190. (or byte-compile-current-file "") (nth 1 form)))
  2191. (cond (that-one
  2192. (if (and (byte-compile-warning-enabled-p 'redefine)
  2193. ;; don't warn when compiling the stubs in byte-run...
  2194. (not (assq (nth 1 form)
  2195. byte-compile-initial-macro-environment)))
  2196. (byte-compile-warn
  2197. "`%s' defined multiple times, as both function and macro"
  2198. (nth 1 form)))
  2199. (setcdr that-one nil))
  2200. (this-one
  2201. (when (and (byte-compile-warning-enabled-p 'redefine)
  2202. ;; hack: don't warn when compiling the magic internal
  2203. ;; byte-compiler macros in byte-run.el...
  2204. (not (assq (nth 1 form)
  2205. byte-compile-initial-macro-environment)))
  2206. (byte-compile-warn "%s `%s' defined multiple times in this file"
  2207. (if macrop "macro" "function")
  2208. (nth 1 form))))
  2209. ((and (fboundp name)
  2210. (eq (car-safe (symbol-function name))
  2211. (if macrop 'lambda 'macro)))
  2212. (when (byte-compile-warning-enabled-p 'redefine)
  2213. (byte-compile-warn "%s `%s' being redefined as a %s"
  2214. (if macrop "function" "macro")
  2215. (nth 1 form)
  2216. (if macrop "macro" "function")))
  2217. ;; shadow existing definition
  2218. (set this-kind
  2219. (cons (cons name nil)
  2220. (symbol-value this-kind))))
  2221. )
  2222. (let ((body (nthcdr 3 form)))
  2223. (when (and (stringp (car body))
  2224. (symbolp (car-safe (cdr-safe body)))
  2225. (car-safe (cdr-safe body))
  2226. (stringp (car-safe (cdr-safe (cdr-safe body)))))
  2227. (byte-compile-set-symbol-position (nth 1 form))
  2228. (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
  2229. (nth 1 form))))
  2230. ;; Generate code for declarations in macro definitions.
  2231. ;; Remove declarations from the body of the macro definition.
  2232. (when macrop
  2233. (dolist (decl (byte-compile-defmacro-declaration form))
  2234. (prin1 decl byte-compile--outbuffer)))
  2235. (let* ((code (byte-compile-lambda (nthcdr 2 form) t)))
  2236. (if this-one
  2237. ;; A definition in b-c-initial-m-e should always take precedence
  2238. ;; during compilation, so don't let it be redefined. (Bug#8647)
  2239. (or (and macrop
  2240. (assq name byte-compile-initial-macro-environment))
  2241. (setcdr this-one code))
  2242. (set this-kind
  2243. (cons (cons name code)
  2244. (symbol-value this-kind))))
  2245. (byte-compile-flush-pending)
  2246. (if (not (stringp (nth 3 form)))
  2247. ;; No doc string. Provide -1 as the "doc string index"
  2248. ;; so that no element will be treated as a doc string.
  2249. (byte-compile-output-docform
  2250. "\n(defalias '"
  2251. name
  2252. (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]"))
  2253. (append code nil) ; Turn byte-code-function-p into list.
  2254. (and (atom code) byte-compile-dynamic
  2255. 1)
  2256. nil)
  2257. ;; Output the form by hand, that's much simpler than having
  2258. ;; b-c-output-file-form analyze the defalias.
  2259. (byte-compile-output-docform
  2260. "\n(defalias '"
  2261. name
  2262. (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]"))
  2263. (append code nil) ; Turn byte-code-function-p into list.
  2264. (and (atom code) byte-compile-dynamic
  2265. 1)
  2266. nil))
  2267. (princ ")" byte-compile--outbuffer)
  2268. nil)))
  2269. ;; Print Lisp object EXP in the output file, inside a comment,
  2270. ;; and return the file position it will have.
  2271. ;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
  2272. (defun byte-compile-output-as-comment (exp quoted)
  2273. (let ((position (point)))
  2274. (with-current-buffer byte-compile--outbuffer
  2275. ;; Insert EXP, and make it a comment with #@LENGTH.
  2276. (insert " ")
  2277. (if quoted
  2278. (prin1 exp byte-compile--outbuffer)
  2279. (princ exp byte-compile--outbuffer))
  2280. (goto-char position)
  2281. ;; Quote certain special characters as needed.
  2282. ;; get_doc_string in doc.c does the unquoting.
  2283. (while (search-forward "\^A" nil t)
  2284. (replace-match "\^A\^A" t t))
  2285. (goto-char position)
  2286. (while (search-forward "\000" nil t)
  2287. (replace-match "\^A0" t t))
  2288. (goto-char position)
  2289. (while (search-forward "\037" nil t)
  2290. (replace-match "\^A_" t t))
  2291. (goto-char (point-max))
  2292. (insert "\037")
  2293. (goto-char position)
  2294. (insert "#@" (format "%d" (- (position-bytes (point-max))
  2295. (position-bytes position))))
  2296. ;; Save the file position of the object.
  2297. ;; Note we should add 1 to skip the space
  2298. ;; that we inserted before the actual doc string,
  2299. ;; and subtract 1 to convert from an 1-origin Emacs position
  2300. ;; to a file position; they cancel.
  2301. (setq position (point))
  2302. (goto-char (point-max)))
  2303. position))
  2304. ;;;###autoload
  2305. (defun byte-compile (form)
  2306. "If FORM is a symbol, byte-compile its function definition.
  2307. If FORM is a lambda or a macro, byte-compile it as a function."
  2308. (displaying-byte-compile-warnings
  2309. (byte-compile-close-variables
  2310. (let* ((fun (if (symbolp form)
  2311. (and (fboundp form) (symbol-function form))
  2312. form))
  2313. (macro (eq (car-safe fun) 'macro)))
  2314. (if macro
  2315. (setq fun (cdr fun)))
  2316. (cond ((eq (car-safe fun) 'lambda)
  2317. ;; Expand macros.
  2318. (setq fun (byte-compile-preprocess fun))
  2319. ;; Get rid of the `function' quote added by the `lambda' macro.
  2320. (if (eq (car-safe fun) 'function) (setq fun (cadr fun)))
  2321. (setq fun (if macro
  2322. (cons 'macro (byte-compile-lambda fun))
  2323. (byte-compile-lambda fun)))
  2324. (if (symbolp form)
  2325. (defalias form fun)
  2326. fun)))))))
  2327. (defun byte-compile-sexp (sexp)
  2328. "Compile and return SEXP."
  2329. (displaying-byte-compile-warnings
  2330. (byte-compile-close-variables
  2331. (byte-compile-top-level (byte-compile-preprocess sexp)))))
  2332. (defun byte-compile-check-lambda-list (list)
  2333. "Check lambda-list LIST for errors."
  2334. (let (vars)
  2335. (while list
  2336. (let ((arg (car list)))
  2337. (when (symbolp arg)
  2338. (byte-compile-set-symbol-position arg))
  2339. (cond ((or (not (symbolp arg))
  2340. (byte-compile-const-symbol-p arg t))
  2341. (error "Invalid lambda variable %s" arg))
  2342. ((eq arg '&rest)
  2343. (unless (cdr list)
  2344. (error "&rest without variable name"))
  2345. (when (cddr list)
  2346. (error "Garbage following &rest VAR in lambda-list")))
  2347. ((eq arg '&optional)
  2348. (unless (cdr list)
  2349. (error "Variable name missing after &optional")))
  2350. ((memq arg vars)
  2351. (byte-compile-warn "repeated variable %s in lambda-list" arg))
  2352. (t
  2353. (push arg vars))))
  2354. (setq list (cdr list)))))
  2355. (defun byte-compile-arglist-vars (arglist)
  2356. "Return a list of the variables in the lambda argument list ARGLIST."
  2357. (remq '&rest (remq '&optional arglist)))
  2358. (defun byte-compile-make-lambda-lexenv (form)
  2359. "Return a new lexical environment for a lambda expression FORM."
  2360. ;; See if this is a closure or not
  2361. (let ((args (byte-compile-arglist-vars (cadr form))))
  2362. (let ((lexenv nil))
  2363. ;; Fill in the initial stack contents
  2364. (let ((stackpos 0))
  2365. ;; Add entries for each argument
  2366. (dolist (arg args)
  2367. (push (cons arg stackpos) lexenv)
  2368. (setq stackpos (1+ stackpos)))
  2369. ;; Return the new lexical environment
  2370. lexenv))))
  2371. (defun byte-compile-make-args-desc (arglist)
  2372. (let ((mandatory 0)
  2373. nonrest (rest 0))
  2374. (while (and arglist (not (memq (car arglist) '(&optional &rest))))
  2375. (setq mandatory (1+ mandatory))
  2376. (setq arglist (cdr arglist)))
  2377. (setq nonrest mandatory)
  2378. (when (eq (car arglist) '&optional)
  2379. (setq arglist (cdr arglist))
  2380. (while (and arglist (not (eq (car arglist) '&rest)))
  2381. (setq nonrest (1+ nonrest))
  2382. (setq arglist (cdr arglist))))
  2383. (when arglist
  2384. (setq rest 1))
  2385. (if (> mandatory 127)
  2386. (byte-compile-report-error "Too many (>127) mandatory arguments")
  2387. (logior mandatory
  2388. (lsh nonrest 8)
  2389. (lsh rest 7)))))
  2390. ;; Byte-compile a lambda-expression and return a valid function.
  2391. ;; The value is usually a compiled function but may be the original
  2392. ;; lambda-expression.
  2393. ;; When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
  2394. ;; of the list FUN and `byte-compile-set-symbol-position' is not called.
  2395. ;; Use this feature to avoid calling `byte-compile-set-symbol-position'
  2396. ;; for symbols generated by the byte compiler itself.
  2397. (defun byte-compile-lambda (fun &optional add-lambda reserved-csts)
  2398. (if add-lambda
  2399. (setq fun (cons 'lambda fun))
  2400. (unless (eq 'lambda (car-safe fun))
  2401. (error "Not a lambda list: %S" fun))
  2402. (byte-compile-set-symbol-position 'lambda))
  2403. (byte-compile-check-lambda-list (nth 1 fun))
  2404. (let* ((arglist (nth 1 fun))
  2405. (byte-compile-bound-variables
  2406. (append (and (not lexical-binding)
  2407. (byte-compile-arglist-vars arglist))
  2408. byte-compile-bound-variables))
  2409. (body (cdr (cdr fun)))
  2410. (doc (if (stringp (car body))
  2411. (prog1 (car body)
  2412. ;; Discard the doc string
  2413. ;; unless it is the last element of the body.
  2414. (if (cdr body)
  2415. (setq body (cdr body))))))
  2416. (int (assq 'interactive body)))
  2417. ;; Process the interactive spec.
  2418. (when int
  2419. (byte-compile-set-symbol-position 'interactive)
  2420. ;; Skip (interactive) if it is in front (the most usual location).
  2421. (if (eq int (car body))
  2422. (setq body (cdr body)))
  2423. (cond ((consp (cdr int))
  2424. (if (cdr (cdr int))
  2425. (byte-compile-warn "malformed interactive spec: %s"
  2426. (prin1-to-string int)))
  2427. ;; If the interactive spec is a call to `list', don't
  2428. ;; compile it, because `call-interactively' looks at the
  2429. ;; args of `list'. Actually, compile it to get warnings,
  2430. ;; but don't use the result.
  2431. (let* ((form (nth 1 int))
  2432. (newform (byte-compile-top-level form)))
  2433. (while (memq (car-safe form) '(let let* progn save-excursion))
  2434. (while (consp (cdr form))
  2435. (setq form (cdr form)))
  2436. (setq form (car form)))
  2437. (if (and (eq (car-safe form) 'list)
  2438. ;; The spec is evalled in callint.c in dynamic-scoping
  2439. ;; mode, so just leaving the form unchanged would mean
  2440. ;; it won't be eval'd in the right mode.
  2441. (not lexical-binding))
  2442. nil
  2443. (setq int `(interactive ,newform)))))
  2444. ((cdr int)
  2445. (byte-compile-warn "malformed interactive spec: %s"
  2446. (prin1-to-string int)))))
  2447. ;; Process the body.
  2448. (let ((compiled
  2449. (byte-compile-top-level (cons 'progn body) nil 'lambda
  2450. ;; If doing lexical binding, push a new
  2451. ;; lexical environment containing just the
  2452. ;; args (since lambda expressions should be
  2453. ;; closed by now).
  2454. (and lexical-binding
  2455. (byte-compile-make-lambda-lexenv fun))
  2456. reserved-csts)))
  2457. ;; Build the actual byte-coded function.
  2458. (if (eq 'byte-code (car-safe compiled))
  2459. (apply 'make-byte-code
  2460. (if lexical-binding
  2461. (byte-compile-make-args-desc arglist)
  2462. arglist)
  2463. (append
  2464. ;; byte-string, constants-vector, stack depth
  2465. (cdr compiled)
  2466. ;; optionally, the doc string.
  2467. (cond (lexical-binding
  2468. (require 'help-fns)
  2469. (list (help-add-fundoc-usage doc arglist)))
  2470. ((or doc int)
  2471. (list doc)))
  2472. ;; optionally, the interactive spec.
  2473. (if int
  2474. (list (nth 1 int)))))
  2475. (error "byte-compile-top-level did not return byte-code")))))
  2476. (defvar byte-compile-reserved-constants 0)
  2477. (defun byte-compile-constants-vector ()
  2478. ;; Builds the constants-vector from the current variables and constants.
  2479. ;; This modifies the constants from (const . nil) to (const . offset).
  2480. ;; To keep the byte-codes to look up the vector as short as possible:
  2481. ;; First 6 elements are vars, as there are one-byte varref codes for those.
  2482. ;; Next up to byte-constant-limit are constants, still with one-byte codes.
  2483. ;; Next variables again, to get 2-byte codes for variable lookup.
  2484. ;; The rest of the constants and variables need 3-byte byte-codes.
  2485. (let* ((i (1- byte-compile-reserved-constants))
  2486. (rest (nreverse byte-compile-variables)) ; nreverse because the first
  2487. (other (nreverse byte-compile-constants)) ; vars often are used most.
  2488. ret tmp
  2489. (limits '(5 ; Use the 1-byte varref codes,
  2490. 63 ; 1-constlim ; 1-byte byte-constant codes,
  2491. 255 ; 2-byte varref codes,
  2492. 65535 ; 3-byte codes for the rest.
  2493. 65535)) ; twice since we step when we swap.
  2494. limit)
  2495. (while (or rest other)
  2496. (setq limit (car limits))
  2497. (while (and rest (< i limit))
  2498. (cond
  2499. ((numberp (car rest))
  2500. (assert (< (car rest) byte-compile-reserved-constants)))
  2501. ((setq tmp (assq (car (car rest)) ret))
  2502. (setcdr (car rest) (cdr tmp)))
  2503. (t
  2504. (setcdr (car rest) (setq i (1+ i)))
  2505. (setq ret (cons (car rest) ret))))
  2506. (setq rest (cdr rest)))
  2507. (setq limits (cdr limits) ;Step
  2508. rest (prog1 other ;&Swap.
  2509. (setq other rest))))
  2510. (apply 'vector (nreverse (mapcar 'car ret)))))
  2511. ;; Given an expression FORM, compile it and return an equivalent byte-code
  2512. ;; expression (a call to the function byte-code).
  2513. (defun byte-compile-top-level (form &optional for-effect output-type
  2514. lexenv reserved-csts)
  2515. ;; OUTPUT-TYPE advises about how form is expected to be used:
  2516. ;; 'eval or nil -> a single form,
  2517. ;; 'progn or t -> a list of forms,
  2518. ;; 'lambda -> body of a lambda,
  2519. ;; 'file -> used at file-level.
  2520. (let ((byte-compile--for-effect for-effect)
  2521. (byte-compile-constants nil)
  2522. (byte-compile-variables nil)
  2523. (byte-compile-tag-number 0)
  2524. (byte-compile-depth 0)
  2525. (byte-compile-maxdepth 0)
  2526. (byte-compile--lexical-environment lexenv)
  2527. (byte-compile-reserved-constants (or reserved-csts 0))
  2528. (byte-compile-output nil))
  2529. (if (memq byte-optimize '(t source))
  2530. (setq form (byte-optimize-form form byte-compile--for-effect)))
  2531. (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
  2532. (setq form (nth 1 form)))
  2533. ;; Set up things for a lexically-bound function.
  2534. (when (and lexical-binding (eq output-type 'lambda))
  2535. ;; See how many arguments there are, and set the current stack depth
  2536. ;; accordingly.
  2537. (setq byte-compile-depth (length byte-compile--lexical-environment))
  2538. ;; If there are args, output a tag to record the initial
  2539. ;; stack-depth for the optimizer.
  2540. (when (> byte-compile-depth 0)
  2541. (byte-compile-out-tag (byte-compile-make-tag))))
  2542. ;; Now compile FORM
  2543. (byte-compile-form form byte-compile--for-effect)
  2544. (byte-compile-out-toplevel byte-compile--for-effect output-type)))
  2545. (defun byte-compile-out-toplevel (&optional for-effect output-type)
  2546. (if for-effect
  2547. ;; The stack is empty. Push a value to be returned from (byte-code ..).
  2548. (if (eq (car (car byte-compile-output)) 'byte-discard)
  2549. (setq byte-compile-output (cdr byte-compile-output))
  2550. (byte-compile-push-constant
  2551. ;; Push any constant - preferably one which already is used, and
  2552. ;; a number or symbol - ie not some big sequence. The return value
  2553. ;; isn't returned, but it would be a shame if some textually large
  2554. ;; constant was not optimized away because we chose to return it.
  2555. (and (not (assq nil byte-compile-constants)) ; Nil is often there.
  2556. (let ((tmp (reverse byte-compile-constants)))
  2557. (while (and tmp (not (or (symbolp (caar tmp))
  2558. (numberp (caar tmp)))))
  2559. (setq tmp (cdr tmp)))
  2560. (caar tmp))))))
  2561. (byte-compile-out 'byte-return 0)
  2562. (setq byte-compile-output (nreverse byte-compile-output))
  2563. (if (memq byte-optimize '(t byte))
  2564. (setq byte-compile-output
  2565. (byte-optimize-lapcode byte-compile-output)))
  2566. ;; Decompile trivial functions:
  2567. ;; only constants and variables, or a single funcall except in lambdas.
  2568. ;; Except for Lisp_Compiled objects, forms like (foo "hi")
  2569. ;; are still quicker than (byte-code "..." [foo "hi"] 2).
  2570. ;; Note that even (quote foo) must be parsed just as any subr by the
  2571. ;; interpreter, so quote should be compiled into byte-code in some contexts.
  2572. ;; What to leave uncompiled:
  2573. ;; lambda -> never. we used to leave it uncompiled if the body was
  2574. ;; a single atom, but that causes confusion if the docstring
  2575. ;; uses the (file . pos) syntax. Besides, now that we have
  2576. ;; the Lisp_Compiled type, the compiled form is faster.
  2577. ;; eval -> atom, quote or (function atom atom atom)
  2578. ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
  2579. ;; file -> as progn, but takes both quotes and atoms, and longer forms.
  2580. (let (rest
  2581. (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
  2582. tmp body)
  2583. (cond
  2584. ;; #### This should be split out into byte-compile-nontrivial-function-p.
  2585. ((or (eq output-type 'lambda)
  2586. (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
  2587. (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
  2588. (not (setq tmp (assq 'byte-return byte-compile-output)))
  2589. (progn
  2590. (setq rest (nreverse
  2591. (cdr (memq tmp (reverse byte-compile-output)))))
  2592. (while
  2593. (cond
  2594. ((memq (car (car rest)) '(byte-varref byte-constant))
  2595. (setq tmp (car (cdr (car rest))))
  2596. (if (if (eq (car (car rest)) 'byte-constant)
  2597. (or (consp tmp)
  2598. (and (symbolp tmp)
  2599. (not (byte-compile-const-symbol-p tmp)))))
  2600. (if maycall
  2601. (setq body (cons (list 'quote tmp) body)))
  2602. (setq body (cons tmp body))))
  2603. ((and maycall
  2604. ;; Allow a funcall if at most one atom follows it.
  2605. (null (nthcdr 3 rest))
  2606. (setq tmp (get (car (car rest)) 'byte-opcode-invert))
  2607. (or (null (cdr rest))
  2608. (and (memq output-type '(file progn t))
  2609. (cdr (cdr rest))
  2610. (eq (car (nth 1 rest)) 'byte-discard)
  2611. (progn (setq rest (cdr rest)) t))))
  2612. (setq maycall nil) ; Only allow one real function call.
  2613. (setq body (nreverse body))
  2614. (setq body (list
  2615. (if (and (eq tmp 'funcall)
  2616. (eq (car-safe (car body)) 'quote))
  2617. (cons (nth 1 (car body)) (cdr body))
  2618. (cons tmp body))))
  2619. (or (eq output-type 'file)
  2620. (not (delq nil (mapcar 'consp (cdr (car body))))))))
  2621. (setq rest (cdr rest)))
  2622. rest))
  2623. (let ((byte-compile-vector (byte-compile-constants-vector)))
  2624. (list 'byte-code (byte-compile-lapcode byte-compile-output)
  2625. byte-compile-vector byte-compile-maxdepth)))
  2626. ;; it's a trivial function
  2627. ((cdr body) (cons 'progn (nreverse body)))
  2628. ((car body)))))
  2629. ;; Given BODY, compile it and return a new body.
  2630. (defun byte-compile-top-level-body (body &optional for-effect)
  2631. (setq body
  2632. (byte-compile-top-level (cons 'progn body) for-effect t))
  2633. (cond ((eq (car-safe body) 'progn)
  2634. (cdr body))
  2635. (body
  2636. (list body))))
  2637. ;; Special macro-expander used during byte-compilation.
  2638. (defun byte-compile-macroexpand-declare-function (fn file &rest args)
  2639. (push (cons fn
  2640. (if (and (consp args) (listp (car args)))
  2641. (list 'declared (car args))
  2642. t)) ; arglist not specified
  2643. byte-compile-function-environment)
  2644. ;; We are stating that it _will_ be defined at runtime.
  2645. (setq byte-compile-noruntime-functions
  2646. (delq fn byte-compile-noruntime-functions))
  2647. ;; Delegate the rest to the normal macro definition.
  2648. (macroexpand `(declare-function ,fn ,file ,@args)))
  2649. ;; This is the recursive entry point for compiling each subform of an
  2650. ;; expression.
  2651. ;; If for-effect is non-nil, byte-compile-form will output a byte-discard
  2652. ;; before terminating (ie no value will be left on the stack).
  2653. ;; A byte-compile handler may, when byte-compile--for-effect is non-nil, choose
  2654. ;; output code which does not leave a value on the stack, and then set
  2655. ;; byte-compile--for-effect to nil (to prevent byte-compile-form from
  2656. ;; outputting the byte-discard).
  2657. ;; If a handler wants to call another handler, it should do so via
  2658. ;; byte-compile-form, or take extreme care to handle byte-compile--for-effect
  2659. ;; correctly. (Use byte-compile-form-do-effect to reset the
  2660. ;; byte-compile--for-effect flag too.)
  2661. ;;
  2662. (defun byte-compile-form (form &optional for-effect)
  2663. (let ((byte-compile--for-effect for-effect))
  2664. (cond
  2665. ((not (consp form))
  2666. (cond ((or (not (symbolp form)) (byte-compile-const-symbol-p form))
  2667. (when (symbolp form)
  2668. (byte-compile-set-symbol-position form))
  2669. (byte-compile-constant form))
  2670. ((and byte-compile--for-effect byte-compile-delete-errors)
  2671. (when (symbolp form)
  2672. (byte-compile-set-symbol-position form))
  2673. (setq byte-compile--for-effect nil))
  2674. (t
  2675. (byte-compile-variable-ref form))))
  2676. ((symbolp (car form))
  2677. (let* ((fn (car form))
  2678. (handler (get fn 'byte-compile)))
  2679. (when (byte-compile-const-symbol-p fn)
  2680. (byte-compile-warn "`%s' called as a function" fn))
  2681. (and (byte-compile-warning-enabled-p 'interactive-only)
  2682. (memq fn byte-compile-interactive-only-functions)
  2683. (byte-compile-warn "`%s' used from Lisp code\n\
  2684. That command is designed for interactive use only" fn))
  2685. (if (and (fboundp (car form))
  2686. (eq (car-safe (symbol-function (car form))) 'macro))
  2687. (byte-compile-log-warning
  2688. (format "Forgot to expand macro %s" (car form)) nil :error))
  2689. (if (and handler
  2690. ;; Make sure that function exists. This is important
  2691. ;; for CL compiler macros since the symbol may be
  2692. ;; `cl-byte-compile-compiler-macro' but if CL isn't
  2693. ;; loaded, this function doesn't exist.
  2694. (and (not (eq handler
  2695. ;; Already handled by macroexpand-all.
  2696. 'cl-byte-compile-compiler-macro))
  2697. (functionp handler)))
  2698. (funcall handler form)
  2699. (byte-compile-normal-call form))
  2700. (if (byte-compile-warning-enabled-p 'cl-functions)
  2701. (byte-compile-cl-warn form))))
  2702. ((and (byte-code-function-p (car form))
  2703. (memq byte-optimize '(t lap)))
  2704. (byte-compile-unfold-bcf form))
  2705. ((and (eq (car-safe (car form)) 'lambda)
  2706. ;; if the form comes out the same way it went in, that's
  2707. ;; because it was malformed, and we couldn't unfold it.
  2708. (not (eq form (setq form (byte-compile-unfold-lambda form)))))
  2709. (byte-compile-form form byte-compile--for-effect)
  2710. (setq byte-compile--for-effect nil))
  2711. ((byte-compile-normal-call form)))
  2712. (if byte-compile--for-effect
  2713. (byte-compile-discard))))
  2714. (defun byte-compile-normal-call (form)
  2715. (when (and (byte-compile-warning-enabled-p 'callargs)
  2716. (symbolp (car form)))
  2717. (if (memq (car form)
  2718. '(custom-declare-group custom-declare-variable
  2719. custom-declare-face))
  2720. (byte-compile-nogroup-warn form))
  2721. (when (get (car form) 'byte-obsolete-info)
  2722. (byte-compile-warn-obsolete (car form)))
  2723. (byte-compile-callargs-warn form))
  2724. (if byte-compile-generate-call-tree
  2725. (byte-compile-annotate-call-tree form))
  2726. (when (and byte-compile--for-effect (eq (car form) 'mapcar)
  2727. (byte-compile-warning-enabled-p 'mapcar))
  2728. (byte-compile-set-symbol-position 'mapcar)
  2729. (byte-compile-warn
  2730. "`mapcar' called for effect; use `mapc' or `dolist' instead"))
  2731. (byte-compile-push-constant (car form))
  2732. (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster.
  2733. (byte-compile-out 'byte-call (length (cdr form))))
  2734. ;; Splice the given lap code into the current instruction stream.
  2735. ;; If it has any labels in it, you're responsible for making sure there
  2736. ;; are no collisions, and that byte-compile-tag-number is reasonable
  2737. ;; after this is spliced in. The provided list is destroyed.
  2738. (defun byte-compile-inline-lapcode (lap end-depth)
  2739. ;; "Replay" the operations: we used to just do
  2740. ;; (setq byte-compile-output (nconc (nreverse lap) byte-compile-output))
  2741. ;; but that fails to update byte-compile-depth, so we had to assume
  2742. ;; that `lap' ends up adding exactly 1 element to the stack. This
  2743. ;; happens to be true for byte-code generated by bytecomp.el without
  2744. ;; lexical-binding, but it's not true in general, and it's not true for
  2745. ;; code output by bytecomp.el with lexical-binding.
  2746. (let ((endtag (byte-compile-make-tag)))
  2747. (dolist (op lap)
  2748. (cond
  2749. ((eq (car op) 'TAG) (byte-compile-out-tag op))
  2750. ((memq (car op) byte-goto-ops) (byte-compile-goto (car op) (cdr op)))
  2751. ((eq (car op) 'byte-return)
  2752. (byte-compile-discard (- byte-compile-depth end-depth) t)
  2753. (byte-compile-goto 'byte-goto endtag))
  2754. (t (byte-compile-out (car op) (cdr op)))))
  2755. (byte-compile-out-tag endtag)))
  2756. (defun byte-compile-unfold-bcf (form)
  2757. "Inline call to byte-code-functions."
  2758. (let* ((byte-compile-bound-variables byte-compile-bound-variables)
  2759. (fun (car form))
  2760. (fargs (aref fun 0))
  2761. (start-depth byte-compile-depth)
  2762. (fmax2 (if (numberp fargs) (lsh fargs -7))) ;2*max+rest.
  2763. ;; (fmin (if (numberp fargs) (logand fargs 127)))
  2764. (alen (length (cdr form)))
  2765. (dynbinds ()))
  2766. (fetch-bytecode fun)
  2767. (mapc 'byte-compile-form (cdr form))
  2768. (unless fmax2
  2769. ;; Old-style byte-code.
  2770. (assert (listp fargs))
  2771. (while fargs
  2772. (case (car fargs)
  2773. (&optional (setq fargs (cdr fargs)))
  2774. (&rest (setq fmax2 (+ (* 2 (length dynbinds)) 1))
  2775. (push (cadr fargs) dynbinds)
  2776. (setq fargs nil))
  2777. (t (push (pop fargs) dynbinds))))
  2778. (unless fmax2 (setq fmax2 (* 2 (length dynbinds)))))
  2779. (cond
  2780. ((<= (+ alen alen) fmax2)
  2781. ;; Add missing &optional (or &rest) arguments.
  2782. (dotimes (_ (- (/ (1+ fmax2) 2) alen))
  2783. (byte-compile-push-constant nil)))
  2784. ((zerop (logand fmax2 1))
  2785. (byte-compile-log-warning "Too many arguments for inlined function"
  2786. nil :error)
  2787. (byte-compile-discard (- alen (/ fmax2 2))))
  2788. (t
  2789. ;; Turn &rest args into a list.
  2790. (let ((n (- alen (/ (1- fmax2) 2))))
  2791. (assert (> n 0) nil "problem: fmax2=%S alen=%S n=%S" fmax2 alen n)
  2792. (if (< n 5)
  2793. (byte-compile-out
  2794. (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- n))
  2795. 0)
  2796. (byte-compile-out 'byte-listN n)))))
  2797. (mapc #'byte-compile-dynamic-variable-bind dynbinds)
  2798. (byte-compile-inline-lapcode
  2799. (byte-decompile-bytecode-1 (aref fun 1) (aref fun 2) t)
  2800. (1+ start-depth))
  2801. ;; Unbind dynamic variables.
  2802. (when dynbinds
  2803. (byte-compile-out 'byte-unbind (length dynbinds)))
  2804. (assert (eq byte-compile-depth (1+ start-depth))
  2805. nil "Wrong depth start=%s end=%s" start-depth byte-compile-depth)))
  2806. (defun byte-compile-check-variable (var access-type)
  2807. "Do various error checks before a use of the variable VAR."
  2808. (when (symbolp var)
  2809. (byte-compile-set-symbol-position var))
  2810. (cond ((or (not (symbolp var)) (byte-compile-const-symbol-p var))
  2811. (when (byte-compile-warning-enabled-p 'constants)
  2812. (byte-compile-warn (if (eq access-type 'let-bind)
  2813. "attempt to let-bind %s `%s`"
  2814. "variable reference to %s `%s'")
  2815. (if (symbolp var) "constant" "nonvariable")
  2816. (prin1-to-string var))))
  2817. ((let ((od (get var 'byte-obsolete-variable)))
  2818. (and od
  2819. (not (memq var byte-compile-not-obsolete-vars))
  2820. (not (memq var byte-compile-global-not-obsolete-vars))
  2821. (or (case (nth 1 od)
  2822. (set (not (eq access-type 'reference)))
  2823. (get (eq access-type 'reference))
  2824. (t t)))))
  2825. (byte-compile-warn-obsolete var))))
  2826. (defsubst byte-compile-dynamic-variable-op (base-op var)
  2827. (let ((tmp (assq var byte-compile-variables)))
  2828. (unless tmp
  2829. (setq tmp (list var))
  2830. (push tmp byte-compile-variables))
  2831. (byte-compile-out base-op tmp)))
  2832. (defun byte-compile-dynamic-variable-bind (var)
  2833. "Generate code to bind the lexical variable VAR to the top-of-stack value."
  2834. (byte-compile-check-variable var 'let-bind)
  2835. (push var byte-compile-bound-variables)
  2836. (byte-compile-dynamic-variable-op 'byte-varbind var))
  2837. (defun byte-compile-variable-ref (var)
  2838. "Generate code to push the value of the variable VAR on the stack."
  2839. (byte-compile-check-variable var 'reference)
  2840. (let ((lex-binding (assq var byte-compile--lexical-environment)))
  2841. (if lex-binding
  2842. ;; VAR is lexically bound
  2843. (byte-compile-stack-ref (cdr lex-binding))
  2844. ;; VAR is dynamically bound
  2845. (unless (or (not (byte-compile-warning-enabled-p 'free-vars))
  2846. (boundp var)
  2847. (memq var byte-compile-bound-variables)
  2848. (memq var byte-compile-free-references))
  2849. (byte-compile-warn "reference to free variable `%S'" var)
  2850. (push var byte-compile-free-references))
  2851. (byte-compile-dynamic-variable-op 'byte-varref var))))
  2852. (defun byte-compile-variable-set (var)
  2853. "Generate code to set the variable VAR from the top-of-stack value."
  2854. (byte-compile-check-variable var 'assign)
  2855. (let ((lex-binding (assq var byte-compile--lexical-environment)))
  2856. (if lex-binding
  2857. ;; VAR is lexically bound
  2858. (byte-compile-stack-set (cdr lex-binding))
  2859. ;; VAR is dynamically bound
  2860. (unless (or (not (byte-compile-warning-enabled-p 'free-vars))
  2861. (boundp var)
  2862. (memq var byte-compile-bound-variables)
  2863. (memq var byte-compile-free-assignments))
  2864. (byte-compile-warn "assignment to free variable `%s'" var)
  2865. (push var byte-compile-free-assignments))
  2866. (byte-compile-dynamic-variable-op 'byte-varset var))))
  2867. (defmacro byte-compile-get-constant (const)
  2868. `(or (if (stringp ,const)
  2869. ;; In a string constant, treat properties as significant.
  2870. (let (result)
  2871. (dolist (elt byte-compile-constants)
  2872. (if (equal-including-properties (car elt) ,const)
  2873. (setq result elt)))
  2874. result)
  2875. (assq ,const byte-compile-constants))
  2876. (car (setq byte-compile-constants
  2877. (cons (list ,const) byte-compile-constants)))))
  2878. ;; Use this when the value of a form is a constant.
  2879. ;; This obeys byte-compile--for-effect.
  2880. (defun byte-compile-constant (const)
  2881. (if byte-compile--for-effect
  2882. (setq byte-compile--for-effect nil)
  2883. (when (symbolp const)
  2884. (byte-compile-set-symbol-position const))
  2885. (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
  2886. ;; Use this for a constant that is not the value of its containing form.
  2887. ;; This ignores byte-compile--for-effect.
  2888. (defun byte-compile-push-constant (const)
  2889. (let ((byte-compile--for-effect nil))
  2890. (inline (byte-compile-constant const))))
  2891. ;; Compile those primitive ordinary functions
  2892. ;; which have special byte codes just for speed.
  2893. (defmacro byte-defop-compiler (function &optional compile-handler)
  2894. "Add a compiler-form for FUNCTION.
  2895. If function is a symbol, then the variable \"byte-SYMBOL\" must name
  2896. the opcode to be used. If function is a list, the first element
  2897. is the function and the second element is the bytecode-symbol.
  2898. The second element may be nil, meaning there is no opcode.
  2899. COMPILE-HANDLER is the function to use to compile this byte-op, or
  2900. may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
  2901. If it is nil, then the handler is \"byte-compile-SYMBOL.\""
  2902. (let (opcode)
  2903. (if (symbolp function)
  2904. (setq opcode (intern (concat "byte-" (symbol-name function))))
  2905. (setq opcode (car (cdr function))
  2906. function (car function)))
  2907. (let ((fnform
  2908. (list 'put (list 'quote function) ''byte-compile
  2909. (list 'quote
  2910. (or (cdr (assq compile-handler
  2911. '((0 . byte-compile-no-args)
  2912. (1 . byte-compile-one-arg)
  2913. (2 . byte-compile-two-args)
  2914. (3 . byte-compile-three-args)
  2915. (0-1 . byte-compile-zero-or-one-arg)
  2916. (1-2 . byte-compile-one-or-two-args)
  2917. (2-3 . byte-compile-two-or-three-args)
  2918. )))
  2919. compile-handler
  2920. (intern (concat "byte-compile-"
  2921. (symbol-name function))))))))
  2922. (if opcode
  2923. (list 'progn fnform
  2924. (list 'put (list 'quote function)
  2925. ''byte-opcode (list 'quote opcode))
  2926. (list 'put (list 'quote opcode)
  2927. ''byte-opcode-invert (list 'quote function)))
  2928. fnform))))
  2929. (defmacro byte-defop-compiler-1 (function &optional compile-handler)
  2930. (list 'byte-defop-compiler (list function nil) compile-handler))
  2931. (put 'byte-call 'byte-opcode-invert 'funcall)
  2932. (put 'byte-list1 'byte-opcode-invert 'list)
  2933. (put 'byte-list2 'byte-opcode-invert 'list)
  2934. (put 'byte-list3 'byte-opcode-invert 'list)
  2935. (put 'byte-list4 'byte-opcode-invert 'list)
  2936. (put 'byte-listN 'byte-opcode-invert 'list)
  2937. (put 'byte-concat2 'byte-opcode-invert 'concat)
  2938. (put 'byte-concat3 'byte-opcode-invert 'concat)
  2939. (put 'byte-concat4 'byte-opcode-invert 'concat)
  2940. (put 'byte-concatN 'byte-opcode-invert 'concat)
  2941. (put 'byte-insertN 'byte-opcode-invert 'insert)
  2942. (byte-defop-compiler point 0)
  2943. ;;(byte-defop-compiler mark 0) ;; obsolete
  2944. (byte-defop-compiler point-max 0)
  2945. (byte-defop-compiler point-min 0)
  2946. (byte-defop-compiler following-char 0)
  2947. (byte-defop-compiler preceding-char 0)
  2948. (byte-defop-compiler current-column 0)
  2949. (byte-defop-compiler eolp 0)
  2950. (byte-defop-compiler eobp 0)
  2951. (byte-defop-compiler bolp 0)
  2952. (byte-defop-compiler bobp 0)
  2953. (byte-defop-compiler current-buffer 0)
  2954. ;;(byte-defop-compiler read-char 0) ;; obsolete
  2955. ;; (byte-defop-compiler interactive-p 0) ;; Obsolete.
  2956. (byte-defop-compiler widen 0)
  2957. (byte-defop-compiler end-of-line 0-1)
  2958. (byte-defop-compiler forward-char 0-1)
  2959. (byte-defop-compiler forward-line 0-1)
  2960. (byte-defop-compiler symbolp 1)
  2961. (byte-defop-compiler consp 1)
  2962. (byte-defop-compiler stringp 1)
  2963. (byte-defop-compiler listp 1)
  2964. (byte-defop-compiler not 1)
  2965. (byte-defop-compiler (null byte-not) 1)
  2966. (byte-defop-compiler car 1)
  2967. (byte-defop-compiler cdr 1)
  2968. (byte-defop-compiler length 1)
  2969. (byte-defop-compiler symbol-value 1)
  2970. (byte-defop-compiler symbol-function 1)
  2971. (byte-defop-compiler (1+ byte-add1) 1)
  2972. (byte-defop-compiler (1- byte-sub1) 1)
  2973. (byte-defop-compiler goto-char 1)
  2974. (byte-defop-compiler char-after 0-1)
  2975. (byte-defop-compiler set-buffer 1)
  2976. ;;(byte-defop-compiler set-mark 1) ;; obsolete
  2977. (byte-defop-compiler forward-word 0-1)
  2978. (byte-defop-compiler char-syntax 1)
  2979. (byte-defop-compiler nreverse 1)
  2980. (byte-defop-compiler car-safe 1)
  2981. (byte-defop-compiler cdr-safe 1)
  2982. (byte-defop-compiler numberp 1)
  2983. (byte-defop-compiler integerp 1)
  2984. (byte-defop-compiler skip-chars-forward 1-2)
  2985. (byte-defop-compiler skip-chars-backward 1-2)
  2986. (byte-defop-compiler eq 2)
  2987. (byte-defop-compiler memq 2)
  2988. (byte-defop-compiler cons 2)
  2989. (byte-defop-compiler aref 2)
  2990. (byte-defop-compiler set 2)
  2991. (byte-defop-compiler (= byte-eqlsign) 2)
  2992. (byte-defop-compiler (< byte-lss) 2)
  2993. (byte-defop-compiler (> byte-gtr) 2)
  2994. (byte-defop-compiler (<= byte-leq) 2)
  2995. (byte-defop-compiler (>= byte-geq) 2)
  2996. (byte-defop-compiler get 2)
  2997. (byte-defop-compiler nth 2)
  2998. (byte-defop-compiler substring 2-3)
  2999. (byte-defop-compiler (move-marker byte-set-marker) 2-3)
  3000. (byte-defop-compiler set-marker 2-3)
  3001. (byte-defop-compiler match-beginning 1)
  3002. (byte-defop-compiler match-end 1)
  3003. (byte-defop-compiler upcase 1)
  3004. (byte-defop-compiler downcase 1)
  3005. (byte-defop-compiler string= 2)
  3006. (byte-defop-compiler string< 2)
  3007. (byte-defop-compiler (string-equal byte-string=) 2)
  3008. (byte-defop-compiler (string-lessp byte-string<) 2)
  3009. (byte-defop-compiler equal 2)
  3010. (byte-defop-compiler nthcdr 2)
  3011. (byte-defop-compiler elt 2)
  3012. (byte-defop-compiler member 2)
  3013. (byte-defop-compiler assq 2)
  3014. (byte-defop-compiler (rplaca byte-setcar) 2)
  3015. (byte-defop-compiler (rplacd byte-setcdr) 2)
  3016. (byte-defop-compiler setcar 2)
  3017. (byte-defop-compiler setcdr 2)
  3018. (byte-defop-compiler buffer-substring 2)
  3019. (byte-defop-compiler delete-region 2)
  3020. (byte-defop-compiler narrow-to-region 2)
  3021. (byte-defop-compiler (% byte-rem) 2)
  3022. (byte-defop-compiler aset 3)
  3023. (byte-defop-compiler max byte-compile-associative)
  3024. (byte-defop-compiler min byte-compile-associative)
  3025. (byte-defop-compiler (+ byte-plus) byte-compile-associative)
  3026. (byte-defop-compiler (* byte-mult) byte-compile-associative)
  3027. ;;####(byte-defop-compiler move-to-column 1)
  3028. (byte-defop-compiler-1 interactive byte-compile-noop)
  3029. (defun byte-compile-subr-wrong-args (form n)
  3030. (byte-compile-set-symbol-position (car form))
  3031. (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
  3032. (car form) (length (cdr form))
  3033. (if (= 1 (length (cdr form))) "" "s") n)
  3034. ;; Get run-time wrong-number-of-args error.
  3035. (byte-compile-normal-call form))
  3036. (defun byte-compile-no-args (form)
  3037. (if (not (= (length form) 1))
  3038. (byte-compile-subr-wrong-args form "none")
  3039. (byte-compile-out (get (car form) 'byte-opcode) 0)))
  3040. (defun byte-compile-one-arg (form)
  3041. (if (not (= (length form) 2))
  3042. (byte-compile-subr-wrong-args form 1)
  3043. (byte-compile-form (car (cdr form))) ;; Push the argument
  3044. (byte-compile-out (get (car form) 'byte-opcode) 0)))
  3045. (defun byte-compile-two-args (form)
  3046. (if (not (= (length form) 3))
  3047. (byte-compile-subr-wrong-args form 2)
  3048. (byte-compile-form (car (cdr form))) ;; Push the arguments
  3049. (byte-compile-form (nth 2 form))
  3050. (byte-compile-out (get (car form) 'byte-opcode) 0)))
  3051. (defun byte-compile-three-args (form)
  3052. (if (not (= (length form) 4))
  3053. (byte-compile-subr-wrong-args form 3)
  3054. (byte-compile-form (car (cdr form))) ;; Push the arguments
  3055. (byte-compile-form (nth 2 form))
  3056. (byte-compile-form (nth 3 form))
  3057. (byte-compile-out (get (car form) 'byte-opcode) 0)))
  3058. (defun byte-compile-zero-or-one-arg (form)
  3059. (let ((len (length form)))
  3060. (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
  3061. ((= len 2) (byte-compile-one-arg form))
  3062. (t (byte-compile-subr-wrong-args form "0-1")))))
  3063. (defun byte-compile-one-or-two-args (form)
  3064. (let ((len (length form)))
  3065. (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
  3066. ((= len 3) (byte-compile-two-args form))
  3067. (t (byte-compile-subr-wrong-args form "1-2")))))
  3068. (defun byte-compile-two-or-three-args (form)
  3069. (let ((len (length form)))
  3070. (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
  3071. ((= len 4) (byte-compile-three-args form))
  3072. (t (byte-compile-subr-wrong-args form "2-3")))))
  3073. (defun byte-compile-noop (_form)
  3074. (byte-compile-constant nil))
  3075. (defun byte-compile-discard (&optional num preserve-tos)
  3076. "Output byte codes to discard the NUM entries at the top of the stack.
  3077. NUM defaults to 1.
  3078. If PRESERVE-TOS is non-nil, preserve the top-of-stack value, as if it were
  3079. popped before discarding the num values, and then pushed back again after
  3080. discarding."
  3081. (if (and (null num) (not preserve-tos))
  3082. ;; common case
  3083. (byte-compile-out 'byte-discard)
  3084. ;; general case
  3085. (unless num
  3086. (setq num 1))
  3087. (when (and preserve-tos (> num 0))
  3088. ;; Preserve the top-of-stack value by writing it directly to the stack
  3089. ;; location which will be at the top-of-stack after popping.
  3090. (byte-compile-stack-set (1- (- byte-compile-depth num)))
  3091. ;; Now we actually discard one less value, since we want to keep
  3092. ;; the eventual TOS
  3093. (setq num (1- num)))
  3094. (while (> num 0)
  3095. (byte-compile-out 'byte-discard)
  3096. (setq num (1- num)))))
  3097. (defun byte-compile-stack-ref (stack-pos)
  3098. "Output byte codes to push the value at stack position STACK-POS."
  3099. (let ((dist (- byte-compile-depth (1+ stack-pos))))
  3100. (if (zerop dist)
  3101. ;; A simple optimization
  3102. (byte-compile-out 'byte-dup)
  3103. ;; normal case
  3104. (byte-compile-out 'byte-stack-ref dist))))
  3105. (defun byte-compile-stack-set (stack-pos)
  3106. "Output byte codes to store the TOS value at stack position STACK-POS."
  3107. (byte-compile-out 'byte-stack-set (- byte-compile-depth (1+ stack-pos))))
  3108. (byte-defop-compiler-1 internal-make-closure byte-compile-make-closure)
  3109. (byte-defop-compiler-1 internal-get-closed-var byte-compile-get-closed-var)
  3110. (defun byte-compile-make-closure (form)
  3111. "Byte-compile the special `internal-make-closure' form."
  3112. (if byte-compile--for-effect (setq byte-compile--for-effect nil)
  3113. (let* ((vars (nth 1 form))
  3114. (env (nth 2 form))
  3115. (body (nthcdr 3 form))
  3116. (fun
  3117. (byte-compile-lambda `(lambda ,vars . ,body) nil (length env))))
  3118. (assert (byte-code-function-p fun))
  3119. (byte-compile-form `(make-byte-code
  3120. ',(aref fun 0) ',(aref fun 1)
  3121. (vconcat (vector . ,env) ',(aref fun 2))
  3122. ,@(nthcdr 3 (mapcar (lambda (x) `',x) fun)))))))
  3123. (defun byte-compile-get-closed-var (form)
  3124. "Byte-compile the special `internal-get-closed-var' form."
  3125. (if byte-compile--for-effect (setq byte-compile--for-effect nil)
  3126. (byte-compile-out 'byte-constant (nth 1 form))))
  3127. ;; Compile a function that accepts one or more args and is right-associative.
  3128. ;; We do it by left-associativity so that the operations
  3129. ;; are done in the same order as in interpreted code.
  3130. ;; We treat the one-arg case, as in (+ x), like (+ x 0).
  3131. ;; in order to convert markers to numbers, and trigger expected errors.
  3132. (defun byte-compile-associative (form)
  3133. (if (cdr form)
  3134. (let ((opcode (get (car form) 'byte-opcode))
  3135. args)
  3136. (if (and (< 3 (length form))
  3137. (memq opcode (list (get '+ 'byte-opcode)
  3138. (get '* 'byte-opcode))))
  3139. ;; Don't use binary operations for > 2 operands, as that
  3140. ;; may cause overflow/truncation in float operations.
  3141. (byte-compile-normal-call form)
  3142. (setq args (copy-sequence (cdr form)))
  3143. (byte-compile-form (car args))
  3144. (setq args (cdr args))
  3145. (or args (setq args '(0)
  3146. opcode (get '+ 'byte-opcode)))
  3147. (dolist (arg args)
  3148. (byte-compile-form arg)
  3149. (byte-compile-out opcode 0))))
  3150. (byte-compile-constant (eval form))))
  3151. ;; more complicated compiler macros
  3152. (byte-defop-compiler char-before)
  3153. (byte-defop-compiler backward-char)
  3154. (byte-defop-compiler backward-word)
  3155. (byte-defop-compiler list)
  3156. (byte-defop-compiler concat)
  3157. (byte-defop-compiler fset)
  3158. (byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
  3159. (byte-defop-compiler indent-to)
  3160. (byte-defop-compiler insert)
  3161. (byte-defop-compiler-1 function byte-compile-function-form)
  3162. (byte-defop-compiler-1 - byte-compile-minus)
  3163. (byte-defop-compiler (/ byte-quo) byte-compile-quo)
  3164. (byte-defop-compiler nconc)
  3165. (defun byte-compile-char-before (form)
  3166. (cond ((= 2 (length form))
  3167. (byte-compile-form (list 'char-after (if (numberp (nth 1 form))
  3168. (1- (nth 1 form))
  3169. `(1- ,(nth 1 form))))))
  3170. ((= 1 (length form))
  3171. (byte-compile-form '(char-after (1- (point)))))
  3172. (t (byte-compile-subr-wrong-args form "0-1"))))
  3173. ;; backward-... ==> forward-... with negated argument.
  3174. (defun byte-compile-backward-char (form)
  3175. (cond ((= 2 (length form))
  3176. (byte-compile-form (list 'forward-char (if (numberp (nth 1 form))
  3177. (- (nth 1 form))
  3178. `(- ,(nth 1 form))))))
  3179. ((= 1 (length form))
  3180. (byte-compile-form '(forward-char -1)))
  3181. (t (byte-compile-subr-wrong-args form "0-1"))))
  3182. (defun byte-compile-backward-word (form)
  3183. (cond ((= 2 (length form))
  3184. (byte-compile-form (list 'forward-word (if (numberp (nth 1 form))
  3185. (- (nth 1 form))
  3186. `(- ,(nth 1 form))))))
  3187. ((= 1 (length form))
  3188. (byte-compile-form '(forward-word -1)))
  3189. (t (byte-compile-subr-wrong-args form "0-1"))))
  3190. (defun byte-compile-list (form)
  3191. (let ((count (length (cdr form))))
  3192. (cond ((= count 0)
  3193. (byte-compile-constant nil))
  3194. ((< count 5)
  3195. (mapc 'byte-compile-form (cdr form))
  3196. (byte-compile-out
  3197. (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
  3198. ((< count 256)
  3199. (mapc 'byte-compile-form (cdr form))
  3200. (byte-compile-out 'byte-listN count))
  3201. (t (byte-compile-normal-call form)))))
  3202. (defun byte-compile-concat (form)
  3203. (let ((count (length (cdr form))))
  3204. (cond ((and (< 1 count) (< count 5))
  3205. (mapc 'byte-compile-form (cdr form))
  3206. (byte-compile-out
  3207. (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
  3208. 0))
  3209. ;; Concat of one arg is not a no-op if arg is not a string.
  3210. ((= count 0)
  3211. (byte-compile-form ""))
  3212. ((< count 256)
  3213. (mapc 'byte-compile-form (cdr form))
  3214. (byte-compile-out 'byte-concatN count))
  3215. ((byte-compile-normal-call form)))))
  3216. (defun byte-compile-minus (form)
  3217. (let ((len (length form)))
  3218. (cond
  3219. ((= 1 len) (byte-compile-constant 0))
  3220. ((= 2 len)
  3221. (byte-compile-form (cadr form))
  3222. (byte-compile-out 'byte-negate 0))
  3223. ((= 3 len)
  3224. (byte-compile-form (nth 1 form))
  3225. (byte-compile-form (nth 2 form))
  3226. (byte-compile-out 'byte-diff 0))
  3227. ;; Don't use binary operations for > 2 operands, as that may
  3228. ;; cause overflow/truncation in float operations.
  3229. (t (byte-compile-normal-call form)))))
  3230. (defun byte-compile-quo (form)
  3231. (let ((len (length form)))
  3232. (cond ((<= len 2)
  3233. (byte-compile-subr-wrong-args form "2 or more"))
  3234. ((= len 3)
  3235. (byte-compile-two-args form))
  3236. (t
  3237. ;; Don't use binary operations for > 2 operands, as that
  3238. ;; may cause overflow/truncation in float operations.
  3239. (byte-compile-normal-call form)))))
  3240. (defun byte-compile-nconc (form)
  3241. (let ((len (length form)))
  3242. (cond ((= len 1)
  3243. (byte-compile-constant nil))
  3244. ((= len 2)
  3245. ;; nconc of one arg is a noop, even if that arg isn't a list.
  3246. (byte-compile-form (nth 1 form)))
  3247. (t
  3248. (byte-compile-form (car (setq form (cdr form))))
  3249. (while (setq form (cdr form))
  3250. (byte-compile-form (car form))
  3251. (byte-compile-out 'byte-nconc 0))))))
  3252. (defun byte-compile-fset (form)
  3253. ;; warn about forms like (fset 'foo '(lambda () ...))
  3254. ;; (where the lambda expression is non-trivial...)
  3255. (let ((fn (nth 2 form))
  3256. body)
  3257. (if (and (eq (car-safe fn) 'quote)
  3258. (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
  3259. (progn
  3260. (setq body (cdr (cdr fn)))
  3261. (if (stringp (car body)) (setq body (cdr body)))
  3262. (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
  3263. (if (and (consp (car body))
  3264. (not (eq 'byte-code (car (car body)))))
  3265. (byte-compile-warn
  3266. "A quoted lambda form is the second argument of `fset'. This is probably
  3267. not what you want, as that lambda cannot be compiled. Consider using
  3268. the syntax #'(lambda (...) ...) instead.")))))
  3269. (byte-compile-two-args form))
  3270. ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
  3271. ;; Otherwise it will be incompatible with the interpreter,
  3272. ;; and (funcall (function foo)) will lose with autoloads.
  3273. (defun byte-compile-function-form (form)
  3274. (byte-compile-constant (if (eq 'lambda (car-safe (nth 1 form)))
  3275. (byte-compile-lambda (nth 1 form))
  3276. (nth 1 form))))
  3277. (defun byte-compile-indent-to (form)
  3278. (let ((len (length form)))
  3279. (cond ((= len 2)
  3280. (byte-compile-form (car (cdr form)))
  3281. (byte-compile-out 'byte-indent-to 0))
  3282. ((= len 3)
  3283. ;; no opcode for 2-arg case.
  3284. (byte-compile-normal-call form))
  3285. (t
  3286. (byte-compile-subr-wrong-args form "1-2")))))
  3287. (defun byte-compile-insert (form)
  3288. (cond ((null (cdr form))
  3289. (byte-compile-constant nil))
  3290. ((<= (length form) 256)
  3291. (mapc 'byte-compile-form (cdr form))
  3292. (if (cdr (cdr form))
  3293. (byte-compile-out 'byte-insertN (length (cdr form)))
  3294. (byte-compile-out 'byte-insert 0)))
  3295. ((memq t (mapcar 'consp (cdr (cdr form))))
  3296. (byte-compile-normal-call form))
  3297. ;; We can split it; there is no function call after inserting 1st arg.
  3298. (t
  3299. (while (setq form (cdr form))
  3300. (byte-compile-form (car form))
  3301. (byte-compile-out 'byte-insert 0)
  3302. (if (cdr form)
  3303. (byte-compile-discard))))))
  3304. (byte-defop-compiler-1 setq)
  3305. (byte-defop-compiler-1 setq-default)
  3306. (byte-defop-compiler-1 quote)
  3307. (defun byte-compile-setq (form)
  3308. (let ((args (cdr form)))
  3309. (if args
  3310. (while args
  3311. (byte-compile-form (car (cdr args)))
  3312. (or byte-compile--for-effect (cdr (cdr args))
  3313. (byte-compile-out 'byte-dup 0))
  3314. (byte-compile-variable-set (car args))
  3315. (setq args (cdr (cdr args))))
  3316. ;; (setq), with no arguments.
  3317. (byte-compile-form nil byte-compile--for-effect))
  3318. (setq byte-compile--for-effect nil)))
  3319. (defun byte-compile-setq-default (form)
  3320. (setq form (cdr form))
  3321. (if (> (length form) 2)
  3322. (let ((setters ()))
  3323. (while (consp form)
  3324. (push `(setq-default ,(pop form) ,(pop form)) setters))
  3325. (byte-compile-form (cons 'progn (nreverse setters))))
  3326. (let ((var (car form)))
  3327. (and (or (not (symbolp var))
  3328. (byte-compile-const-symbol-p var t))
  3329. (byte-compile-warning-enabled-p 'constants)
  3330. (byte-compile-warn
  3331. "variable assignment to %s `%s'"
  3332. (if (symbolp var) "constant" "nonvariable")
  3333. (prin1-to-string var)))
  3334. (byte-compile-normal-call `(set-default ',var ,@(cdr form))))))
  3335. (byte-defop-compiler-1 set-default)
  3336. (defun byte-compile-set-default (form)
  3337. (let ((varexp (car-safe (cdr-safe form))))
  3338. (if (eq (car-safe varexp) 'quote)
  3339. ;; If the varexp is constant, compile it as a setq-default
  3340. ;; so we get more warnings.
  3341. (byte-compile-setq-default `(setq-default ,(car-safe (cdr varexp))
  3342. ,@(cddr form)))
  3343. (byte-compile-normal-call form))))
  3344. (defun byte-compile-quote (form)
  3345. (byte-compile-constant (car (cdr form))))
  3346. ;;; control structures
  3347. (defun byte-compile-body (body &optional for-effect)
  3348. (while (cdr body)
  3349. (byte-compile-form (car body) t)
  3350. (setq body (cdr body)))
  3351. (byte-compile-form (car body) for-effect))
  3352. (defsubst byte-compile-body-do-effect (body)
  3353. (byte-compile-body body byte-compile--for-effect)
  3354. (setq byte-compile--for-effect nil))
  3355. (defsubst byte-compile-form-do-effect (form)
  3356. (byte-compile-form form byte-compile--for-effect)
  3357. (setq byte-compile--for-effect nil))
  3358. (byte-defop-compiler-1 inline byte-compile-progn)
  3359. (byte-defop-compiler-1 progn)
  3360. (byte-defop-compiler-1 prog1)
  3361. (byte-defop-compiler-1 prog2)
  3362. (byte-defop-compiler-1 if)
  3363. (byte-defop-compiler-1 cond)
  3364. (byte-defop-compiler-1 and)
  3365. (byte-defop-compiler-1 or)
  3366. (byte-defop-compiler-1 while)
  3367. (byte-defop-compiler-1 funcall)
  3368. (byte-defop-compiler-1 let)
  3369. (byte-defop-compiler-1 let* byte-compile-let)
  3370. (defun byte-compile-progn (form)
  3371. (byte-compile-body-do-effect (cdr form)))
  3372. (defun byte-compile-prog1 (form)
  3373. (byte-compile-form-do-effect (car (cdr form)))
  3374. (byte-compile-body (cdr (cdr form)) t))
  3375. (defun byte-compile-prog2 (form)
  3376. (byte-compile-form (nth 1 form) t)
  3377. (byte-compile-form-do-effect (nth 2 form))
  3378. (byte-compile-body (cdr (cdr (cdr form))) t))
  3379. (defmacro byte-compile-goto-if (cond discard tag)
  3380. `(byte-compile-goto
  3381. (if ,cond
  3382. (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
  3383. (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
  3384. ,tag))
  3385. ;; Return the list of items in CONDITION-PARAM that match PRED-LIST.
  3386. ;; Only return items that are not in ONLY-IF-NOT-PRESENT.
  3387. (defun byte-compile-find-bound-condition (condition-param
  3388. pred-list
  3389. &optional only-if-not-present)
  3390. (let ((result nil)
  3391. (nth-one nil)
  3392. (cond-list
  3393. (if (memq (car-safe condition-param) pred-list)
  3394. ;; The condition appears by itself.
  3395. (list condition-param)
  3396. ;; If the condition is an `and', look for matches among the
  3397. ;; `and' arguments.
  3398. (when (eq 'and (car-safe condition-param))
  3399. (cdr condition-param)))))
  3400. (dolist (crt cond-list)
  3401. (when (and (memq (car-safe crt) pred-list)
  3402. (eq 'quote (car-safe (setq nth-one (nth 1 crt))))
  3403. ;; Ignore if the symbol is already on the unresolved
  3404. ;; list.
  3405. (not (assq (nth 1 nth-one) ; the relevant symbol
  3406. only-if-not-present)))
  3407. (push (nth 1 (nth 1 crt)) result)))
  3408. result))
  3409. (defmacro byte-compile-maybe-guarded (condition &rest body)
  3410. "Execute forms in BODY, potentially guarded by CONDITION.
  3411. CONDITION is a variable whose value is a test in an `if' or `cond'.
  3412. BODY is the code to compile in the first arm of the if or the body of
  3413. the cond clause. If CONDITION's value is of the form (fboundp 'foo)
  3414. or (boundp 'foo), the relevant warnings from BODY about foo's
  3415. being undefined (or obsolete) will be suppressed.
  3416. If CONDITION's value is (not (featurep 'emacs)) or (featurep 'xemacs),
  3417. that suppresses all warnings during execution of BODY."
  3418. (declare (indent 1) (debug t))
  3419. `(let* ((fbound-list (byte-compile-find-bound-condition
  3420. ,condition (list 'fboundp)
  3421. byte-compile-unresolved-functions))
  3422. (bound-list (byte-compile-find-bound-condition
  3423. ,condition (list 'boundp 'default-boundp)))
  3424. ;; Maybe add to the bound list.
  3425. (byte-compile-bound-variables
  3426. (append bound-list byte-compile-bound-variables)))
  3427. (unwind-protect
  3428. ;; If things not being bound at all is ok, so must them being
  3429. ;; obsolete. Note that we add to the existing lists since Tramp
  3430. ;; (ab)uses this feature.
  3431. (let ((byte-compile-not-obsolete-vars
  3432. (append byte-compile-not-obsolete-vars bound-list))
  3433. (byte-compile-not-obsolete-funcs
  3434. (append byte-compile-not-obsolete-funcs fbound-list)))
  3435. ,@body)
  3436. ;; Maybe remove the function symbol from the unresolved list.
  3437. (dolist (fbound fbound-list)
  3438. (when fbound
  3439. (setq byte-compile-unresolved-functions
  3440. (delq (assq fbound byte-compile-unresolved-functions)
  3441. byte-compile-unresolved-functions)))))))
  3442. (defun byte-compile-if (form)
  3443. (byte-compile-form (car (cdr form)))
  3444. ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
  3445. ;; and avoid warnings about the relevant symbols in the consequent.
  3446. (let ((clause (nth 1 form))
  3447. (donetag (byte-compile-make-tag)))
  3448. (if (null (nthcdr 3 form))
  3449. ;; No else-forms
  3450. (progn
  3451. (byte-compile-goto-if nil byte-compile--for-effect donetag)
  3452. (byte-compile-maybe-guarded clause
  3453. (byte-compile-form (nth 2 form) byte-compile--for-effect))
  3454. (byte-compile-out-tag donetag))
  3455. (let ((elsetag (byte-compile-make-tag)))
  3456. (byte-compile-goto 'byte-goto-if-nil elsetag)
  3457. (byte-compile-maybe-guarded clause
  3458. (byte-compile-form (nth 2 form) byte-compile--for-effect))
  3459. (byte-compile-goto 'byte-goto donetag)
  3460. (byte-compile-out-tag elsetag)
  3461. (byte-compile-maybe-guarded (list 'not clause)
  3462. (byte-compile-body (cdr (cdr (cdr form))) byte-compile--for-effect))
  3463. (byte-compile-out-tag donetag))))
  3464. (setq byte-compile--for-effect nil))
  3465. (defun byte-compile-cond (clauses)
  3466. (let ((donetag (byte-compile-make-tag))
  3467. nexttag clause)
  3468. (while (setq clauses (cdr clauses))
  3469. (setq clause (car clauses))
  3470. (cond ((or (eq (car clause) t)
  3471. (and (eq (car-safe (car clause)) 'quote)
  3472. (car-safe (cdr-safe (car clause)))))
  3473. ;; Unconditional clause
  3474. (setq clause (cons t clause)
  3475. clauses nil))
  3476. ((cdr clauses)
  3477. (byte-compile-form (car clause))
  3478. (if (null (cdr clause))
  3479. ;; First clause is a singleton.
  3480. (byte-compile-goto-if t byte-compile--for-effect donetag)
  3481. (setq nexttag (byte-compile-make-tag))
  3482. (byte-compile-goto 'byte-goto-if-nil nexttag)
  3483. (byte-compile-maybe-guarded (car clause)
  3484. (byte-compile-body (cdr clause) byte-compile--for-effect))
  3485. (byte-compile-goto 'byte-goto donetag)
  3486. (byte-compile-out-tag nexttag)))))
  3487. ;; Last clause
  3488. (let ((guard (car clause)))
  3489. (and (cdr clause) (not (eq guard t))
  3490. (progn (byte-compile-form guard)
  3491. (byte-compile-goto-if nil byte-compile--for-effect donetag)
  3492. (setq clause (cdr clause))))
  3493. (byte-compile-maybe-guarded guard
  3494. (byte-compile-body-do-effect clause)))
  3495. (byte-compile-out-tag donetag)))
  3496. (defun byte-compile-and (form)
  3497. (let ((failtag (byte-compile-make-tag))
  3498. (args (cdr form)))
  3499. (if (null args)
  3500. (byte-compile-form-do-effect t)
  3501. (byte-compile-and-recursion args failtag))))
  3502. ;; Handle compilation of a nontrivial `and' call.
  3503. ;; We use tail recursion so we can use byte-compile-maybe-guarded.
  3504. (defun byte-compile-and-recursion (rest failtag)
  3505. (if (cdr rest)
  3506. (progn
  3507. (byte-compile-form (car rest))
  3508. (byte-compile-goto-if nil byte-compile--for-effect failtag)
  3509. (byte-compile-maybe-guarded (car rest)
  3510. (byte-compile-and-recursion (cdr rest) failtag)))
  3511. (byte-compile-form-do-effect (car rest))
  3512. (byte-compile-out-tag failtag)))
  3513. (defun byte-compile-or (form)
  3514. (let ((wintag (byte-compile-make-tag))
  3515. (args (cdr form)))
  3516. (if (null args)
  3517. (byte-compile-form-do-effect nil)
  3518. (byte-compile-or-recursion args wintag))))
  3519. ;; Handle compilation of a nontrivial `or' call.
  3520. ;; We use tail recursion so we can use byte-compile-maybe-guarded.
  3521. (defun byte-compile-or-recursion (rest wintag)
  3522. (if (cdr rest)
  3523. (progn
  3524. (byte-compile-form (car rest))
  3525. (byte-compile-goto-if t byte-compile--for-effect wintag)
  3526. (byte-compile-maybe-guarded (list 'not (car rest))
  3527. (byte-compile-or-recursion (cdr rest) wintag)))
  3528. (byte-compile-form-do-effect (car rest))
  3529. (byte-compile-out-tag wintag)))
  3530. (defun byte-compile-while (form)
  3531. (let ((endtag (byte-compile-make-tag))
  3532. (looptag (byte-compile-make-tag)))
  3533. (byte-compile-out-tag looptag)
  3534. (byte-compile-form (car (cdr form)))
  3535. (byte-compile-goto-if nil byte-compile--for-effect endtag)
  3536. (byte-compile-body (cdr (cdr form)) t)
  3537. (byte-compile-goto 'byte-goto looptag)
  3538. (byte-compile-out-tag endtag)
  3539. (setq byte-compile--for-effect nil)))
  3540. (defun byte-compile-funcall (form)
  3541. (mapc 'byte-compile-form (cdr form))
  3542. (byte-compile-out 'byte-call (length (cdr (cdr form)))))
  3543. ;; let binding
  3544. (defun byte-compile-push-binding-init (clause)
  3545. "Emit byte-codes to push the initialization value for CLAUSE on the stack.
  3546. Return the offset in the form (VAR . OFFSET)."
  3547. (let* ((var (if (consp clause) (car clause) clause)))
  3548. ;; We record the stack position even of dynamic bindings and
  3549. ;; variables in non-stack lexical environments; we'll put
  3550. ;; them in the proper place below.
  3551. (prog1 (cons var byte-compile-depth)
  3552. (if (consp clause)
  3553. (byte-compile-form (cadr clause))
  3554. (byte-compile-push-constant nil)))))
  3555. (defun byte-compile-not-lexical-var-p (var)
  3556. (or (not (symbolp var))
  3557. (special-variable-p var)
  3558. (memq var byte-compile-bound-variables)
  3559. (memq var '(nil t))
  3560. (keywordp var)))
  3561. (defun byte-compile-bind (var init-lexenv)
  3562. "Emit byte-codes to bind VAR and update `byte-compile--lexical-environment'.
  3563. INIT-LEXENV should be a lexical-environment alist describing the
  3564. positions of the init value that have been pushed on the stack.
  3565. Return non-nil if the TOS value was popped."
  3566. ;; The presence of lexical bindings mean that we may have to
  3567. ;; juggle things on the stack, to move them to TOS for
  3568. ;; dynamic binding.
  3569. (cond ((not (byte-compile-not-lexical-var-p var))
  3570. ;; VAR is a simple stack-allocated lexical variable
  3571. (push (assq var init-lexenv)
  3572. byte-compile--lexical-environment)
  3573. nil)
  3574. ((eq var (caar init-lexenv))
  3575. ;; VAR is dynamic and is on the top of the
  3576. ;; stack, so we can just bind it like usual
  3577. (byte-compile-dynamic-variable-bind var)
  3578. t)
  3579. (t
  3580. ;; VAR is dynamic, but we have to get its
  3581. ;; value out of the middle of the stack
  3582. (let ((stack-pos (cdr (assq var init-lexenv))))
  3583. (byte-compile-stack-ref stack-pos)
  3584. (byte-compile-dynamic-variable-bind var)
  3585. ;; Now we have to store nil into its temporary
  3586. ;; stack position to avoid problems with GC
  3587. (byte-compile-push-constant nil)
  3588. (byte-compile-stack-set stack-pos))
  3589. nil)))
  3590. (defun byte-compile-unbind (clauses init-lexenv
  3591. &optional preserve-body-value)
  3592. "Emit byte-codes to unbind the variables bound by CLAUSES.
  3593. CLAUSES is a `let'-style variable binding list. INIT-LEXENV should be a
  3594. lexical-environment alist describing the positions of the init value that
  3595. have been pushed on the stack. If PRESERVE-BODY-VALUE is true,
  3596. then an additional value on the top of the stack, above any lexical binding
  3597. slots, is preserved, so it will be on the top of the stack after all
  3598. binding slots have been popped."
  3599. ;; Unbind dynamic variables
  3600. (let ((num-dynamic-bindings 0))
  3601. (dolist (clause clauses)
  3602. (unless (assq (if (consp clause) (car clause) clause)
  3603. byte-compile--lexical-environment)
  3604. (setq num-dynamic-bindings (1+ num-dynamic-bindings))))
  3605. (unless (zerop num-dynamic-bindings)
  3606. (byte-compile-out 'byte-unbind num-dynamic-bindings)))
  3607. ;; Pop lexical variables off the stack, possibly preserving the
  3608. ;; return value of the body.
  3609. (when init-lexenv
  3610. ;; INIT-LEXENV contains all init values left on the stack
  3611. (byte-compile-discard (length init-lexenv) preserve-body-value)))
  3612. (defun byte-compile-let (form)
  3613. "Generate code for the `let' form FORM."
  3614. (let ((clauses (cadr form))
  3615. (init-lexenv nil))
  3616. (when (eq (car form) 'let)
  3617. ;; First compute the binding values in the old scope.
  3618. (dolist (var clauses)
  3619. (push (byte-compile-push-binding-init var) init-lexenv)))
  3620. ;; New scope.
  3621. (let ((byte-compile-bound-variables byte-compile-bound-variables)
  3622. (byte-compile--lexical-environment
  3623. byte-compile--lexical-environment))
  3624. ;; Bind the variables.
  3625. ;; For `let', do it in reverse order, because it makes no
  3626. ;; semantic difference, but it is a lot more efficient since the
  3627. ;; values are now in reverse order on the stack.
  3628. (dolist (var (if (eq (car form) 'let) (reverse clauses) clauses))
  3629. (unless (eq (car form) 'let)
  3630. (push (byte-compile-push-binding-init var) init-lexenv))
  3631. (let ((var (if (consp var) (car var) var)))
  3632. (cond ((null lexical-binding)
  3633. ;; If there are no lexical bindings, we can do things simply.
  3634. (byte-compile-dynamic-variable-bind var))
  3635. ((byte-compile-bind var init-lexenv)
  3636. (pop init-lexenv)))))
  3637. ;; Emit the body.
  3638. (let ((init-stack-depth byte-compile-depth))
  3639. (byte-compile-body-do-effect (cdr (cdr form)))
  3640. ;; Unbind the variables.
  3641. (if lexical-binding
  3642. ;; Unbind both lexical and dynamic variables.
  3643. (progn
  3644. (assert (or (eq byte-compile-depth init-stack-depth)
  3645. (eq byte-compile-depth (1+ init-stack-depth))))
  3646. (byte-compile-unbind clauses init-lexenv (> byte-compile-depth
  3647. init-stack-depth)))
  3648. ;; Unbind dynamic variables.
  3649. (byte-compile-out 'byte-unbind (length clauses)))))))
  3650. (byte-defop-compiler-1 /= byte-compile-negated)
  3651. (byte-defop-compiler-1 atom byte-compile-negated)
  3652. (byte-defop-compiler-1 nlistp byte-compile-negated)
  3653. (put '/= 'byte-compile-negated-op '=)
  3654. (put 'atom 'byte-compile-negated-op 'consp)
  3655. (put 'nlistp 'byte-compile-negated-op 'listp)
  3656. (defun byte-compile-negated (form)
  3657. (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
  3658. ;; Even when optimization is off, /= is optimized to (not (= ...)).
  3659. (defun byte-compile-negation-optimizer (form)
  3660. ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
  3661. (byte-compile-set-symbol-position (car form))
  3662. (list 'not
  3663. (cons (or (get (car form) 'byte-compile-negated-op)
  3664. (error
  3665. "Compiler error: `%s' has no `byte-compile-negated-op' property"
  3666. (car form)))
  3667. (cdr form))))
  3668. ;;; other tricky macro-like special-forms
  3669. (byte-defop-compiler-1 catch)
  3670. (byte-defop-compiler-1 unwind-protect)
  3671. (byte-defop-compiler-1 condition-case)
  3672. (byte-defop-compiler-1 save-excursion)
  3673. (byte-defop-compiler-1 save-current-buffer)
  3674. (byte-defop-compiler-1 save-restriction)
  3675. ;; (byte-defop-compiler-1 save-window-excursion) ;Obsolete: now a macro.
  3676. ;; (byte-defop-compiler-1 with-output-to-temp-buffer) ;Obsolete: now a macro.
  3677. (byte-defop-compiler-1 track-mouse)
  3678. (defun byte-compile-catch (form)
  3679. (byte-compile-form (car (cdr form)))
  3680. (pcase (cddr form)
  3681. (`(:fun-body ,f)
  3682. (byte-compile-form `(list 'funcall ,f)))
  3683. (body
  3684. (byte-compile-push-constant
  3685. (byte-compile-top-level (cons 'progn body) byte-compile--for-effect))))
  3686. (byte-compile-out 'byte-catch 0))
  3687. (defun byte-compile-unwind-protect (form)
  3688. (pcase (cddr form)
  3689. (`(:fun-body ,f)
  3690. (byte-compile-form `(list (list 'funcall ,f))))
  3691. (handlers
  3692. (byte-compile-push-constant
  3693. (byte-compile-top-level-body handlers t))))
  3694. (byte-compile-out 'byte-unwind-protect 0)
  3695. (byte-compile-form-do-effect (car (cdr form)))
  3696. (byte-compile-out 'byte-unbind 1))
  3697. (defun byte-compile-track-mouse (form)
  3698. (byte-compile-form
  3699. (pcase form
  3700. (`(,_ :fun-body ,f) `(eval (list 'track-mouse (list 'funcall ,f))))
  3701. (_ `(eval '(track-mouse ,@(byte-compile-top-level-body (cdr form))))))))
  3702. (defun byte-compile-condition-case (form)
  3703. (let* ((var (nth 1 form))
  3704. (fun-bodies (eq var :fun-body))
  3705. (byte-compile-bound-variables
  3706. (if (and var (not fun-bodies))
  3707. (cons var byte-compile-bound-variables)
  3708. byte-compile-bound-variables)))
  3709. (byte-compile-set-symbol-position 'condition-case)
  3710. (unless (symbolp var)
  3711. (byte-compile-warn
  3712. "`%s' is not a variable-name or nil (in condition-case)" var))
  3713. (if fun-bodies (setq var (make-symbol "err")))
  3714. (byte-compile-push-constant var)
  3715. (if fun-bodies
  3716. (byte-compile-form `(list 'funcall ,(nth 2 form)))
  3717. (byte-compile-push-constant
  3718. (byte-compile-top-level (nth 2 form) byte-compile--for-effect)))
  3719. (let ((compiled-clauses
  3720. (mapcar
  3721. (lambda (clause)
  3722. (let ((condition (car clause)))
  3723. (cond ((not (or (symbolp condition)
  3724. (and (listp condition)
  3725. (let ((ok t))
  3726. (dolist (sym condition)
  3727. (if (not (symbolp sym))
  3728. (setq ok nil)))
  3729. ok))))
  3730. (byte-compile-warn
  3731. "`%S' is not a condition name or list of such (in condition-case)"
  3732. condition))
  3733. ;; (not (or (eq condition 't)
  3734. ;; (and (stringp (get condition 'error-message))
  3735. ;; (consp (get condition
  3736. ;; 'error-conditions)))))
  3737. ;; (byte-compile-warn
  3738. ;; "`%s' is not a known condition name
  3739. ;; (in condition-case)"
  3740. ;; condition))
  3741. )
  3742. (if fun-bodies
  3743. `(list ',condition (list 'funcall ,(cadr clause) ',var))
  3744. (cons condition
  3745. (byte-compile-top-level-body
  3746. (cdr clause) byte-compile--for-effect)))))
  3747. (cdr (cdr (cdr form))))))
  3748. (if fun-bodies
  3749. (byte-compile-form `(list ,@compiled-clauses))
  3750. (byte-compile-push-constant compiled-clauses)))
  3751. (byte-compile-out 'byte-condition-case 0)))
  3752. (defun byte-compile-save-excursion (form)
  3753. (if (and (eq 'set-buffer (car-safe (car-safe (cdr form))))
  3754. (byte-compile-warning-enabled-p 'suspicious))
  3755. (byte-compile-warn
  3756. "Use `with-current-buffer' rather than save-excursion+set-buffer"))
  3757. (byte-compile-out 'byte-save-excursion 0)
  3758. (byte-compile-body-do-effect (cdr form))
  3759. (byte-compile-out 'byte-unbind 1))
  3760. (defun byte-compile-save-restriction (form)
  3761. (byte-compile-out 'byte-save-restriction 0)
  3762. (byte-compile-body-do-effect (cdr form))
  3763. (byte-compile-out 'byte-unbind 1))
  3764. (defun byte-compile-save-current-buffer (form)
  3765. (byte-compile-out 'byte-save-current-buffer 0)
  3766. (byte-compile-body-do-effect (cdr form))
  3767. (byte-compile-out 'byte-unbind 1))
  3768. ;;; top-level forms elsewhere
  3769. (byte-defop-compiler-1 defun)
  3770. (byte-defop-compiler-1 defmacro)
  3771. (byte-defop-compiler-1 defvar)
  3772. (byte-defop-compiler-1 defconst byte-compile-defvar)
  3773. (byte-defop-compiler-1 autoload)
  3774. (byte-defop-compiler-1 lambda byte-compile-lambda-form)
  3775. (defun byte-compile-defun (form)
  3776. ;; This is not used for file-level defuns with doc strings.
  3777. (if (symbolp (car form))
  3778. (byte-compile-set-symbol-position (car form))
  3779. (byte-compile-set-symbol-position 'defun)
  3780. (error "defun name must be a symbol, not %s" (car form)))
  3781. (byte-compile-push-constant 'defalias)
  3782. (byte-compile-push-constant (nth 1 form))
  3783. (byte-compile-push-constant (byte-compile-lambda (cdr (cdr form)) t))
  3784. (byte-compile-out 'byte-call 2))
  3785. (defun byte-compile-defmacro (form)
  3786. ;; This is not used for file-level defmacros with doc strings.
  3787. (byte-compile-body-do-effect
  3788. (let ((decls (byte-compile-defmacro-declaration form))
  3789. (code (byte-compile-lambda (cdr (cdr form)) t)))
  3790. `((defalias ',(nth 1 form)
  3791. ,(if (eq (car-safe code) 'make-byte-code)
  3792. `(cons 'macro ,code)
  3793. `'(macro . ,(eval code))))
  3794. ,@decls
  3795. ',(nth 1 form)))))
  3796. ;; If foo.el declares `toto' as obsolete, it is likely that foo.el will
  3797. ;; actually use `toto' in order for this obsolete variable to still work
  3798. ;; correctly, so paradoxically, while byte-compiling foo.el, the presence
  3799. ;; of a make-obsolete-variable call for `toto' is an indication that `toto'
  3800. ;; should not trigger obsolete-warnings in foo.el.
  3801. (byte-defop-compiler-1 make-obsolete-variable)
  3802. (defun byte-compile-make-obsolete-variable (form)
  3803. (when (eq 'quote (car-safe (nth 1 form)))
  3804. (push (nth 1 (nth 1 form)) byte-compile-global-not-obsolete-vars))
  3805. (byte-compile-normal-call form))
  3806. (defconst byte-compile-tmp-var (make-symbol "def-tmp-var"))
  3807. (defun byte-compile-defvar (form)
  3808. ;; This is not used for file-level defvar/consts.
  3809. (when (and (symbolp (nth 1 form))
  3810. (not (string-match "[-*/:$]" (symbol-name (nth 1 form))))
  3811. (byte-compile-warning-enabled-p 'lexical))
  3812. (byte-compile-warn "global/dynamic var `%s' lacks a prefix"
  3813. (nth 1 form)))
  3814. (let ((fun (nth 0 form))
  3815. (var (nth 1 form))
  3816. (value (nth 2 form))
  3817. (string (nth 3 form)))
  3818. (byte-compile-set-symbol-position fun)
  3819. (when (or (> (length form) 4)
  3820. (and (eq fun 'defconst) (null (cddr form))))
  3821. (let ((ncall (length (cdr form))))
  3822. (byte-compile-warn
  3823. "`%s' called with %d argument%s, but %s %s"
  3824. fun ncall
  3825. (if (= 1 ncall) "" "s")
  3826. (if (< ncall 2) "requires" "accepts only")
  3827. "2-3")))
  3828. (push var byte-compile-bound-variables)
  3829. (if (eq fun 'defconst)
  3830. (push var byte-compile-const-variables))
  3831. (when (and string (not (stringp string)))
  3832. (byte-compile-warn "third arg to `%s %s' is not a string: %s"
  3833. fun var string))
  3834. (byte-compile-form-do-effect
  3835. (if (cddr form) ; `value' provided
  3836. ;; Quote with `quote' to prevent byte-compiling the body,
  3837. ;; which would lead to an inf-loop.
  3838. `(funcall '(lambda (,byte-compile-tmp-var)
  3839. (,fun ,var ,byte-compile-tmp-var ,@(nthcdr 3 form)))
  3840. ,value)
  3841. (if (eq fun 'defconst)
  3842. ;; This will signal an appropriate error at runtime.
  3843. `(eval ',form)
  3844. ;; A simple (defvar foo) just returns foo.
  3845. `',var)))))
  3846. (defun byte-compile-autoload (form)
  3847. (byte-compile-set-symbol-position 'autoload)
  3848. (and (byte-compile-constp (nth 1 form))
  3849. (byte-compile-constp (nth 5 form))
  3850. (eval (nth 5 form)) ; macro-p
  3851. (not (fboundp (eval (nth 1 form))))
  3852. (byte-compile-warn
  3853. "The compiler ignores `autoload' except at top level. You should
  3854. probably put the autoload of the macro `%s' at top-level."
  3855. (eval (nth 1 form))))
  3856. (byte-compile-normal-call form))
  3857. ;; Lambdas in valid places are handled as special cases by various code.
  3858. ;; The ones that remain are errors.
  3859. (defun byte-compile-lambda-form (_form)
  3860. (byte-compile-set-symbol-position 'lambda)
  3861. (error "`lambda' used as function name is invalid"))
  3862. ;; Compile normally, but deal with warnings for the function being defined.
  3863. (put 'defalias 'byte-hunk-handler 'byte-compile-file-form-defalias)
  3864. ;; Used for eieio--defalias as well.
  3865. (defun byte-compile-file-form-defalias (form)
  3866. (if (and (consp (cdr form)) (consp (nth 1 form))
  3867. (eq (car (nth 1 form)) 'quote)
  3868. (consp (cdr (nth 1 form)))
  3869. (symbolp (nth 1 (nth 1 form))))
  3870. (let ((constant
  3871. (and (consp (nthcdr 2 form))
  3872. (consp (nth 2 form))
  3873. (eq (car (nth 2 form)) 'quote)
  3874. (consp (cdr (nth 2 form)))
  3875. (symbolp (nth 1 (nth 2 form))))))
  3876. (byte-compile-defalias-warn (nth 1 (nth 1 form)))
  3877. (push (cons (nth 1 (nth 1 form))
  3878. (if constant (nth 1 (nth 2 form)) t))
  3879. byte-compile-function-environment)))
  3880. ;; We used to just do: (byte-compile-normal-call form)
  3881. ;; But it turns out that this fails to optimize the code.
  3882. ;; So instead we now do the same as what other byte-hunk-handlers do,
  3883. ;; which is to call back byte-compile-file-form and then return nil.
  3884. ;; Except that we can't just call byte-compile-file-form since it would
  3885. ;; call us right back.
  3886. (byte-compile-keep-pending form)
  3887. ;; Return nil so the form is not output twice.
  3888. nil)
  3889. ;; Turn off warnings about prior calls to the function being defalias'd.
  3890. ;; This could be smarter and compare those calls with
  3891. ;; the function it is being aliased to.
  3892. (defun byte-compile-defalias-warn (new)
  3893. (let ((calls (assq new byte-compile-unresolved-functions)))
  3894. (if calls
  3895. (setq byte-compile-unresolved-functions
  3896. (delq calls byte-compile-unresolved-functions)))))
  3897. (byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings)
  3898. (defun byte-compile-no-warnings (form)
  3899. (let (byte-compile-warnings)
  3900. (byte-compile-form (cons 'progn (cdr form)))))
  3901. ;; Warn about misuses of make-variable-buffer-local.
  3902. (byte-defop-compiler-1 make-variable-buffer-local
  3903. byte-compile-make-variable-buffer-local)
  3904. (defun byte-compile-make-variable-buffer-local (form)
  3905. (if (and (eq (car-safe (car-safe (cdr-safe form))) 'quote)
  3906. (byte-compile-warning-enabled-p 'make-local))
  3907. (byte-compile-warn
  3908. "`make-variable-buffer-local' should be called at toplevel"))
  3909. (byte-compile-normal-call form))
  3910. (put 'make-variable-buffer-local
  3911. 'byte-hunk-handler 'byte-compile-form-make-variable-buffer-local)
  3912. (defun byte-compile-form-make-variable-buffer-local (form)
  3913. (byte-compile-keep-pending form 'byte-compile-normal-call))
  3914. (byte-defop-compiler-1 add-to-list byte-compile-add-to-list)
  3915. (defun byte-compile-add-to-list (form)
  3916. ;; FIXME: This could be used for `set' as well, except that it's got
  3917. ;; its own opcode, so the final `byte-compile-normal-call' needs to
  3918. ;; be replaced with something else.
  3919. (pcase form
  3920. (`(,fun ',var . ,_)
  3921. (byte-compile-check-variable var 'assign)
  3922. (if (assq var byte-compile--lexical-environment)
  3923. (byte-compile-log-warning
  3924. (format "%s cannot use lexical var `%s'" fun var)
  3925. nil :error)
  3926. (unless (or (not (byte-compile-warning-enabled-p 'free-vars))
  3927. (boundp var)
  3928. (memq var byte-compile-bound-variables)
  3929. (memq var byte-compile-free-references))
  3930. (byte-compile-warn "assignment to free variable `%S'" var)
  3931. (push var byte-compile-free-references)))))
  3932. (byte-compile-normal-call form))
  3933. ;;; tags
  3934. ;; Note: Most operations will strip off the 'TAG, but it speeds up
  3935. ;; optimization to have the 'TAG as a part of the tag.
  3936. ;; Tags will be (TAG . (tag-number . stack-depth)).
  3937. (defun byte-compile-make-tag ()
  3938. (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
  3939. (defun byte-compile-out-tag (tag)
  3940. (setq byte-compile-output (cons tag byte-compile-output))
  3941. (if (cdr (cdr tag))
  3942. (progn
  3943. ;; ## remove this someday
  3944. (and byte-compile-depth
  3945. (not (= (cdr (cdr tag)) byte-compile-depth))
  3946. (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
  3947. (setq byte-compile-depth (cdr (cdr tag))))
  3948. (setcdr (cdr tag) byte-compile-depth)))
  3949. (defun byte-compile-goto (opcode tag)
  3950. (push (cons opcode tag) byte-compile-output)
  3951. (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
  3952. (1- byte-compile-depth)
  3953. byte-compile-depth))
  3954. (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
  3955. (1- byte-compile-depth))))
  3956. (defun byte-compile-stack-adjustment (op operand)
  3957. "Return the amount by which an operation adjusts the stack.
  3958. OP and OPERAND are as passed to `byte-compile-out'."
  3959. (if (memq op '(byte-call byte-discardN byte-discardN-preserve-tos))
  3960. ;; For calls, OPERAND is the number of args, so we pop OPERAND + 1
  3961. ;; elements, and the push the result, for a total of -OPERAND.
  3962. ;; For discardN*, of course, we just pop OPERAND elements.
  3963. (- operand)
  3964. (or (aref byte-stack+-info (symbol-value op))
  3965. ;; Ops with a nil entry in `byte-stack+-info' are byte-codes
  3966. ;; that take OPERAND values off the stack and push a result, for
  3967. ;; a total of 1 - OPERAND
  3968. (- 1 operand))))
  3969. (defun byte-compile-out (op &optional operand)
  3970. (push (cons op operand) byte-compile-output)
  3971. (if (eq op 'byte-return)
  3972. ;; This is actually an unnecessary case, because there should be no
  3973. ;; more ops behind byte-return.
  3974. (setq byte-compile-depth nil)
  3975. (setq byte-compile-depth
  3976. (+ byte-compile-depth (byte-compile-stack-adjustment op operand)))
  3977. (setq byte-compile-maxdepth (max byte-compile-depth byte-compile-maxdepth))
  3978. ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
  3979. ))
  3980. ;;; call tree stuff
  3981. (defun byte-compile-annotate-call-tree (form)
  3982. (let (entry)
  3983. ;; annotate the current call
  3984. (if (setq entry (assq (car form) byte-compile-call-tree))
  3985. (or (memq byte-compile-current-form (nth 1 entry)) ;callers
  3986. (setcar (cdr entry)
  3987. (cons byte-compile-current-form (nth 1 entry))))
  3988. (setq byte-compile-call-tree
  3989. (cons (list (car form) (list byte-compile-current-form) nil)
  3990. byte-compile-call-tree)))
  3991. ;; annotate the current function
  3992. (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
  3993. (or (memq (car form) (nth 2 entry)) ;called
  3994. (setcar (cdr (cdr entry))
  3995. (cons (car form) (nth 2 entry))))
  3996. (setq byte-compile-call-tree
  3997. (cons (list byte-compile-current-form nil (list (car form)))
  3998. byte-compile-call-tree)))
  3999. ))
  4000. ;; Renamed from byte-compile-report-call-tree
  4001. ;; to avoid interfering with completion of byte-compile-file.
  4002. ;;;###autoload
  4003. (defun display-call-tree (&optional filename)
  4004. "Display a call graph of a specified file.
  4005. This lists which functions have been called, what functions called
  4006. them, and what functions they call. The list includes all functions
  4007. whose definitions have been compiled in this Emacs session, as well as
  4008. all functions called by those functions.
  4009. The call graph does not include macros, inline functions, or
  4010. primitives that the byte-code interpreter knows about directly \(eq,
  4011. cons, etc.\).
  4012. The call tree also lists those functions which are not known to be called
  4013. \(that is, to which no calls have been compiled\), and which cannot be
  4014. invoked interactively."
  4015. (interactive)
  4016. (message "Generating call tree...")
  4017. (with-output-to-temp-buffer "*Call-Tree*"
  4018. (set-buffer "*Call-Tree*")
  4019. (erase-buffer)
  4020. (message "Generating call tree... (sorting on %s)"
  4021. byte-compile-call-tree-sort)
  4022. (insert "Call tree for "
  4023. (cond ((null byte-compile-current-file) (or filename "???"))
  4024. ((stringp byte-compile-current-file)
  4025. byte-compile-current-file)
  4026. (t (buffer-name byte-compile-current-file)))
  4027. " sorted on "
  4028. (prin1-to-string byte-compile-call-tree-sort)
  4029. ":\n\n")
  4030. (if byte-compile-call-tree-sort
  4031. (setq byte-compile-call-tree
  4032. (sort byte-compile-call-tree
  4033. (case byte-compile-call-tree-sort
  4034. (callers
  4035. (lambda (x y) (< (length (nth 1 x))
  4036. (length (nth 1 y)))))
  4037. (calls
  4038. (lambda (x y) (< (length (nth 2 x))
  4039. (length (nth 2 y)))))
  4040. (calls+callers
  4041. (lambda (x y) (< (+ (length (nth 1 x))
  4042. (length (nth 2 x)))
  4043. (+ (length (nth 1 y))
  4044. (length (nth 2 y))))))
  4045. (name
  4046. (lambda (x y) (string< (car x) (car y))))
  4047. (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
  4048. byte-compile-call-tree-sort))))))
  4049. (message "Generating call tree...")
  4050. (let ((rest byte-compile-call-tree)
  4051. (b (current-buffer))
  4052. f p
  4053. callers calls)
  4054. (while rest
  4055. (prin1 (car (car rest)) b)
  4056. (setq callers (nth 1 (car rest))
  4057. calls (nth 2 (car rest)))
  4058. (insert "\t"
  4059. (cond ((not (fboundp (setq f (car (car rest)))))
  4060. (if (null f)
  4061. " <top level>";; shouldn't insert nil then, actually -sk
  4062. " <not defined>"))
  4063. ((subrp (setq f (symbol-function f)))
  4064. " <subr>")
  4065. ((symbolp f)
  4066. (format " ==> %s" f))
  4067. ((byte-code-function-p f)
  4068. "<compiled function>")
  4069. ((not (consp f))
  4070. "<malformed function>")
  4071. ((eq 'macro (car f))
  4072. (if (or (byte-code-function-p (cdr f))
  4073. (assq 'byte-code (cdr (cdr (cdr f)))))
  4074. " <compiled macro>"
  4075. " <macro>"))
  4076. ((assq 'byte-code (cdr (cdr f)))
  4077. "<compiled lambda>")
  4078. ((eq 'lambda (car f))
  4079. "<function>")
  4080. (t "???"))
  4081. (format " (%d callers + %d calls = %d)"
  4082. ;; Does the optimizer eliminate common subexpressions?-sk
  4083. (length callers)
  4084. (length calls)
  4085. (+ (length callers) (length calls)))
  4086. "\n")
  4087. (if callers
  4088. (progn
  4089. (insert " called by:\n")
  4090. (setq p (point))
  4091. (insert " " (if (car callers)
  4092. (mapconcat 'symbol-name callers ", ")
  4093. "<top level>"))
  4094. (let ((fill-prefix " "))
  4095. (fill-region-as-paragraph p (point)))
  4096. (unless (= 0 (current-column))
  4097. (insert "\n"))))
  4098. (if calls
  4099. (progn
  4100. (insert " calls:\n")
  4101. (setq p (point))
  4102. (insert " " (mapconcat 'symbol-name calls ", "))
  4103. (let ((fill-prefix " "))
  4104. (fill-region-as-paragraph p (point)))
  4105. (unless (= 0 (current-column))
  4106. (insert "\n"))))
  4107. (setq rest (cdr rest)))
  4108. (message "Generating call tree...(finding uncalled functions...)")
  4109. (setq rest byte-compile-call-tree)
  4110. (let (uncalled def)
  4111. (while rest
  4112. (or (nth 1 (car rest))
  4113. (null (setq f (caar rest)))
  4114. (progn
  4115. (setq def (byte-compile-fdefinition f t))
  4116. (and (eq (car-safe def) 'macro)
  4117. (eq (car-safe (cdr-safe def)) 'lambda)
  4118. (setq def (cdr def)))
  4119. (functionp def))
  4120. (progn
  4121. (setq def (byte-compile-fdefinition f nil))
  4122. (and (eq (car-safe def) 'macro)
  4123. (eq (car-safe (cdr-safe def)) 'lambda)
  4124. (setq def (cdr def)))
  4125. (commandp def))
  4126. (setq uncalled (cons f uncalled)))
  4127. (setq rest (cdr rest)))
  4128. (if uncalled
  4129. (let ((fill-prefix " "))
  4130. (insert "Noninteractive functions not known to be called:\n ")
  4131. (setq p (point))
  4132. (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
  4133. (fill-region-as-paragraph p (point))))))
  4134. (message "Generating call tree...done.")))
  4135. ;;;###autoload
  4136. (defun batch-byte-compile-if-not-done ()
  4137. "Like `byte-compile-file' but doesn't recompile if already up to date.
  4138. Use this from the command line, with `-batch';
  4139. it won't work in an interactive Emacs."
  4140. (batch-byte-compile t))
  4141. ;;; by crl@newton.purdue.edu
  4142. ;;; Only works noninteractively.
  4143. ;;;###autoload
  4144. (defun batch-byte-compile (&optional noforce)
  4145. "Run `byte-compile-file' on the files remaining on the command line.
  4146. Use this from the command line, with `-batch';
  4147. it won't work in an interactive Emacs.
  4148. Each file is processed even if an error occurred previously.
  4149. For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
  4150. If NOFORCE is non-nil, don't recompile a file that seems to be
  4151. already up-to-date."
  4152. ;; command-line-args-left is what is left of the command line, from
  4153. ;; startup.el.
  4154. (defvar command-line-args-left) ;Avoid 'free variable' warning
  4155. (if (not noninteractive)
  4156. (error "`batch-byte-compile' is to be used only with -batch"))
  4157. (let ((error nil))
  4158. (while command-line-args-left
  4159. (if (file-directory-p (expand-file-name (car command-line-args-left)))
  4160. ;; Directory as argument.
  4161. (let (source dest)
  4162. (dolist (file (directory-files (car command-line-args-left)))
  4163. (if (and (string-match emacs-lisp-file-regexp file)
  4164. (not (auto-save-file-name-p file))
  4165. (setq source
  4166. (expand-file-name file
  4167. (car command-line-args-left)))
  4168. (setq dest (byte-compile-dest-file source))
  4169. (file-exists-p dest)
  4170. (file-newer-than-file-p source dest))
  4171. (if (null (batch-byte-compile-file source))
  4172. (setq error t)))))
  4173. ;; Specific file argument
  4174. (if (or (not noforce)
  4175. (let* ((source (car command-line-args-left))
  4176. (dest (byte-compile-dest-file source)))
  4177. (or (not (file-exists-p dest))
  4178. (file-newer-than-file-p source dest))))
  4179. (if (null (batch-byte-compile-file (car command-line-args-left)))
  4180. (setq error t))))
  4181. (setq command-line-args-left (cdr command-line-args-left)))
  4182. (kill-emacs (if error 1 0))))
  4183. (defun batch-byte-compile-file (file)
  4184. (if debug-on-error
  4185. (byte-compile-file file)
  4186. (condition-case err
  4187. (byte-compile-file file)
  4188. (file-error
  4189. (message (if (cdr err)
  4190. ">>Error occurred processing %s: %s (%s)"
  4191. ">>Error occurred processing %s: %s")
  4192. file
  4193. (get (car err) 'error-message)
  4194. (prin1-to-string (cdr err)))
  4195. (let ((destfile (byte-compile-dest-file file)))
  4196. (if (file-exists-p destfile)
  4197. (delete-file destfile)))
  4198. nil)
  4199. (error
  4200. (message (if (cdr err)
  4201. ">>Error occurred processing %s: %s (%s)"
  4202. ">>Error occurred processing %s: %s")
  4203. file
  4204. (get (car err) 'error-message)
  4205. (prin1-to-string (cdr err)))
  4206. nil))))
  4207. (defun byte-compile-refresh-preloaded ()
  4208. "Reload any Lisp file that was changed since Emacs was dumped.
  4209. Use with caution."
  4210. (let* ((argv0 (car command-line-args))
  4211. (emacs-file (executable-find argv0)))
  4212. (if (not (and emacs-file (file-executable-p emacs-file)))
  4213. (message "Can't find %s to refresh preloaded Lisp files" argv0)
  4214. (dolist (f (reverse load-history))
  4215. (setq f (car f))
  4216. (if (string-match "elc\\'" f) (setq f (substring f 0 -1)))
  4217. (when (and (file-readable-p f)
  4218. (file-newer-than-file-p f emacs-file)
  4219. ;; Don't reload the source version of the files below
  4220. ;; because that causes subsequent byte-compilation to
  4221. ;; be a lot slower and need a higher max-lisp-eval-depth,
  4222. ;; so it can cause recompilation to fail.
  4223. (not (member (file-name-nondirectory f)
  4224. '("pcase.el" "bytecomp.el" "macroexp.el"
  4225. "cconv.el" "byte-opt.el"))))
  4226. (message "Reloading stale %s" (file-name-nondirectory f))
  4227. (condition-case nil
  4228. (load f 'noerror nil 'nosuffix)
  4229. ;; Probably shouldn't happen, but in case of an error, it seems
  4230. ;; at least as useful to ignore it as it is to stop compilation.
  4231. (error nil)))))))
  4232. ;;;###autoload
  4233. (defun batch-byte-recompile-directory (&optional arg)
  4234. "Run `byte-recompile-directory' on the dirs remaining on the command line.
  4235. Must be used only with `-batch', and kills Emacs on completion.
  4236. For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
  4237. Optional argument ARG is passed as second argument ARG to
  4238. `byte-recompile-directory'; see there for its possible values
  4239. and corresponding effects."
  4240. ;; command-line-args-left is what is left of the command line (startup.el)
  4241. (defvar command-line-args-left) ;Avoid 'free variable' warning
  4242. (if (not noninteractive)
  4243. (error "batch-byte-recompile-directory is to be used only with -batch"))
  4244. (or command-line-args-left
  4245. (setq command-line-args-left '(".")))
  4246. (while command-line-args-left
  4247. (byte-recompile-directory (car command-line-args-left) arg)
  4248. (setq command-line-args-left (cdr command-line-args-left)))
  4249. (kill-emacs 0))
  4250. (provide 'byte-compile)
  4251. (provide 'bytecomp)
  4252. ;;; report metering (see the hacks in bytecode.c)
  4253. (defvar byte-code-meter)
  4254. (defun byte-compile-report-ops ()
  4255. (or (boundp 'byte-metering-on)
  4256. (error "You must build Emacs with -DBYTE_CODE_METER to use this"))
  4257. (with-output-to-temp-buffer "*Meter*"
  4258. (set-buffer "*Meter*")
  4259. (let ((i 0) n op off)
  4260. (while (< i 256)
  4261. (setq n (aref (aref byte-code-meter 0) i)
  4262. off nil)
  4263. (if t ;(not (zerop n))
  4264. (progn
  4265. (setq op i)
  4266. (setq off nil)
  4267. (cond ((< op byte-nth)
  4268. (setq off (logand op 7))
  4269. (setq op (logand op 248)))
  4270. ((>= op byte-constant)
  4271. (setq off (- op byte-constant)
  4272. op byte-constant)))
  4273. (setq op (aref byte-code-vector op))
  4274. (insert (format "%-4d" i))
  4275. (insert (symbol-name op))
  4276. (if off (insert " [" (int-to-string off) "]"))
  4277. (indent-to 40)
  4278. (insert (int-to-string n) "\n")))
  4279. (setq i (1+ i))))))
  4280. ;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
  4281. ;; itself, compile some of its most used recursive functions (at load time).
  4282. ;;
  4283. (eval-when-compile
  4284. (or (byte-code-function-p (symbol-function 'byte-compile-form))
  4285. (assq 'byte-code (symbol-function 'byte-compile-form))
  4286. (let ((byte-optimize nil) ; do it fast
  4287. (byte-compile-warnings nil))
  4288. (mapc (lambda (x)
  4289. (or noninteractive (message "compiling %s..." x))
  4290. (byte-compile x)
  4291. (or noninteractive (message "compiling %s...done" x)))
  4292. '(byte-compile-normal-call
  4293. byte-compile-form
  4294. byte-compile-body
  4295. ;; Inserted some more than necessary, to speed it up.
  4296. byte-compile-top-level
  4297. byte-compile-out-toplevel
  4298. byte-compile-constant
  4299. byte-compile-variable-ref))))
  4300. nil)
  4301. (run-hooks 'bytecomp-load-hook)
  4302. ;;; bytecomp.el ends here