Debian Codegraph @

src/cmp/cmpname.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2007, Juan Jose Garcia Ripoll.
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8
9 ;;;; CMPNAME Unambiguous init names for object files
10 ;;;;
11 ;;;; Every object file in a lisp library or combined FASL (such as the
12 ;;;; compiler), needs a function that creates its data and installs the
13 ;;;; functions. This initialization function has a C name which needs
14 ;;;; to be unique. This file has functions to create such names.
15
16 (in-package "COMPILER")
17
18 (defun encode-number-in-name (number)
19 ;; Encode a number in an alphanumeric identifier which is a valid C name.
20 (declare (si::c-local))
21 (cond ((zerop number) "0")
22 ((minusp number) (encode-number-in-name (- number)))
23 (t
24 (do* ((code "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
25 (base (length code))
26 (output '())
27 (digit 0))
28 ((zerop number) (coerce (nreverse output) 'base-string))
29 (multiple-value-setq (number digit) (floor number base))
30 (push (char code digit) output)))))
31
32 (defun unique-init-name (file)
33 "Create a unique name for this initialization function. The current algorithm
34 relies only on the name of the source file and the time at which it is built. This
35 should be enough to prevent name collisions for object files built in the same
36 machine."
37 (let* ((path (pathname file))
38 (path-hash (logxor (ash (sxhash path) 8)
39 (ash (sxhash (cddr (pathname-directory path))) 16)
40 (sxhash (pathname-name path))))
41 (seconds (get-universal-time))
42 (ms (+ (* seconds 1000)
43 (mod (floor (* 1000 (get-internal-real-time))
44 internal-time-units-per-second)
45 1000)))
46 (tag (concatenate 'base-string
47 "_ecl"
48 (encode-number-in-name path-hash)
49 "_"
50 (encode-number-in-name ms))))
51 tag))
52
53 (defun kind->tag (kind)
54 (case kind
55 ((:object :c) "@EcLtAg")
56 ((:fasl :fas) "@EcLtAg_fas")
57 ((:static-library :lib) "@EcLtAg_lib")
58 ((:shared-library :dll) "@EcLtAg_dll")
59 ((:program) "@EcLtAg_exe")
60 (otherwise (error "C::BUILDER cannot accept files of kind ~s" kind))))
61
62 (defun init-name-tag (init-name &key (kind :object))
63 (concatenate 'base-string (kind->tag kind) ":" init-name "@"))
64
65 (defun search-tag (stream tag)
66 (declare (si::c-local))
67 (do* ((eof nil)
68 (key (concatenate 'list tag ":"))
69 (string key))
70 (nil)
71 (let ((c (read-byte stream nil nil)))
72 (cond ((null c) (return nil))
73 ((not (= c (char-code (pop string))))
74 (setf string key))
75 ((null string)
76 (return t))))))
77
78 (defun read-name (stream)
79 (declare (si::c-local))
80 (concatenate 'string
81 (loop with c = t
82 until (or (null (setf c (read-byte stream nil nil)))
83 (= c #.(char-code #\@)))
84 collect (code-char c))))
85
86 (defun find-init-name (file &key (tag "@EcLtAg"))
87 "Search for the initialization function in an object file. Since the
88 initialization function in object files have more or less unpredictable
89 names, we store them in a string in the object file. This string is recognized
90 by the TAG it has at the beginning This function searches that tag and retrieves
91 the function name it precedes."
92 #-pnacl
93 (with-open-file (stream file :direction :input :element-type '(unsigned-byte 8))
94 (when (search-tag stream tag)
95 (let ((name (read-name stream)))
96 name)))
97 #+pnacl
98 (let* ((pnacl-dis
99 (or (ext:getenv "PNACL_DIS")
100 (error "Please set the PNACL_DIS environment variable to your ~
101 toolchain's pnacl-dis location")))
102 (stream (ext:run-program
103 pnacl-dis
104 (list (namestring (translate-logical-pathname file)))
105 :wait nil :input nil :output :stream :error :output)))
106 (unless stream
107 (error "Unable to disasemble file ~a" file))
108 (when (search-tag stream tag)
109 (let ((name (read-name stream)))
110 name))))
111
112 (defun guess-init-name (pathname kind)
113 (case kind
114 ((:object :c :static-library :lib :shared-library :dll)
115 (or (and (probe-file pathname)
116 (find-init-name pathname :tag (kind->tag kind)))
117 (cmpnote "Cannot find out entry point for binary file ~A" pathname)))
118 (otherwise (compute-init-name pathname :kind kind))))
119
120 (defun compute-init-name (pathname
121 &key
122 (kind (guess-kind pathname))
123 (prefix nil)
124 &aux
125 (unique-name (unique-init-name pathname)))
126 "Computes initialization function name. Libraries, FASLS and
127 programs init function names can't be randomized to allow
128 initialization from the C code which wants to use it."
129 (case kind
130 ((:object :c)
131 unique-name)
132 ((:fasl :fas)
133 (init-function-name "CODE" :kind :fas :prefix prefix))
134 ((:static-library :lib)
135 (init-function-name unique-name :kind :lib :prefix prefix))
136 ((:shared-library :dll)
137 (init-function-name unique-name :kind :dll :prefix prefix))
138 ((:program)
139 (concatenate 'string (or prefix "init_") "ECL_PROGRAM"))
140 (otherwise
141 (error "C::BUILDER cannot accept files of kind ~s" kind))))
142
143 (defun init-function-name (s &key (kind :object) (prefix nil))
144 (flet ((translate-char (c)
145 (cond ((and (char>= c #\a) (char<= c #\z))
146 (char-upcase c))
147 ((and (char>= c #\A) (char<= c #\Z))
148 c)
149 ((or (eq c #\-) (eq c #\_))
150 #\_)
151 ((eq c #\*)
152 #\x)
153 ((eq c #\?)
154 #\a)
155 ((digit-char-p c)
156 c)
157 (t
158 #\p)))
159 (disambiguation (kind)
160 (case kind
161 ((:object :c) "")
162 ((:fasl :fas) "fas_")
163 ((:library :static-library :lib) "lib_")
164 ((:shared-library :dll) "dll_")
165 ((:program) "exe_")
166 (otherwise (error "Not a valid argument to INIT-FUNCTION-NAME: kind = ~S"
167 kind)))))
168 (setq s (map 'string #'translate-char (string s)))
169 (concatenate 'string
170 (or prefix "init_")
171 (disambiguation kind)
172 (map 'string #'translate-char (string s)))))