2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes --- Maxwell Equations of Software
|
2020-12-30 20:20:19 +00:00
|
|
|
;;; Copyright © 2016,2017,2018,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2017-06-11 11:11:40 +00:00
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; This file is part of GNU Mes.
|
2017-06-11 11:11:40 +00:00
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes is free software; you can redistribute it and/or modify it
|
2017-06-11 11:11:40 +00:00
|
|
|
;;; 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.
|
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes is distributed in the hope that it will be useful, but
|
2017-06-11 11:11:40 +00:00
|
|
|
;;; 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
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
2017-06-11 11:11:40 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
;;; M1.scm produces stage0' M1 assembly format
|
2017-06-11 11:11:40 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
(define-module (mescc M1)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (system base pmatch)
|
|
|
|
#:use-module (mes misc)
|
|
|
|
#:use-module (mes guile)
|
|
|
|
|
|
|
|
#:use-module (mescc as)
|
|
|
|
#:use-module (mescc info)
|
|
|
|
#:export (info->M1
|
|
|
|
infos->M1
|
|
|
|
M1:merge-infos))
|
|
|
|
|
2020-12-23 13:38:38 +00:00
|
|
|
(define* (infos->M1 file-name infos #:key align verbose?)
|
2018-05-25 06:05:02 +00:00
|
|
|
(let ((info (fold M1:merge-infos (make <info>) infos)))
|
2020-12-23 13:38:38 +00:00
|
|
|
(info->M1 file-name info #:align align #:verbose? verbose?)))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (M1:merge-infos o info)
|
|
|
|
(clone info
|
|
|
|
#:functions (alist-add (.functions info) (.functions o))
|
2018-10-06 13:56:14 +00:00
|
|
|
#:globals (alist-add (.globals info) (.globals o))
|
|
|
|
#:types (.types o)))
|
2017-06-11 11:11:40 +00:00
|
|
|
|
|
|
|
(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))
|
|
|
|
|
2018-10-06 12:51:57 +00:00
|
|
|
(define (hex2:address8 o)
|
|
|
|
(string-append "&" o " %0")) ;; FIXME: 64bit
|
|
|
|
|
2017-06-25 07:33:55 +00:00
|
|
|
(define (hex2:offset o)
|
|
|
|
(string-append "%" o))
|
|
|
|
|
|
|
|
(define (hex2:offset1 o)
|
|
|
|
(string-append "!" o))
|
|
|
|
|
2020-06-06 17:51:45 +00:00
|
|
|
(define (hex2:offset2 o)
|
|
|
|
(string-append "@" o))
|
|
|
|
|
|
|
|
(define (hex2:offset3 o)
|
|
|
|
"Note: Uses architecture-specific printer (for branch instructions)"
|
|
|
|
(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
|
|
|
|
2018-08-15 16:26:55 +00:00
|
|
|
(define (hex2:immediate2 o)
|
|
|
|
(if hex? (string-append "@0x" (dec->hex o))
|
|
|
|
(string-append "@" (number->string o))))
|
|
|
|
|
|
|
|
(define (hex2:immediate4 o)
|
|
|
|
(if hex? (string-append "%0x" (dec->hex o))
|
|
|
|
(string-append "%" (number->string o))))
|
|
|
|
|
2018-10-06 12:51:57 +00:00
|
|
|
(define (hex2:immediate8 o)
|
2023-07-03 18:15:56 +00:00
|
|
|
(if hex? (string-append "%0x" (dec->hex (modulo o #x100000000))
|
2018-10-06 12:51:57 +00:00
|
|
|
" %0x" (if (< o 0) "-1"
|
2023-07-03 18:15:56 +00:00
|
|
|
(dec->hex (quotient o #x100000000))))
|
|
|
|
(string-append "%" (number->string (dec->hex (modulo o #x100000000)))
|
2018-10-06 12:51:57 +00:00
|
|
|
" %" (if (< o 0) "-1"
|
2023-07-03 18:15:56 +00:00
|
|
|
(number->string (dec->hex (quotient o #x100000000)))))))
|
2018-10-06 12:51:57 +00:00
|
|
|
|
2018-04-24 05:26:27 +00:00
|
|
|
(define* (display-join o #:optional (sep ""))
|
|
|
|
(let loop ((o o))
|
|
|
|
(when (pair? o)
|
|
|
|
(display (car o))
|
|
|
|
(if (pair? (cdr o))
|
|
|
|
(display sep))
|
|
|
|
(loop (cdr o)))))
|
|
|
|
|
2019-07-27 15:22:00 +00:00
|
|
|
(define (global-string? o)
|
|
|
|
(and (pair? o) (pair? (car o)) (eq? (caar o) #:string)))
|
|
|
|
|
|
|
|
(define (global-extern? o)
|
|
|
|
(and=> (global:storage o) (cut eq? <> 'extern)))
|
|
|
|
|
2020-12-23 13:38:38 +00:00
|
|
|
(define* (info->M1 file-name o #:key align verbose?)
|
2018-05-25 06:05:02 +00:00
|
|
|
(let* ((functions (.functions o))
|
2017-06-11 11:11:40 +00:00
|
|
|
(function-names (map car functions))
|
2018-05-25 06:05:02 +00:00
|
|
|
(globals (.globals o))
|
2019-07-27 15:22:00 +00:00
|
|
|
(globals (filter (negate (compose global-extern? cdr)) globals))
|
|
|
|
(strings (filter global-string? globals))
|
|
|
|
(strings (map car strings))
|
2020-12-23 13:38:38 +00:00
|
|
|
(reg-size (type:size (assoc-ref (.types o) "*")))
|
|
|
|
(align-functions? (memq 'functions align))
|
|
|
|
(align-globals? (memq 'globals align)))
|
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))
|
2018-08-15 16:26:55 +00:00
|
|
|
(if (equal? o "%0") o ; FIXME: 64b
|
|
|
|
(error "no such string:" o)))))
|
2017-07-02 14:25:14 +00:00
|
|
|
(define (text->M1 o)
|
2018-11-11 15:25:36 +00:00
|
|
|
;;
|
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
|
2018-05-04 18:45:27 +00:00
|
|
|
;; FIXME
|
2018-10-06 12:51:57 +00:00
|
|
|
((#:address (#:string ,string))
|
|
|
|
(hex2:address (string->label `(#:string ,string))))
|
2018-05-04 18:45:27 +00:00
|
|
|
((#:address (#:address ,address)) (guard (string? address))
|
|
|
|
(hex2:address address))
|
|
|
|
((#:address (#:address ,global)) (guard (global? global))
|
|
|
|
(hex2:address (global->string global)))
|
2018-05-11 17:46:26 +00:00
|
|
|
((#:address ,function) (guard (function? function))
|
|
|
|
(hex2:address (function->string function)))
|
|
|
|
((#:address ,number) (guard (number? number))
|
|
|
|
(string-join (map text->M1 (int->bv32 number))))
|
2018-10-06 12:51:57 +00:00
|
|
|
|
|
|
|
((#:address8 (#:string ,string))
|
|
|
|
(hex2:address8 (string->label `(#:string ,string))))
|
|
|
|
((#:address8 (#:address ,address)) (guard (string? address))
|
|
|
|
(hex2:address8 address))
|
|
|
|
((#:address8 (#:address ,global)) (guard (global? global))
|
|
|
|
(hex2:address8 (global->string global)))
|
|
|
|
((#:address8 ,function) (guard (function? function))
|
|
|
|
(hex2:address8 (function->string function)))
|
|
|
|
((#:address8 ,number) (guard (number? number))
|
|
|
|
(string-join (map text->M1 (int->bv64 number))))
|
|
|
|
|
2018-05-05 10:30:06 +00:00
|
|
|
((#:string ,string)
|
|
|
|
(hex2:address (string->label o)))
|
2018-10-06 12:51:57 +00:00
|
|
|
|
|
|
|
((#:address ,address) (guard (string? address))
|
|
|
|
(hex2:address address))
|
2018-05-05 10:30:06 +00:00
|
|
|
((#:address ,global) (guard (global? global))
|
|
|
|
(hex2:address (global->string global)))
|
2018-10-06 12:51:57 +00:00
|
|
|
|
|
|
|
((#:address8 ,address) (guard (string? address))
|
|
|
|
(hex2:address8 address))
|
|
|
|
((#:address8 ,global) (guard (global? global))
|
|
|
|
(hex2:address8 (global->string global)))
|
|
|
|
|
2018-05-04 18:45:27 +00:00
|
|
|
((#:offset ,offset) (hex2:offset offset))
|
|
|
|
((#:offset1 ,offset1) (hex2:offset1 offset1))
|
2020-06-06 17:51:45 +00:00
|
|
|
((#:offset2 ,offset2) (hex2:offset2 offset2))
|
|
|
|
((#:offset3 ,offset3) (hex2:offset3 offset3))
|
2018-05-04 18:45:27 +00:00
|
|
|
((#:immediate ,immediate) (hex2:immediate immediate))
|
2018-05-11 17:46:26 +00:00
|
|
|
((#:immediate1 ,immediate1) (hex2:immediate1 immediate1))
|
2018-08-15 16:26:55 +00:00
|
|
|
((#:immediate2 ,immediate2) (hex2:immediate2 immediate2))
|
|
|
|
((#:immediate4 ,immediate4) (hex2:immediate4 immediate4))
|
2018-10-06 12:51:57 +00:00
|
|
|
((#:immediate8 ,immediate8) (hex2:immediate8 immediate8))
|
2018-05-11 17:46:26 +00:00
|
|
|
(_ (error "text->M1 no match o" o))))
|
2018-11-11 15:25:36 +00:00
|
|
|
((pair? o) (string-join (map text->M1 o)))
|
|
|
|
(#t (error "no such text:" 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))
|
2018-05-04 11:58:27 +00:00
|
|
|
(text (function:text (cdr o))))
|
2017-07-02 14:25:14 +00:00
|
|
|
(define (line->M1 o)
|
|
|
|
(cond ((eq? (car o) #:label)
|
2018-04-23 04:56:35 +00:00
|
|
|
(display (string-append ":" (cadr o))))
|
2017-06-25 07:33:55 +00:00
|
|
|
((eq? (car o) #:comment)
|
2018-04-24 05:26:27 +00:00
|
|
|
(display "\t\t\t\t\t# ")
|
|
|
|
(display (text->M1 (cadr o))))
|
2017-07-02 14:25:14 +00:00
|
|
|
((or (string? (car o)) (symbol? (car o)))
|
2018-04-24 05:26:27 +00:00
|
|
|
(display "\t" )
|
|
|
|
(display-join (map text->M1 o) " "))
|
2020-06-06 17:51:45 +00:00
|
|
|
((or (string? (car (reverse o))) (symbol? (car (reverse o))))
|
|
|
|
(display "\t" )
|
|
|
|
(display-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))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when verbose?
|
|
|
|
(display (string-append " :" name "\n") (current-error-port)))
|
2020-12-23 13:38:38 +00:00
|
|
|
(display "\n\n")
|
|
|
|
(when align-functions?
|
|
|
|
;; "<" aligns to multiple of 4 Bytes.
|
|
|
|
(display "<\n"))
|
|
|
|
(display (string-append ":" 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))
|
2018-05-04 18:45:27 +00:00
|
|
|
(string? (not (equal? string-label "_string_#f"))))
|
|
|
|
(cond ((and (pair? o) (global? (cdr o))) (string-append "&" (global->string o)))
|
2020-12-30 20:20:19 +00:00
|
|
|
((and (not string?) (not function?)) (format (current-error-port) "warning: unresolved label: ~s\n" label))
|
2018-08-15 16:26:55 +00:00
|
|
|
((equal? string-label "%0") o) ;; FIXME: 64b
|
2018-05-04 18:45:27 +00:00
|
|
|
(else (string-append "&" label))))))
|
2018-05-21 19:54:54 +00:00
|
|
|
(define (display-align size)
|
2018-10-06 13:56:14 +00:00
|
|
|
(let ((alignment (- reg-size (modulo size reg-size))))
|
2020-12-23 13:38:38 +00:00
|
|
|
(when (and align-globals? (> reg-size alignment 0))
|
2018-05-21 19:54:54 +00:00
|
|
|
(display " ")
|
2018-10-06 13:56:14 +00:00
|
|
|
(display-join (map text->M1 (map (const 0) (iota alignment))) " "))
|
|
|
|
#t))
|
2018-05-04 18:45:27 +00:00
|
|
|
(let* ((label (cond
|
|
|
|
((and (pair? (car o)) (eq? (caar o) #:string))
|
|
|
|
(string->label (car o)))
|
|
|
|
((global? (cdr o)) (global->string (cdr o)))
|
|
|
|
(else (car o))))
|
2018-05-02 13:53:37 +00:00
|
|
|
(string? (string-prefix? "_string" label))
|
2019-07-27 07:51:21 +00:00
|
|
|
(foo (when (and verbose? (not (eq? (car (string->list label)) #\_)))
|
|
|
|
(display (string-append " :" label "\n") (current-error-port))))
|
2018-05-04 18:45:27 +00:00
|
|
|
(data ((compose global:value cdr) o))
|
2017-07-26 21:42:18 +00:00
|
|
|
(data (filter-map labelize data))
|
|
|
|
(len (length data))
|
2018-05-02 13:53:37 +00:00
|
|
|
(string-max (or (and=> (getenv "M1_STRING_MAX") string->number) 256))
|
|
|
|
(string-data (and string? (list-head data (1- (length data))))))
|
2018-04-23 04:56:35 +00:00
|
|
|
(display (string-append "\n:" label "\n"))
|
2018-05-02 13:53:37 +00:00
|
|
|
(if (and string-data
|
|
|
|
(< len string-max)
|
|
|
|
(char? (car data))
|
|
|
|
(eq? (last data) #\nul)
|
|
|
|
(not (find (cut memq <> '(#\")) string-data))
|
|
|
|
(not (any (lambda (ch)
|
|
|
|
(or (and (not (memq ch '(#\tab #\newline)))
|
|
|
|
(< (char->integer ch) #x20))
|
|
|
|
(>= (char->integer ch) #x80))) string-data)))
|
2018-05-21 19:54:54 +00:00
|
|
|
(let ((text string-data))
|
|
|
|
(display (string-append "\"" (list->string string-data) "\""))
|
|
|
|
(display-align (1+ (length string-data))))
|
|
|
|
(let ((text (map text->M1 data)))
|
|
|
|
(display-join text " ")
|
|
|
|
(display-align (length text))))
|
2017-06-11 11:11:40 +00:00
|
|
|
(newline)))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when verbose?
|
|
|
|
(display "M1: functions\n" (current-error-port)))
|
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-23 04:56:35 +00:00
|
|
|
(display "\n\n:ELF_data\n") ;; FIXME
|
|
|
|
(display "\n\n:HEX2_data\n"))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when verbose?
|
|
|
|
(display "M1: globals\n" (current-error-port)))
|
2019-07-27 15:22:00 +00:00
|
|
|
(for-each write-global (filter global-string? globals))
|
|
|
|
(for-each write-global (filter (negate global-string?) globals))))
|