2017-06-11 11:11:40 +00:00
|
|
|
;;; -*-scheme-*-
|
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
2017-11-21 18:22:26 +00:00
|
|
|
;;; Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2017-06-11 11:11:40 +00:00
|
|
|
;;;
|
|
|
|
;;; This file is part of Mes.
|
|
|
|
;;;
|
|
|
|
;;; Mes is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Mes is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2017-07-02 14:25:14 +00:00
|
|
|
;;; M1.mes produces stage0' M1 object format
|
2017-06-11 11:11:40 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(cond-expand
|
|
|
|
(guile)
|
|
|
|
(mes
|
|
|
|
(mes-use-module (srfi srfi-1))
|
2017-07-02 14:25:14 +00:00
|
|
|
(mes-use-module (srfi srfi-26))
|
mescc: Remove ELF creation, handled by hex2 now.
* module/language/c99/compiler.scm (make-global, global:type,
global:pointer, global:value): Move from elf-util.mes
* module/mes/as.mes: New file.
* module/mes/as-i386.mes: Use it.
* module/mes/as-i386.scm: Use it.
* module/mes/elf-util.mes: Remove.
* module/mes/elf.mes (elf32-addr, elf32-half, elf32-off, elf32-word,
make-elf, write-any, object->elf): Remove
(hex2->elf): New function with dummy implementation.
* module/mes/elf.scm: Update exports.
* module/mes/hex2.mes (object->elf): New function.
* module/mes/hex2.scm: Export it.
2017-06-25 07:26:25 +00:00
|
|
|
(mes-use-module (mes as))
|
2017-06-11 11:11:40 +00:00
|
|
|
(mes-use-module (mes elf))
|
2017-06-25 07:33:55 +00:00
|
|
|
(mes-use-module (mes optargs))
|
mescc: Use records for Guile: preparation.
* module/language/c99/info.mes: New file.
* module/mes/M1.mes: Use it.
* scripts/mescc.mes: Use it.
* module/language/c99/compiler.mes: Use it. (<info>, <types>,
<constants>, <functions>, <globals>, <locals>, <function>, <text>,
<break>, <continue>, make, info?, .info, .types, .constants,
.functions, .globals, .locals, .function, .text, .break, .continue):
Remove.
* module/language/c99/info.scm: New file.
* module/language/c99/compiler.scm: Use it.
* guile/mescc.scm: Use it.
* module/mes/M1.scm: Use it.
2017-07-14 18:42:26 +00:00
|
|
|
(mes-use-module (mes pmatch))
|
|
|
|
(mes-use-module (language c99 info))))
|
2017-06-11 11:11:40 +00:00
|
|
|
|
|
|
|
(define (logf port string . rest)
|
|
|
|
(apply format (cons* port string rest))
|
|
|
|
(force-output port)
|
|
|
|
#t)
|
|
|
|
|
|
|
|
(define (stderr string . rest)
|
|
|
|
(apply logf (cons* (current-error-port) string rest)))
|
|
|
|
|
2017-07-02 14:25:14 +00:00
|
|
|
(define (objects->M1 objects)
|
|
|
|
((compose object->M1 merge-objects) objects))
|
2017-06-11 11:11:40 +00:00
|
|
|
|
mescc: Remove ELF creation, handled by hex2 now.
* module/language/c99/compiler.scm (make-global, global:type,
global:pointer, global:value): Move from elf-util.mes
* module/mes/as.mes: New file.
* module/mes/as-i386.mes: Use it.
* module/mes/as-i386.scm: Use it.
* module/mes/elf-util.mes: Remove.
* module/mes/elf.mes (elf32-addr, elf32-half, elf32-off, elf32-word,
make-elf, write-any, object->elf): Remove
(hex2->elf): New function with dummy implementation.
* module/mes/elf.scm: Update exports.
* module/mes/hex2.mes (object->elf): New function.
* module/mes/hex2.scm: Export it.
2017-06-25 07:26:25 +00:00
|
|
|
(define (object->elf o)
|
2017-07-02 14:25:14 +00:00
|
|
|
((compose M1->elf object->M1) o))
|
mescc: Remove ELF creation, handled by hex2 now.
* module/language/c99/compiler.scm (make-global, global:type,
global:pointer, global:value): Move from elf-util.mes
* module/mes/as.mes: New file.
* module/mes/as-i386.mes: Use it.
* module/mes/as-i386.scm: Use it.
* module/mes/elf-util.mes: Remove.
* module/mes/elf.mes (elf32-addr, elf32-half, elf32-off, elf32-word,
make-elf, write-any, object->elf): Remove
(hex2->elf): New function with dummy implementation.
* module/mes/elf.scm: Update exports.
* module/mes/hex2.mes (object->elf): New function.
* module/mes/hex2.scm: Export it.
2017-06-25 07:26:25 +00:00
|
|
|
|
2017-06-11 11:11:40 +00:00
|
|
|
(define (objects->elf objects)
|
2017-07-02 14:25:14 +00:00
|
|
|
((compose M1->elf object->M1 merge-objects) objects))
|
2017-06-11 11:11:40 +00:00
|
|
|
|
|
|
|
(define (merge-objects objects)
|
|
|
|
(let loop ((objects (cdr objects)) (object (car objects)))
|
|
|
|
(if (null? objects) object
|
|
|
|
(loop (cdr objects)
|
|
|
|
`((functions . ,(alist-add (assoc-ref object 'functions) (assoc-ref (car objects) 'functions)))
|
|
|
|
(globals . ,(alist-add (assoc-ref object 'globals) (assoc-ref (car objects) 'globals))))))))
|
|
|
|
|
|
|
|
(define (alist-add a b)
|
|
|
|
(let* ((b-keys (map car b))
|
|
|
|
(a (filter (lambda (f) (or (cdr f) (not (member (car f) b-keys)))) a))
|
|
|
|
(a-keys (map car a)))
|
|
|
|
(append a (filter (lambda (e) (not (member (car e) a-keys))) b))))
|
|
|
|
|
2017-06-25 07:33:55 +00:00
|
|
|
(define (hex2:address o)
|
|
|
|
(string-append "&" o))
|
|
|
|
|
|
|
|
(define (hex2:offset o)
|
|
|
|
(string-append "%" o))
|
|
|
|
|
|
|
|
(define (hex2:offset1 o)
|
|
|
|
(string-append "!" o))
|
|
|
|
|
2017-07-09 16:40:12 +00:00
|
|
|
(define hex? #t)
|
2017-07-12 17:41:31 +00:00
|
|
|
|
2017-07-02 14:25:14 +00:00
|
|
|
(define (hex2:immediate o)
|
2017-07-12 17:41:31 +00:00
|
|
|
(if hex? (string-append "%0x" (dec->hex o))
|
2017-12-09 19:10:57 +00:00
|
|
|
(string-append "%" (number->string o))))
|
2017-07-02 14:25:14 +00:00
|
|
|
|
|
|
|
(define (hex2:immediate1 o)
|
2017-07-12 17:41:31 +00:00
|
|
|
(if hex? (string-append "!0x" (dec->hex o))
|
2017-12-09 19:10:57 +00:00
|
|
|
(string-append "!" (number->string o))))
|
2017-07-02 14:25:14 +00:00
|
|
|
|
|
|
|
(define (object->M1 o)
|
2018-04-07 08:24:57 +00:00
|
|
|
(stderr "dumping M1: object\n")
|
2017-06-11 11:11:40 +00:00
|
|
|
(let* ((functions (assoc-ref o 'functions))
|
|
|
|
(function-names (map car functions))
|
2017-07-29 07:08:44 +00:00
|
|
|
(file-name (car (or (assoc-ref o 'file-names) function-names)))
|
2017-06-11 11:11:40 +00:00
|
|
|
(globals (assoc-ref o 'globals))
|
|
|
|
(global-names (map car globals))
|
2017-06-11 16:05:56 +00:00
|
|
|
(strings (filter (lambda (g) (and (pair? g) (eq? (car g) #:string))) global-names)))
|
2017-06-11 11:11:40 +00:00
|
|
|
(define (string->label o)
|
2017-06-25 07:33:55 +00:00
|
|
|
(let ((index (list-index (lambda (s) (equal? s o)) strings)))
|
2017-12-09 19:10:57 +00:00
|
|
|
(if index
|
|
|
|
(string-append "_string_" file-name "_" (number->string index))
|
|
|
|
"")))
|
2017-07-02 14:25:14 +00:00
|
|
|
(define (text->M1 o)
|
2017-12-09 19:10:57 +00:00
|
|
|
(cond
|
|
|
|
((char? o) (text->M1 (char->integer o)))
|
|
|
|
((string? o) o)
|
|
|
|
((symbol? o) (symbol->string o))
|
|
|
|
((number? o) (let ((o (if (< o #x80) o (- o #x100))))
|
|
|
|
(if hex? (string-append "!0x"
|
|
|
|
(if (and (>= o 0) (< o 16)) "0" "")
|
|
|
|
(number->string o 16))
|
|
|
|
(string-append "!" (number->string o)))))
|
|
|
|
((and (pair? o) (keyword? (car o)))
|
|
|
|
(pmatch o
|
|
|
|
;; FIXME
|
|
|
|
((#:address (#:string ,string)) (hex2:address (string->label `(#:string ,string))))
|
|
|
|
((#:string (#:address ,address)) (hex2:address address))
|
|
|
|
((#:address (#:address ,address)) (hex2:address address))
|
|
|
|
((#:string ,string) (hex2:address (string->label o)))
|
|
|
|
((#:address ,address) (hex2:address address))
|
|
|
|
((#:offset ,offset) (hex2:offset offset))
|
|
|
|
((#:offset1 ,offset1) (hex2:offset1 offset1))
|
|
|
|
((#:immediate ,immediate) (hex2:immediate immediate))
|
|
|
|
((#:immediate1 ,immediate1) (hex2:immediate1 immediate1))))
|
|
|
|
((pair? o) (string-join (map text->M1 o)))))
|
2017-06-11 11:11:40 +00:00
|
|
|
(define (write-function o)
|
2017-06-25 07:33:55 +00:00
|
|
|
(let ((name (car o))
|
|
|
|
(text (cdr o)))
|
2017-07-02 14:25:14 +00:00
|
|
|
(define (line->M1 o)
|
|
|
|
(cond ((eq? (car o) #:label)
|
2017-12-09 19:10:57 +00:00
|
|
|
(core:display (string-append ":" (cadr o))))
|
2017-06-25 07:33:55 +00:00
|
|
|
((eq? (car o) #:comment)
|
2017-12-09 19:10:57 +00:00
|
|
|
(core:display (string-append "\t\t\t\t\t# " (text->M1 (cadr o)))))
|
2017-07-02 14:25:14 +00:00
|
|
|
((or (string? (car o)) (symbol? (car o)))
|
2017-12-09 19:10:57 +00:00
|
|
|
(core:display (string-append "\t" (string-join (map text->M1 o) " "))))
|
2017-07-02 14:25:14 +00:00
|
|
|
(else (error "line->M1 invalid line:" o)))
|
2017-06-25 07:33:55 +00:00
|
|
|
(newline))
|
2018-04-07 08:24:57 +00:00
|
|
|
(core:display-error (string-append " :" name "\n"))
|
2017-12-09 19:10:57 +00:00
|
|
|
(core:display (string-append "\n\n:" name "\n"))
|
2017-07-02 14:25:14 +00:00
|
|
|
(for-each line->M1 (apply append text))))
|
2017-06-11 11:11:40 +00:00
|
|
|
(define (write-global o)
|
2017-06-12 19:00:50 +00:00
|
|
|
(define (labelize o)
|
|
|
|
(if (not (string? o)) o
|
|
|
|
(let* ((label o)
|
|
|
|
(function? (member label function-names))
|
|
|
|
(string-label (string->label label))
|
2017-12-02 13:04:41 +00:00
|
|
|
(string? (not (equal? string-label "_string_#f")))
|
2017-06-12 19:00:50 +00:00
|
|
|
(global? (member label global-names)))
|
2017-12-09 19:10:57 +00:00
|
|
|
(if (or global? string?) (string-append "&" label)
|
2017-06-12 19:00:50 +00:00
|
|
|
(begin (if (not function?) (stderr "warning: unresolved label: ~s\n" label))
|
2017-12-09 19:10:57 +00:00
|
|
|
(string-append "&" label))))))
|
2017-06-12 19:00:50 +00:00
|
|
|
(let* ((label (if (not (and (pair? (car o)) (eq? (caar o) #:string))) (car o)
|
2017-12-09 19:10:57 +00:00
|
|
|
(string->label (car o))))
|
2017-06-12 19:00:50 +00:00
|
|
|
(data (cdr o))
|
2017-07-26 21:42:18 +00:00
|
|
|
(data (filter-map labelize data))
|
|
|
|
(len (length data))
|
2017-08-26 11:40:30 +00:00
|
|
|
(string-max (or (and=> (getenv "M1_STRING_MAX") string->number) 80)))
|
2018-04-07 08:24:57 +00:00
|
|
|
(if (not (eq? (car (string->list label)) #\_))
|
|
|
|
(core:display-error (string-append " :" label "\n")))
|
|
|
|
(core:display (string-append "\n:" label "\n"))
|
2017-07-26 21:42:18 +00:00
|
|
|
(cond ((and (< len string-max)
|
|
|
|
(char? (car data))
|
|
|
|
(eq? (last data) #\nul)
|
2017-07-30 12:28:19 +00:00
|
|
|
(not (find (cut memq <> '(#\nul #\backspace #\return #\" #\')) (list-head data (1- (length data)))))
|
|
|
|
(not (any (lambda (ch) (>= (char->integer ch) #x80)) data)))
|
2018-04-07 08:24:57 +00:00
|
|
|
(core:display (string-append "\"" (list->string (list-head data (1- (length data)))) "\"")))
|
|
|
|
(else (core:display (string-join (map text->M1 data) " "))))
|
2017-06-11 11:11:40 +00:00
|
|
|
(newline)))
|
2018-04-07 08:24:57 +00:00
|
|
|
(core:display-error "M1: functions\n")
|
2017-06-11 11:11:40 +00:00
|
|
|
(for-each write-function (filter cdr functions))
|
2017-09-10 14:59:43 +00:00
|
|
|
(when (assoc-ref functions "main")
|
2018-04-07 08:24:57 +00:00
|
|
|
(core:display "\n\n:ELF_data\n") ;; FIXME
|
|
|
|
(core:display "\n\n:HEX2_data\n"))
|
|
|
|
(core:display-error "M1: globals\n")
|
2017-06-11 11:11:40 +00:00
|
|
|
(for-each write-global globals)))
|