From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ken Raeburn Newsgroups: gmane.lisp.guile.devel Subject: Re: expression Date: Thu, 24 Jun 2010 02:55:59 -0400 Message-ID: <867048CD-FB59-4D61-A944-03E4BE4D2960@raeburn.org> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1277362612 31705 80.91.229.12 (24 Jun 2010 06:56:52 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 24 Jun 2010 06:56:52 +0000 (UTC) Cc: guile-devel@gnu.org To: Michael Lucy Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Thu Jun 24 08:56:51 2010 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ORgMd-0007dv-RL for guile-devel@m.gmane.org; Thu, 24 Jun 2010 08:56:44 +0200 Original-Received: from localhost ([127.0.0.1]:37962 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ORgMc-000297-LQ for guile-devel@m.gmane.org; Thu, 24 Jun 2010 02:56:42 -0400 Original-Received: from [140.186.70.92] (port=40779 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ORgMP-00026t-Ew for guile-devel@gnu.org; Thu, 24 Jun 2010 02:56:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1ORgMO-0001kq-6S for guile-devel@gnu.org; Thu, 24 Jun 2010 02:56:29 -0400 Original-Received: from splat.raeburn.org ([69.25.196.39]:48872 helo=raeburn.org) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1ORgMG-0001jp-43 for guile-devel@gnu.org; Thu, 24 Jun 2010 02:56:28 -0400 Original-Received: from squish.raeburn.org (squish.raeburn.org [10.0.0.172]) by raeburn.org (8.14.3/8.14.1) with ESMTP id o5O6u01d005436; Thu, 24 Jun 2010 02:56:00 -0400 (EDT) In-Reply-To: X-Mailer: Apple Mail (2.1081) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:10558 Archived-At: On Jun 23, 2010, at 17:09, Michael Lucy wrote: > Is there any scheme expression that will just get ignored when the > scheme code is compiled? >=20 > I'm generating some code with a function like: >=20 > (define (gen-update-ab updatea updateb) > `(begin > ,(if updatea `(set! a (+ a 1)) `(donothing)) > ,(if updateb `(set! b (+ b 1)) `(donothing)))) >=20 > And ideally I could replace the donothing function with something that > will get discarded during compilation. A simple constant like #f or '() or 42 shouldn't cause any evaluation to = happen, unless it's the last expression and thus the value to be = returned (in which case you'd just be returning a simple constant). I don't know if there's a canonical efficient way to generate it, but it = looks like "(if #f #f)" will be optimized by the current compiler into = just pushing the magic "undefined value" onto the stack. Getting rid of = the push altogether *IF* the result of the expression is unused is up to = the optimizer; you shouldn't be jumping through hoops in the code you = generate to make that happen. But it appears that it does happen at = least in simple cases: scheme@(guile-user)> ,c (begin (if #f #f) (if #f #f) 42) Disassembly of #: 0 (assert-nargs-ee/locals 0) =20 2 (make-int8 42) ;; 42 4 (return) =20 Hmm... here's another way, though I've no idea if RnRS lets you not have = any expressions in here: scheme@(guile-user)> ,c (begin) =20 Disassembly of #: 0 (assert-nargs-ee/locals 0) =20 2 (void) =20 3 (return) =20 scheme@(guile-user)> ,c (begin (begin) (begin) 42) Disassembly of #: 0 (assert-nargs-ee/locals 0) =20 2 (make-int8 42) ;; 42 4 (return) =20 > There are alternatives, but they're pretty ugly (significantly moreso > in my actual code than they look below): Depends how you define ugliness. :-) Your approach expands each possible update into one S-expression. So = you need *something* there for each one, even if it's just a constant, = or an empty "begin". The alternative, I think, is rearranging your code so that no expression = gets added to the list when there's nothing to do; one way to do that, = off the top of my head, would be to generate a possibly-empty list of = expressions rather than exactly one for each possible update, and then = merge the lists together. Or start with an empty list, and tack = S-expressions on the front for updates that are needed, then add the = "begin" and return the result. But in terms of the generated byte code, the constants should just go = away if they're not actually used, and nested "begin" lists should get = flattened, so I don't think it should matter much either way. If you want the macro expansion to be human-readable for debugging = purposes, you could also consider putting in some kind of no-op = annotation that shouldn't generate code, for example a let loop with a = meaningful label name and no useful body, or wrapping code that actually = has effects but doesn't use the label: scheme@(guile-user)> ,c (begin (let no-update-needed-for-a () #f) 42) Disassembly of #: 0 (assert-nargs-ee/locals 0) =20 2 (br :L171) ;; -> 10 6 (br :L172) ;; -> 14 10 (br :L173) ;; -> 6 14 (make-int8 42) ;; 42 16 (return) =20 Ehh... okay, maybe there's a little work to be done for that one. :-) Ken=