quinta-feira, 10 de dezembro de 2009

Introspecção de módulos em Chicken Scheme

Abaixo está um pequeno procedimento para inspecionar símbolos de módulos importados no sistema Chicken (para a versão 4). É semelhante à função dir de Python.

(define (introspect #!optional symbol)
(if symbol
(let ((module-data (alist-ref symbol ##sys#module-table)))
(if module-data
(let-values (((_ module-symbols _) (##sys#module-exports module-data)))
(map car module-symbols))
(let loop ((syms (##sys#current-environment)))
(if (null? syms)
'()
(let* ((sym/sym+prefix (car syms))
(sym+prefix (symbol->string (cdr sym/sym+prefix)))
(tokens (string-split sym+prefix "#" #t))
(prefix (car tokens)))
(if (equal? (->string symbol) prefix)
(cons (string->symbol (string-intersperse (cdr tokens) ""))
(loop (cdr syms)))
(loop (cdr syms))))))))
(map car (##sys#current-environment))))



Exemplos de uso

$ csi -n

CHICKEN
(c)2008-2009 The Chicken Team
(c)2000-2007 Felix L. Winkelmann
Version 4.2.0 - SVN rev. 16023
linux-unix-gnu-x86 [ manyargs dload ptables applyhook ]
compiled 2009-10-20 on dellito (Linux)

#;1> ,l introspection.scm
; loading introspection.scm ...
#;1> (introspect)
()
#;2> (import html-utils)
; loading /usr/local/chicken-4.2.0/lib/chicken/4/html-utils.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/chicken.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/scheme.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/files.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/data-structures.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/srfi-13.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/posix.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/utils.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/html-tags.import.so ...
; loading /usr/local/chicken-4.2.0/lib/chicken/4/srfi-1.import.so ...
#;3> (introspect 'html-utils)
(tabularize itemize enumerate html-page combo-box hidden-input)
#;4> (introspect 'srfi-1)
(alist-cons alist-copy alist-delete alist-delete! any append! append-map append-map!
append-reverse append-reverse! assoc break break! car+cdr circular-list circular-list?
concatenate concatenate! cons* count delete delete! delete-duplicates delete-duplicates!
dotted-list? drop drop-right drop-right! drop-while eighth every fifth filter filter! filter-map
find find-tail first fold fold-right fourth iota last last-pair length+ list-copy list-index
list-tabulate list= lset-adjoin lset-diff+intersection lset-diff+intersection! lset-difference
lset-difference! lset-intersection lset-intersection! lset-union lset-union! lset-xor lset-xor!
lset<= lset= make-list map map! map-in-order member ninth not-pair? null-list? pair-fold
pair-fold-right pair-for-each partition partition! proper-list? reduce reduce-right remove
remove! reverse! second seventh sixth span span! split-at split-at! take take! take-right
take-while take-while! tenth third unfold unfold-right unzip1 unzip2 unzip3 unzip4 unzip5 xcons
zip)

terça-feira, 1 de dezembro de 2009

Opções utilizadas nos diferentes níveis de otimização do GCC

Hoje surgiu uma dica interessante na lista de discussão do GCC. Está reproduzida abaixo:


From: "John (Eljay) Love-Jensen"
Subject: Re: How can I find out which optimizing techniques were used for each function?
To: Byoungyoung Lee, GCC-help
Date: Tue, 1 Dec 2009 04:51:11 -0800

Hi Byoungyoung Lee,

> Is there anyway in gcc to log the information about which compiler
> options were actually used to optimize each function?
> For example, if I compile a program with option -O3, how could I
> figure out which specific options (such as -funroll-loops or something
> like that) were used to optimize function X ?

I use this technique:

echo '' | gcc -O3 -fverbose-asm -S -xc - -o O3.s
cat O3.s

In the created O3.s assembly source comment, it mentions which flags were
enabled by the -O3 switch.

If you want a detailed assembly line-by-line diagnostic of which particular
optimization was used for a given algorithm, you'll have to do some more
sleuthing.

Such as using -fno-<flag> to disable all the flags, and enable them
one-by-one.

Or perhaps by dumping the compiler state after each optimization pass (which
I think is usually only used as a diagnostic aid for making changes to GCC
itself).

Also, the majority of optimizations do not have a toggle-able flag to
enable/disable them. As I understand it... -O1 enables a whole lot of
optimizations that do not have toggle-able flags. -O2 enables a few more.
-O3 enables one-or-two more.

HTH,
--Eljay

Indentação de and-let* no Emacs

A indentação padrão do Emacs para a expressão and-let* não é das melhores. Abaixo está a expressão a ser avaliada no Emacs para fazer com que a indentação padrão de and-let* (Figura 1) fique como a da Figura 2:

(put 'and-let* 'scheme-indent-function 1)