Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

View file

@ -0,0 +1,339 @@
;; ╔──────────────────────────────────────────────────────────────────╗
;; │ To the extent possible under law, Justine Tunney has waived │
;; │ all copyright and related or neighboring rights to this file, │
;; │ as it is written in the following disclaimers: │
;; │ • http://unlicense.org/ │
;; │ • http://creativecommons.org/publicdomain/zero/1.0/ │
;; ╚──────────────────────────────────────────────────────────────────╝
;; Reconfigures GNU Emacs syntax highlighting for GNU Assembler syntax.
(require 'asm-mode)
(defun cosmo-regexpify (x)
(let ((join (lambda (sep lis)
(mapconcat 'identity lis sep))))
(cond ((vectorp x)
(funcall join "" (mapcar 'cosmo-regexpify x)))
((listp x)
(concat "\\(?:"
(funcall join "\\|" (mapcar 'cosmo-regexpify x))
"\\)"))
('t x))))
(defun cosmo-fontify (faces regexp limit predicate)
"Set FACES on REGEXP matches until LIMIT that satisfy PREDICATE."
(while (re-search-forward regexp limit t)
(let (i (face (save-excursion
(backward-char)
(face-at-point)))
(syntax (syntax-ppss)))
(when (funcall predicate face syntax)
(dolist (i faces)
(put-text-property (match-beginning (car i))
(match-end (car i))
'face
(cdr i)))))))
(defconst asm-mode-gas-qualifiers-regexp
(cosmo-regexpify
[("comdat"
":req"
":vararg"
["@" ("function"
"object"
"got"
"size"
"gotoff"
"plt"
"pltoff"
"gotpcrel"
"progbits"
"nobits"
"init_array"
"fini_array")])
"\\>"])
"GNU Assembler section, relocation, macro param qualifiers.")
(defconst asm-mode-doctag-regexp
(cosmo-regexpify
'(["<"
("p" "br"
"b" "/b"
"i" "/i"
"pre" "/pre"
"h3" "h4" "/h3" "/h4"
"ul" "ol" "li" "/ol" "/ul"
"dl" "dt" "dd" "/dl"
"table" "tr" "td" "th" "td" "/table")
">"]
["@"
("param"
"return"
"define"
"see"
"throws"
"returnstwice"
"fileoverview"
"nullable"
"noreturn"
"domain"
"clob"
"since"
"forcealignargpointer"
"mode"
"speed"
"cost"
"todo"
"assume"
"define"
"domain"
"code"
"note"
"protip"
"nxbitsafe"
"preinitsafe"
"asyncsignalsafe"
"notasyncsignalsafe"
"isa"
"sideffect")
"\\>"]))
"Assembly docstring highlighting in Google Java Style.")
(defconst asm-mode-x86-prefix-ops-regexp
(cosmo-regexpify
[(["[lL][oO][cC][kK]\\>[ \t]*"
("[mM][oO][vV][sS]"
"[aA][dD][dDcC]"
"[sS][uUbB][bB]"
"[oO][rR]"
"[aA][nN][dD]"
"[xX][oO][rR]"
"[nN][oO][tT]"
"[nN][eE][gG]"
"[iI][nN][cC]"
"[dD][eE][cC]"
"[bB][tT][sSrRcC]?"
"[xX][aA][dD][dD]"
"[xX][cC][hH][gG]"
["[cC][mM][pP][xX][cC][hH][gG]" ("8[bB]" "16[bB]") "?"])]
["[rR][eE][pP]" ("[eEzZ]" "[nN][eEzZ]") "?\\>[ \t]*"
("[mM][oO][vV][sS]"
"[sS][tT][oO][sS]"
"[sS][cC][aA][sS]"
"[cC][mM][pP][sS]"
"[nN][oO][pP]"
"[rR][eE][tT]"
"[iI][nN][sS]"
"[oO][uU][tT][sS]")])
"[bBwWlLqQ]?\\>"])
"Legal high-level 80x86 prefix instruction combinations.")
(defconst cosmo-asm-font-lock-keywords
(append
`(;; AT&T Fortran-Style Assembler Comment
;;
;; - Valid
;;
;; * /heyho
;; * //heyho
;;
;; - Ignored
;;
;; *  /heyho
;; * code code //heyho
;;
("^/.*$" . font-lock-comment-face)
;; Immediate Argument
;;
;; - Valid
;;
;; * mov $2,%eax
;; * mov $~2+0x2ul,%eax
;; * mov $'c,%eax
;; * mov $'\n,%eax
;;
("[ \t]\\$\\(\\(?:'\\(?:'\\|\\s\"\\|\\s\\.\\|.\\)\\|\\(?:0x[[:xdigit:]]+\\|0b[01]+\\|[1-9][0-9]*\\|0[0-7]*\\)\\(?:[fb]\\|u?l?l?\\)\\|[-*/&^|()%<>~+]\\|[_.[:alpha:]][-_.[:alnum:]]*\\)+\\)"
1 font-lock-constant-face)
(cosmo-asm-doctag-keywords)
;; Static Branch Prediction
;;
;; - Valid
;;
;; * jnz,pt 1f
;; * jnz,pn 1b
;;
;; - Traditionally Implied
;;
;; * jnz,pn 1f
;; * jnz,pt 1b
;;
(",p[tn]" . font-lock-keyword-face)
("\\\\\\(?:@\\|()\\)" . font-lock-function-name-face)
("\\\\\\(\\sw\\|_\\|\\.\\)+\\>" . font-lock-variable-name-face)
(,asm-mode-x86-prefix-ops-regexp . font-lock-keyword-face)
(,(concat "^\\(\\(?:\\sw\\|\\s_\\|\\.\\)+\\):[ \t]*"
"\\(\\(?:\\sw\\|\\.\\)+\\)?")
(1 font-lock-function-name-face)
(2 font-lock-keyword-face nil t))
("^\\((\\sw+)\\)?\\s +\\(\\(\\.?\\sw\\|\\s_\\)+\\(\\.\\sw+\\)*\\)"
2 font-lock-keyword-face)
("^\\(\\.\\(\\sw\\|\\s_\\|\\.\\)+\\)\\>[^:]?"
1 font-lock-keyword-face)
("^.*?\\*/\\(\\.\\(\\sw\\|\\s_\\|\\.\\)+\\)\\>[^:]?"
1 font-lock-keyword-face)
;; it's complicated
(,asm-mode-gas-qualifiers-regexp
. font-lock-type-face))
cpp-font-lock-keywords
'(;; GNU-Style Assembler Comment (Ltd. 80x86 &c.)
;;
;; - Valid
;;
;; * #heyho
;; * # heyho
;; * .org . #heyho
;; * .org . ####euhhcue
;; * .org .# ###euhhcue
;;
;; - Ignored
;;
;; * #if 0
;; * #endif
;; * .ascii "#heyho"
;;
("\\(#.*\\)$" 1 font-lock-comment-face)
("'\\(\\\\?.\\)\\>" 1 font-lock-constant-face)
;; Register Value
;;
;; - Valid
;;
;; * %al
;; * %eax
;;
;; - Ignored
;;
;; * %%al
;;
("%\\sw+" . font-lock-variable-name-face)
;; Hexadecimal Literal
;;
;; - Valid
;;
;; * 0x0123456789abcdef
;; * 0XDEADBEEFu
;; * -0xdeadbeefULL
;;
;; - Ignored
;;
;; * 0x0123456789abcdefg
;;
("\\b\\(0[xX]\\)[[:xdigit:]]+\\([ulUL]*\\)\\b"
(1 font-lock-constant-face)
(2 font-lock-constant-face))
;; Binary Literal
;;
;; - Valid
;;
;; * 0b0101101101
;; * 0B0101101101u
;; * -0b0101101101ULL
;;
;; - Ignored
;;
;; * 0b012
;;
("\\b\\(0[bB]\\)[01]+\\([ulUL]*\\)\\b"
(1 font-lock-constant-face)
(2 font-lock-constant-face))
;; Octal Literal
;;
;; - Valid
;;
;; * 01234567
;; * 01234567l
;; * -01234567ULL
;;
;; - Ignored
;;
;; * 012345678
;;
("\\b\\(0\\)[0-7]+\\([ulUL]*\\)\\b"
(1 font-lock-constant-face)
(2 font-lock-constant-face))
;; Decimal Literal
;;
;; - Valid
;;
;; * 123456789
;; * 123456789l
;; * -01234567ULL
;;
;; - Ignored
;;
;; * 123456789a
;; * 123456789aul
;;
("\\b[1-9][0-9]+\\([ulUL]*\\)\\b"
1 font-lock-constant-face)
;; AT&T Fortran-Style Assembler Comment
;;
;; - Valid
;;
;; * /heyho
;; * //heyho
;;
;; - Ignored
;;
;; *  /heyho
;; * code code //heyho
;;
("^/.*$" . font-lock-comment-face)
;; AT&T-Style Directional Label
;;
;; - Valid
;;
;; * 1f
;; * 99b
;;
("\\b\\([0-9]+\\)[fb]\\b" 1 font-lock-function-name-face)))
"Additional expressions to highlight in Assembler mode.")
(defun cosmo-asm-doctag-keywords (limit)
(cosmo-fontify '((0 . font-lock-constant-face))
asm-mode-doctag-regexp
limit
(lambda (face syntax)
(or (memq face '(font-lock-comment-face))
(and syntax
(nth 4 syntax))))))
(defun cosmo-asm-supplemental-hook ()
"GNU Assembly in Bell Laboratories Style."
(setq asm-comment-char ) ;; Was ESR using TASM?
(font-lock-add-keywords 'asm-mode
'((cosmo-asm-doctag-keywords))
'append)
(set (make-local-variable 'require-final-newline) nil)
(set (make-local-variable 'indent-tabs-mode) t)
(set (make-local-variable 'tab-width) 8))
(progn
(add-hook 'asm-mode-hook 'cosmo-asm-supplemental-hook)
(setq asm-font-lock-keywords cosmo-asm-font-lock-keywords))
(provide 'cosmo-asm-mode)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,232 @@
(defconst cosmo-c-constants-regex
(let (
(c11-constants
'("__func__"
"__STDC_IEC_559__"
"__VA_ARGS__"
"__FILE__"
"__LINE__"
"__DATE__"
"__STDC__"
"__STDC_HOSTED__"
"__STDC_VERSION__"
"__TIME__"
"__STDC_ISO_10646__"
"__STDC_MB_MIGHT_NEQ_WC__"
"__STDC_UTF_16__"
"__STDC_UTF_32__"
"__STDC_ANALYZABLE__"
"__STDC_IEC_559_COMPLEX__"
"__STDC_LIB_EXT1__"
"__STDC_NO_ATOMICS__"
"__STDC_NO_COMPLEX__"
"__STDC_NO_THREADS__"
"__STDC_NO_VLA__"
"__STDC_WANT_LIB_EXT1__"))
(gcc-cpp-412-constants
'("__BASE_FILE__"
"__CHAR_BIT__"
"__FUNCTION__"
"__GNUC_MINOR__"
"__GNUC_PATCHLEVEL__"
"__GNUC__"
"__GNUG__"
"__INCLUDE_LEVEL__"
"__INTMAX_MAX__"
"__INT_MAX__"
"__LONG_LONG_MAX__"
"__LONG_MAX__"
"__SCHAR_MAX__"
"__SHRT_MAX__"
"__DBL_MIN__"
"__DBL_MAX__"
"__FLT_MIN__"
"__FLT_MAX__"
"__WCHAR_MAX__"
"__WCHAR_UNSIGNED__"))
(gcc-cpp-92-constants
'("__x86_64__"
"__amd64__"
"__WINT_MAX__"
"__BIGGEST_ALIGNMENT__"
"__SIZE_MAX__"
"__PTRDIFF_MAX__"
"__UINTMAX_MAX__"
"__SIG_ATOMIC_MAX__"
"__INT8_MAX__"
"__INT16_MAX__"
"__INT32_MAX__"
"__INT64_MAX__"
"__UINT8_MAX__"
"__UINT16_MAX__"
"__UINT32_MAX__"
"__UINT64_MAX__"
"__INT_LEAST8_MAX__"
"__INT_LEAST16_MAX__"
"__INT_LEAST32_MAX__"
"__INT_LEAST64_MAX__"
"__UINT_LEAST8_MAX__"
"__UINT_LEAST16_MAX__"
"__UINT_LEAST32_MAX__"
"__UINT_LEAST64_MAX__"
"__INT_FAST8_MAX__"
"__INT_FAST16_MAX__"
"__INT_FAST32_MAX__"
"__INT_FAST64_MAX__"
"__UINT_FAST8_MAX__"
"__UINT_FAST16_MAX__"
"__UINT_FAST32_MAX__"
"__UINT_FAST64_MAX__"
"__INTPTR_MAX__"
"__UINTPTR_MAX__"
"__WCHAR_MIN__"
"__WINT_MIN__"
"__SIG_ATOMIC_MIN__"
"__SCHAR_WIDTH__"
"__SHRT_WIDTH__"
"__INT_WIDTH__"
"__LONG_WIDTH__"
"__LONG_LONG_WIDTH__"
"__PTRDIFF_WIDTH__"
"__SIG_ATOMIC_WIDTH__"
"__SIZE_WIDTH__"
"__WCHAR_WIDTH__"
"__WINT_WIDTH__"
"__INT_LEAST8_WIDTH__"
"__INT_LEAST16_WIDTH__"
"__INT_LEAST32_WIDTH__"
"__INT_LEAST64_WIDTH__"
"__INT_FAST8_WIDTH__"
"__INT_FAST16_WIDTH__"
"__INT_FAST32_WIDTH__"
"__INT_FAST64_WIDTH__"
"__INTPTR_WIDTH__"
"__INTMAX_WIDTH__"
"__SIZEOF_INT__"
"__SIZEOF_INTMAX__"
"__SIZEOF_UINTMAX__"
"__SIZEOF_LONG__"
"__SIZEOF_LONG_LONG__"
"__SIZEOF_SHORT__"
"__SIZEOF_POINTER__"
"__SIZEOF_FLOAT__"
"__SIZEOF_DOUBLE__"
"__SIZEOF_LONG_DOUBLE__"
"__SIZEOF_SIZE_T__"
"__SIZEOF_WCHAR_T__"
"__SIZEOF_WINT_T__"
"__SIZEOF_PTRDIFF_T__"
"__TIMESTAMP__"))
(limits
'("SIZEOF_SHORT"
"SIZEOF_INT"
"SIZEOF_LONG"
"SIZEOF_LONG_LONG"
"SIZEOF_POINTER"
"SIZEOF_PTRDIFF_T"
"SIZEOF_SIZE_T"
"SIZEOF_WCHAR_T"
"SIZEOF_WINT_T"
"SIZEOF_FLOAT"
"SIZEOF_FLOAT128"
"SIZEOF_DOUBLE"
"SIZEOF_FLOAT80"
"SIZEOF_LONG_DOUBLE"
"SIZEOF_INTMAX"
"SCHAR_MAX"
"SHRT_MAX"
"INT_MAX"
"LONG_MAX"
"LLONG_MAX"
"LONG_LONG_MAX"
"SIZE_MAX"
"INT8_MAX"
"INT16_MAX"
"INT32_MAX"
"INT64_MAX"
"WINT_MAX"
"WCHAR_MAX"
"INTPTR_MAX"
"PTRDIFF_MAX"
"SCHAR_MIN"
"SHRT_MIN"
"UINT_MIN"
"INT_MIN"
"LONG_MIN"
"LLONG_MIN"
"LONG_LONG_MIN"
"SIZE_MIN"
"INT8_MIN"
"INT16_MIN"
"INT32_MIN"
"INT64_MIN"
"INTMAX_MIN"
"INTPTR_MIN"
"WINT_MIN"
"WCHAR_MIN"
"PTRDIFF_MIN"
"USHRT_MAX"
"UINT_MAX"
"ULONG_MAX"
"ULLONG_MAX"
"ULONG_LONG_MAX"
"UINTPTR_MAX"
"UINT8_MAX"
"UINT16_MAX"
"UINT32_MAX"
"UINT64_MAX"
"USHRT_MIN"
"ULONG_MIN"
"ULLONG_MIN"
"ULONG_LONG_MIN"
"UINT8_MIN"
"UINT16_MIN"
"UINT32_MIN"
"UINT64_MIN"
"UINTMAX_MIN"
"UINTPTR_MIN"
"MB_CUR_MAX"
"MB_LEN_MAX"
"INTMAX_MAX"
"UINTMAX_MAX"
"INTMAX_MAX"
"UINTMAX_MAX"
"DBL_MIN"
"DBL_MAX"
"FLT_MIN"
"FLT_MAX"))
(cosmopolitan-constants
'("__SAUCE__"
"PAGESIZE"
"FRAMESIZE"
"BIGPAGESIZE"
"ENV_MAX"
"ARG_MAX"
"CMD_MAX"
"PATH_MAX"
"BUFSIZ"
"CACHELINE"
"CHAR_BIT"
"NAME_MAX"
"NSIG"
"CHILD_MAX"
"OPEN_MAX"
"ATEXIT_MAX"
"__x86__"
"__i386__"))
)
(concat "\\_<"
(regexp-opt (append c11-constants
gcc-cpp-412-constants
gcc-cpp-92-constants
cosmopolitan-constants
limits))
"\\_>")))
(provide 'cosmo-c-constants)

View file

@ -0,0 +1,315 @@
(defconst cosmo-c-keywords-regex
(let (
;; (kar
;; '("case"
;; "do"
;; "return"
;; "struct"
;; "for"
;; "default"
;; "auto"
;; "while"
;; "else"
;; "break"
;; "union"
;; "switch"
;; "continue"
;; "extern"
;; "sizeof"
;; "if"
;; "goto"))
;; (ansi
;; '("static"
;; "sizeof"
;; "if"
;; "typedef"
;; "const"
;; "struct"
;; "for"
;; "union"
;; "switch"
;; "volatile"
;; "do"
;; "return"
;; "goto"
;; "auto"
;; "enum"
;; "else"
;; "break"
;; "extern"
;; "case"
;; "default"
;; "register"
;; "while"
;; "continue"))
(c99
'("inline"
"restrict"
"_Imaginary"
"_Bool"
"_Pragma"
"_Complex"))
(c11
'("_Atomic"
"_Alignas"
"_Alignof"
"_Noreturn"
"_Generic"
"_Thread_local"
"_Static_assert"
"_Complex_I"
"_Imaginary_I"))
;; (cxx17
;; '("this"
;; "thread_local"
;; "private"
;; "catch"
;; "export"
;; "operator"
;; "sizeof"
;; "dynamic_cast"
;; "static_assert"
;; "const_cast"
;; "const"
;; "for"
;; "static_cast"
;; "union"
;; "namespace"
;; "switch"
;; "virtual"
;; "class"
;; "alignas"
;; "continue"
;; "volatile"
;; "template"
;; "mutable"
;; "if"
;; "public"
;; "friend"
;; "do"
;; "inline"
;; "return"
;; "goto"
;; "alignof"
;; "auto"
;; "enum"
;; "typedef"
;; "else"
;; "break"
;; "constexpr"
;; "new"
;; "extern"
;; "using"
;; "throw"
;; "asm"
;; "case"
;; "typeid"
;; "decltype"
;; "reinterpret_cast"
;; "default"
;; "noexcept"
;; "register"
;; "nullptr"
;; "try"
;; "typename"
;; "while"
;; "protected"
;; "static"
;; "explicit"
;; "delete"))
(cosmo
'("__rbx"
"__msabi"
"microarchitecture"
"targetclones"
"winstruct"
"testonly"
"forcealignargpointer"
"textexit"
"externinline"
"noinline"
"donothing"
"byanymeansnecessary"
"threadlocal"
"printfesque"
"flattenout"
"mallocesque"
"vallocesque"
"null"
"paramsnonnull"
"returnspointerwithnoaliases"
"alignas"
"nosideeffect"
"decltype"
"forceinline"
"nocallersavedregisters"
"nothrow"
"nooptimize"
"optimizesize"
"optimizespeed"
"alignof"
"relegated"
"antiquity"
"memcpyesque"
"libcesque"
"artificial"
"returnstwice"
"returnsaligned"
"noprune"
"reallocesque"
"nullterminated"
"unreachable"
"hidden"
"privileged"
"hasatleast"
"nodebuginfo"
"frownedupon"
"noreturn"
"initarray"
"mayalias"
"noinstrument"
"interruptfn"
"nocallback"
"textstartup"
"warnifused"
"attributeallocsize"
"attributeallocalign"
"nodiscard"
"nointerpose"
"compatfn"
"returnsnonnull"
"strftimeesque"
"firstclass"
"preinitarray"
"scanfesque"
"pureconst"
"thatispacked"
"strlenesque"
"textwindows"
"aligned"
"typeof"
"textreal"
"autotype"
"_Section"
"_Vector_size"))
(gnu
'("__inline"
"__alignof"
"__typeof"
"__restrict"
"__const__"
"__label__"
"__noinline__"
"__noclone__"
"__force_align_arg_pointer__"
"__always_inline__"
"__gnu_inline__"
"__alignof__"
"__asm__"
"__attribute__"
"__auto_type"
"__byte__"
"__complex__"
"__imag__"
"__may_alias__"
"__noreturn__"
"__packed__"
"__pointer__"
"__printf__"
"__real__"
"__restrict__"
"__scanf__"
"__strfmon__"
"__strftime__"
"__strong__"
"__target__"
"__transparent_union__"
"__typeof__"
"__volatile__"
"__word__"
"__alias__"
"__aligned__"
"__alloc_align__"
"__alloc_size__"
"__artificial__"
"__assume_aligned__"
"__cold__"
"__constructor__"
"__destructor__"
"__copy__"
"__deprecated__"
"__error__"
"__warning__"
"__externally_visible__"
"__flatten__"
"__format__"
"__gnu_format__"
"__gnu_printf__"
"__gnu_scanf__"
"__format_arg__"
"__hot__"
"__ifunc__"
"__interrupt__"
"__interrupt_handler__"
"__leaf__"
"__no_caller_saved_registers__"
"__malloc__"
"__no_icf__"
"__no_instrument_function__"
"__no_profile_instrument_function__"
"__no_reorder__"
"__no_sanitize__"
"__no_sanitize_address__"
"__no_address_safety_analysis__"
"__no_sanitize_thread__"
"__leaf__"
"__no_sanitize_undefined__"
"__no_split_stack__"
"__no_stack_limit__"
"__noclone__"
"__noipa__"
"__nonnull__"
"__noplt__"
"__nothrow__"
"__optimize__"
"__pure__"
"__patchable_function_entry__"
"__returns_nonnull__"
"__returns_twice__"
"__section__"
"__sentinel__"
"__simd__"
"__target_clones__"
"__unused__"
"__used__"
"__visibility__"
"__warn_unused_result__"
"__params_nonnull__"
"__weak__"
"__vector_size__"
"__mode__"))
(clang
'("__optnone__"
"__nodebug__"))
)
(concat "\\_<"
(regexp-opt (append ;; kar
;; ansi
;; c99
c11
;; cxx17
gnu
clang
cosmo))
"\\_>")))
(provide 'cosmo-c-keywords)

222
tool/emacs/cosmo-c-types.el Normal file
View file

@ -0,0 +1,222 @@
(defconst cosmo-c-types-regex
(let (
;; (kar
;; '("short unsigned int"
;; "int"
;; "long unsigned int"
;; "char"
;; "long"
;; "long signed int"
;; "signed long"
;; "unsigned short int"
;; "short int"
;; "signed short int"
;; "unsigned"
;; "long unsigned"
;; "unsigned short"
;; "short signed int"
;; "short unsigned"
;; "unsigned char"
;; "signed int"
;; "unsigned long"
;; "long int"
;; "unsigned int"
;; "signed short"
;; "unsigned long int"
;; "short signed"
;; "signed long int"
;; "signed char"
;; "long signed"))
(ansi
'("size_t"
"wint_t"
;; "void"
"wchar_t"
;; "long double"
))
;; (kar
;; '("char"
;; "short"
;; "int"
;; "long"
;; "signed"
;; "unsigned"))
;; (ansi
;; '("size_t"
;; "wint_t"
;; "void"
;; "wchar_t"))
(c99
'("bool"
"_Bool"
"unsigned long long int"
"int32_t"
"uint_least64_t"
"long long signed"
"intptr_t"
"uintmax_t"
"long long unsigned int"
"int_fast32_t"
"int16_t"
"int64_t"
"int_fast16_t"
"int_fast64_t"
"errno_t"
"uint_fast32_t"
"int_least8_t"
"uint_least16_t"
"long long signed int"
"long long"
"char16_t"
"uint_least32_t"
"int_least64_t"
"int_least16_t"
"int_fast8_t"
"uint_least8_t"
"uintptr_t"
"ssize_t"
"long long int"
"unsigned long long"
"int8_t"
"long long unsigned"
"signed long long int"
"int_least32_t"
"uint8_t"
"uint_fast64_t"
"intmax_t"
"uint_fast16_t"
"signed long long"
"uint32_t"
"ptrdiff_t"
"char32_t"
"uint64_t"
"uint16_t"
"uint_fast8_t"
"complex float"
"complex double"
"complex long double"))
(c11
'("atomic_uintptr_t"
"atomic_uchar"
"atomic_int_least32_t"
"atomic_uint_least64_t"
"atomic_int_fast32_t"
"atomic_uint_least16_t"
"atomic_short"
"atomic_size_t"
"atomic_uint"
"atomic_char16_t"
"atomic_ullong"
"atomic_uint_fast16_t"
"atomic_int_fast8_t"
"atomic_uint_least32_t"
"atomic_ptrdiff_t"
"atomic_uintmax_t"
"atomic_int_least16_t"
"atomic_long"
"atomic_int"
"atomic_int_fast16_t"
"atomic_uint_least8_t"
"atomic_ushort"
"atomic_int_least8_t"
"atomic_ulong"
"atomic_char32_t"
"atomic_schar"
"atomic_intmax_t"
"atomic_int_least64_t"
"atomic_uint_fast64_t"
"atomic_wchar_t"
"atomic_uint_fast8_t"
"atomic_int_fast64_t"
"atomic_llong"
"atomic_bool"
"atomic_intptr_t"
"atomic_uint_fast32_t"
"atomic_char"))
(gnu
'("__int128"
"unsigned __int128"
"signed __int128"
"_Float16"
"_Complex _Float16"
"_Float16x"
"_Complex _Float16x"
"_Float32"
"_Complex _Float32"
"_Float32x"
"_Complex _Float32x"
"_Float64"
"_Complex _Float64"
"_Float64x"
"_Complex _Float64x"
"_Float128"
"_Complex _Float128"))
(cxx17
'())
(cosmo
'("int_least128_t"
"int_fast128_t"
"bool32"
"int128_t"
"uint128_t"
"axdx_t"))
(x86intrin
'("__v8hu"
"__v16qi"
"__v4su"
"__v8su"
"__v16qu"
"__v16qs"
"__v8hi"
"__v4hi"
"__v2df"
"__v2di"
"__v4si"
"__v8si"
"__m1"
"__v2du"
"__m2"
"__v1di"
"__v4sf"
"__v8sf"
"__v2si"
"__m64"
"__v2sf"
"__v8qi"
"__v32qi"
"__m128"
"__m128d"
"__m128i"
"__m128_u"
"__m128d_u"
"__m128i_u"
"__m256"
"__m256d"
"__m256i"
"__m256_u"
"__m256d_u"
"__m256i_u"))
)
(concat "\\_<"
(regexp-opt (append ;; kar
ansi
c99
c11
gnu
cxx17
cosmo
x86intrin))
"\\_>")))
(provide 'cosmo-c-types)

132
tool/emacs/cosmo-format.el Normal file
View file

@ -0,0 +1,132 @@
;;; cosmo-format.el --- Cosmopolitan Clang-Format Integration
;; Author: Justine Tunney <jtunney@gmail.com>
;; Version: 0.1.0
;; License: Public Domain
;; Keywords: c c++ clang
;; To the extent possible under law, Justine Tunney has waived all
;; copyright and related or neighboring rights to this file, as it is
;; written in the following disclaimers: <http://unlicense.org/> and
;; <http://creativecommons.org/publicdomain/zero/1.0/>
;;; Commentary:
;;
;; This module automates indentation, whitespace, and other stylistic
;; concerns while editing C/C++ source files. The clang-format program,
;; if present on the system, is run each time a buffer is saved.
;;; Installation:
;;
;; Put the following in your .emacs.d/init.el file:
;;
;; (require 'cosmo-format)
;;
;; Put this file in the root of your project:
;;
;; printf '---\nBasedOnStyle: Google\n...\n' >.clang-format
;;
;; Any buffer whose pathname matches `cosmo-format-path-regex' will
;; be formatted automatically on save if:
;;
;; 1. It's able to find the clang-format program, or
;; `cosmo-format-bin' is customized.
;;
;; 2. There's a .clang-format file up the directory tree, or
;; `cosmo-format-arg' is customized; in which case, it is
;; recommended that it be customized buffer locally.
;;
;; For all other cases, there are no latency penalties (i.e. superfluous
;; i/o syscalls) or risks to leaving this enabled globally.
;;; Code:
(defcustom cosmo-format-bin nil
"Explicit command or pathname of clang-format program."
:type 'string
:group 'cosmo-format)
(defcustom cosmo-format-arg nil
"Explicit argument to clang-format program."
:type 'string
:group 'cosmo-format)
(defcustom cosmo-format-modes '(c-mode
c++-mode
java-mode
protobuf-mode)
"List of major-modes that need clang-format."
:type '(repeat symbol)
:group 'cosmo-format)
(defcustom cosmo-format-exts '("c" "cc" "h" "inc" ;; c/c++
"hh" "cpp" "hpp" ;; ms c/c++
"rl" ;; ragel
"proto") ;; protobuf
"List of pathname extensions that need clang-format."
:type '(repeat string)
:group 'cosmo-format)
(defcustom cosmo-format-blacklist '()
"List of files to ignore, matched by basename."
:type '(repeat string)
:group 'cosmo-format)
(defvar cosmo--clang-format-bin)
(defmacro cosmo-memoize (var mvar form)
"Return VAR or evaluate FORM memoized locally to MVAR."
`(cond (,var ,var)
((fboundp (quote ,mvar))
(cond ((eq ,mvar 'null) nil)
(t ,mvar)))
(t (let ((res ,form))
(setq-local ,mvar (or res 'null))
res))))
(defun cosmo--find-clang-format-bin ()
(cosmo-memoize cosmo-format-bin
cosmo--clang-format-bin
(or (executable-find "clang-format-10")
(executable-find "clang-format-9")
(executable-find "clang-format-8")
(executable-find "clang-format-7")
(executable-find "clang-format"))))
(defun cosmo-format ()
"Beautifies source code in current buffer."
(interactive)
(when (and (memq major-mode cosmo-format-modes)
(member (file-name-extension (buffer-file-name))
cosmo-format-exts)
(not (member (file-name-nondirectory (buffer-name))
cosmo-format-blacklist)))
(let ((bin (cosmo--find-clang-format-bin)))
(when bin
(let ((p (point))
(tmp (make-temp-file "cosmo-format"))
(arg (or cosmo-format-arg
(and (locate-dominating-file
(buffer-file-name)
".clang-format")
"-style=file"))))
(when arg
(write-region nil nil tmp)
(let ((buf (get-buffer-create "*clang-format*"))
(exe (cosmo--find-clang-format-bin)))
;; (with-current-buffer buf
;; (set-process-sentinel
;; (call-process exe tmp t nil arg)
;; (lambda (_ _)
;; (display-buffer buf))))
(with-current-buffer buf
(call-process exe tmp t nil arg))
(replace-buffer-contents buf)
(kill-buffer buf)
(delete-file tmp nil))))))))
(add-hook 'before-save-hook 'cosmo-format)
(provide 'cosmo-format)
;;; cosmo-format.el ends here

764
tool/emacs/cosmo-stuff.el Normal file
View file

@ -0,0 +1,764 @@
;; ╔──────────────────────────────────────────────────────────────────╗
;; │ To the extent possible under law, Justine Tunney has waived │
;; │ all copyright and related or neighboring rights to this file, │
;; │ as it is written in the following disclaimers: │
;; │ • http://unlicense.org/ │
;; │ • http://creativecommons.org/publicdomain/zero/1.0/ │
;; ╚──────────────────────────────────────────────────────────────────╝
;; Hodgepodge of copypasta from Justine's Emacs config intended to be
;; helpful to those wanting to configure their own Emacs editor to work
;; pretty well as a Cosmopolitan IDE.
;;; Code:
(require 'asm-mode)
(require 'cc-mode)
(require 'fortran)
(require 'cosmo-c-builtins)
(require 'cosmo-c-constants)
(require 'cosmo-c-keywords)
(require 'cosmo-c-types)
(require 'dired)
(require 'javadown)
(require 'ld-script)
(require 'make-mode)
(setq c-doc-comment-style 'javadown)
(add-to-list 'auto-mode-alist '("\\.x$" . c-mode)) ;; -aux-info
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Support Macros
(defmacro setql (var val)
`(set (make-local-variable ',var) ,val))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Support Functions
(defun cosmo-contains (x s &optional icase)
(declare (pure t) (side-effect-free t))
(let ((case-fold-search icase))
(not (null (string-match-p (regexp-quote x) s)))))
(defun cosmo-startswith (x s &optional icase)
(declare (pure t) (side-effect-free t))
(not (null (string-prefix-p x s icase))))
(defun cosmo-endswith (x s &optional icase)
(declare (pure t) (side-effect-free t))
(let ((p (- (length s) (length x))))
(and (>= p 0)
(eq t (compare-strings x nil nil
s p nil icase)))))
(defun cosmo-replace (x y s)
(declare (pure t) (side-effect-free t))
(let ((case-fold-search nil))
(replace-regexp-in-string (regexp-quote x) y s t t)))
(defun cosmo-join (s l)
(declare (pure t) (side-effect-free t))
(mapconcat 'identity l s))
(defun cosmo-lchop (p s)
(declare (pure t) (side-effect-free t))
(if (and (>= (length s) (length p))
(string= p (substring s 0 (length p))))
(substring s (length p))
s))
(defun cosmo-first-that (predicate list)
(declare (pure t))
(when list
(if (funcall predicate (car list))
(car list)
(cosmo-first-that predicate (cdr list)))))
(defun cosmo-file-name-sans-extensions (filename)
(save-match-data
(let (directory
(file (file-name-sans-versions
(file-name-nondirectory filename))))
(if (and (string-match "[.-].*\\'" file)
(not (eq 0 (match-beginning 0))))
(if (setq directory (file-name-directory filename))
(concat directory (substring file 0 (match-beginning 0)))
(substring file 0 (match-beginning 0)))
filename))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C-c . Jump to symbol definition
;; C-c , Jump back to where we were
;; C-c C-i Show references to symbol
;; TODO(jart): Why doesnt the default-directory variable work?
(defun project-current (&optional maybe-prompt dir)
(expand-file-name
(file-name-directory
(locate-dominating-file
(buffer-name) "Makefile"))))
(defun cosmo-xref-find-references ()
(interactive)
(let ((case-fold-search nil))
(xref-find-references (format "%S" (symbol-at-point)))))
(defun cosmo-xref-find-definitions ()
(interactive)
(let ((case-fold-search nil))
(xref-find-definitions (format "%S" (symbol-at-point)))))
(global-set-key (kbd "M-,") 'xref-pop-marker-stack)
(global-set-key (kbd "M-.") 'cosmo-xref-find-definitions)
(global-set-key (kbd "C-c TAB") 'cosmo-xref-find-references)
(defun stop-asking-questions-etags ()
(set (make-local-variable 'tags-file-name)
(format "%s/TAGS"
(or (locate-dominating-file (buffer-name) "Makefile")
(file-name-directory (buffer-name))))))
(add-hook 'c-mode-common-hook 'stop-asking-questions-etags)
(setq tags-revert-without-query t)
(setq kill-buffer-query-functions ;; disable kill buffer w/ process question
(delq 'process-kill-buffer-query-function kill-buffer-query-functions))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compile buffer and run associated test and/or display assembly
;; C-c C-c Compile
;; M-1 C-c C-c Compile w/ MODE=tiny
;; M-2 C-c C-c Compile w/ MODE=opt
;; M-3 C-c C-c Compile w/ MODE=rel
;; M-4 C-c C-c Compile w/ MODE=dbg
;; M-5 C-c C-c Compile w/ MODE=""
(defun cosmo-intest (&optional file-name)
(let (path root pkg)
(and (setq path (or (buffer-file-name) dired-directory))
(setq root (locate-dominating-file path "Makefile"))
(setq pkg (file-relative-name path root))
(cosmo-startswith "test/" pkg))))
(defun cosmo--make-mode (arg &optional default)
(cond ((eq arg 1) "tiny")
((eq arg 2) "opt")
((eq arg 3) "rel")
((eq arg 4) "dbg")
((eq arg 5) "")
(default default)
((cosmo-intest) "dbg")
(t "")))
(defun cosmo--compile-command (this root kind mode)
(let* ((ext (file-name-extension this)) ;; e.g. "c"
(dir (file-name-directory this)) ;; e.g. "/home/jart/daisy/libc/"
(dots (file-relative-name root dir)) ;; e.g. "../"
(file (file-relative-name this root)) ;; e.g. "libc/crc32c.c"
(name (file-name-sans-extension file)) ;; e.g. "libc/crc32c"
(buddy (format "test/%s_test.c" name))
(runs (format "o/$m/%s.com.runs TESTARGS=-b" name))
(buns (format "o/$m/test/%s_test.com.runs TESTARGS=-b" name)))
(cond ((not (member ext '("c" "cc" "s" "S" "rl" "f")))
(format "m=%s; make -j8 -O MODE=$m SILENT=0 o/$m/%s"
mode
(directory-file-name
(file-name-directory
(file-relative-name this root)))))
((cosmo-contains "_test." (buffer-file-name))
(format "m=%s; make -j8 -O MODE=$m %s"
mode runs))
((file-exists-p (format "%s" buddy))
(format (cosmo-join
" && "
'("m=%s; make -j8 -O o/$m/%s.o MODE=$m SILENT=0"
"objdump -wzCd o/$m/%s.o"
"make -j8 -O MODE=$m %s"))
mode name name buns))
((eq kind 'run)
(format
(cosmo-join
" && "
`("m=%s; f=o/$m/%s.com"
,(concat "make -j8 -O $f MODE=$m SILENT=0")
"./$f"))
mode name))
((and (file-regular-p this)
(file-executable-p this))
(format "./%s" file))
('t
(format
(cosmo-join
" && "
`("m=%s; f=o/$m/%s.o"
,(concat "make -j8 -O $f MODE=$m SILENT=0")
"objdump -wzCd $f"))
mode name)))))
(defun cosmo-compile (arg)
(interactive "P")
(let* ((this (or (buffer-file-name) dired-directory))
(root (locate-dominating-file this "Makefile")))
(when root
(let* ((mode (cosmo--make-mode arg))
(default-directory root)
(compile-command (cosmo--compile-command this root nil mode)))
(compile compile-command)))))
(defun cosmo-compile-hook ()
(local-set-key (kbd "C-c C-c") 'cosmo-compile))
(progn
(add-hook 'makefile-mode-hook 'cosmo-compile-hook)
(add-hook 'asm-mode-hook 'cosmo-compile-hook)
(add-hook 'ld-script-mode-hook 'cosmo-compile-hook)
(add-hook 'dired-mode-hook 'cosmo-compile-hook)
(add-hook 'c-mode-common-hook 'cosmo-compile-hook)
(add-hook 'fortran-mode-hook 'cosmo-compile-hook)
(add-hook 'protobuf-mode-hook 'cosmo-compile-hook))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Display assembly for C/C++ buffer
;;
;; C-c C-a Compile and show assembly, with a native optimized build
;; C-c C-b Compile and show assembly, with a balanced build
;;
;; The ALT key may be used to override the default mode.
;;
;; M-1 C-c C-a Compile w/ MODE=tiny
;; M-2 C-c C-a Compile w/ MODE=opt
;; M-3 C-c C-a Compile w/ MODE=rel
;; M-4 C-c C-a Compile w/ MODE=dbg
;; M-5 C-c C-a Compile w/ MODE=""
;;
(defvar cosmo--assembly-root nil)
(defvar cosmo--assembly-pending nil)
(defun cosmo--scrub (regex replace)
(replace-regexp regex replace nil (point-min) (point-max)))
(defun cosmo--fixup-asm ()
(cosmo--scrub
;; Code like this
;;
;; .section "GNU!GNU!GNU!.must.be.in.every.asm.or.mprotect.breaks"
;;
;; Isn't needed since the underlying issue is addressed by ape/ape.S
;; which generates executable structure.
" .section .note.GNU-stack,.*,@progbits\n"
"")
(cosmo--scrub
;; Code like this
;;
;; and $2047, %edx
;;
;; Should be this
;;
;; and $2047,%edx
;;
;; Per our favored coding style.
", "
",")
(cosmo--scrub
;; Code like this
;;
;; .ascii "foo"
;; .zero 1
;;
;; Should be this
;;
;; .ascii "foo"
;; .zero 1
;;
;; @see -frecord-gcc-switches
"\t\\.ascii\t\"\\([^\"]*\\)\"\n\t\\.zero\t1\n"
"\t.asciz\t\"\\1\"\n")
(cosmo--scrub
;; Code like this
;;
;; movl %eax,a
;; movb b,%bl
;; movb $1,c
;; incb d
;; movq %xmm0,%rax
;;
;; Should be this
;;
;; mov %eax,a
;; mov %bl,b
;; movb $1,c
;; incb d
;; movq %xmm0,%rax # ;_;
;;
;; Because we dislike redundancy and don't want the
;; ibmicroborlandtel crowd feeling too uncomfortable.
(let ((names-a-register
"%[A-Za-z]")
(normal-arithmetic-ops
(regexp-opt
(list
"rcr" "rcl" "ror" "rol" "hlt" "cmc" "div" "sar"
"mov" "shl" "shr" "lea" "cmp" "adc" "sbb" "inc"
"sub" "xor" "add" "and" "or" "not" "btc" "dec"
"mul" "neg" "bt" "bsf" "bsr" "int" "test")))
(prefixes
(regexp-opt
(list
"es" "cs" "ss" "ds" "lock"
"rep" "repnz" "repne" "repz" "repe"
"rex.b" "rex.x" "rex.xb" "rex.r" "rex.rb" "rex.rx"
"rex.rxb" "rex.w" "rex.wb" "rex.wx" "rex.wxb" "rex.wr"
"rex.trb" "rex.wrx" "rex.wrxb"))))
(concat "\\("
"^[ \t]*"
"\\(?:" prefixes " \\|\\)"
normal-arithmetic-ops
"\\)"
"[bwlq]"
"\\("
"[ \t]+"
"\\(?:"
"%[A-Wa-w][^\n]+"
"\\|"
"[^%][^\n,]*,%[A-Za-z][^\n]+"
"\\)"
"\\)"))
"\\1\\2")
;; Scroll first function to top of display.
(goto-char (point-min))
(when (search-forward-regexp
"\t\\.type[^\n]*@function" nil t)
(recenter-top-bottom 'top)))
(defun cosmo--assembly-compilation-finished (compilebuf msg)
(when cosmo--assembly-pending
(let ((asm-gcc (car cosmo--assembly-pending))
(asm-clang (cdr cosmo--assembly-pending))
width
asm-gcc-buffer
asm-gcc-window
asm-clang-buffer
asm-clang-window)
(setq cosmo--assembly-pending nil)
(when (not (cosmo-contains "finished" msg))
(error "making assembly failed: %s" msg))
(let ((f (format "%s/%s" cosmo--assembly-root asm-gcc)))
(when (or (null asm-gcc) (not (file-exists-p f)))
(error "asm-gcc not found: %s" f)))
(let ((f (format "%s/%s" cosmo--assembly-root asm-clang)))
(when (or (null asm-gcc) (not (file-exists-p f)))
(error "asm-gcc not found: %s" f)))
(delete-other-windows)
(setq width (window-total-width))
(setq asm-gcc-buffer (find-file-noselect asm-gcc t nil nil))
(setq asm-clang-buffer (find-file-noselect asm-clang t nil nil))
(setq asm-clang-window (split-window-right (- width (/ width 4))))
(setq asm-gcc-window (split-window-right (- (- width (/ width 4)) (/ width 4))))
(set-window-buffer asm-gcc-window asm-gcc-buffer)
(set-window-buffer asm-clang-window asm-clang-buffer)
(other-window 1)
(cosmo--fixup-asm)
(save-buffer)
(bury-buffer (current-buffer))
(other-window 1)
(cosmo--fixup-asm)
(save-buffer)
(bury-buffer (current-buffer))
(other-window 1)
(bury-buffer compilebuf))))
(defun cosmo--purge-file (path)
(let ((b (find-file-noselect path t nil nil)))
(when b
(with-current-buffer b
(save-buffer))
(when (not (kill-buffer b))
(error "couldn't kill asm buffer"))))
(delete-file path t))
(defun cosmo--assembly (arg extra-make-flags)
(let* ((this (expand-file-name (or (buffer-file-name) dired-directory)))
(root (expand-file-name (locate-dominating-file this "Makefile"))))
(when root
(let* ((mode (cosmo--make-mode arg "opt"))
(ext (file-name-extension this))
(dir (file-name-directory this))
(dots (file-relative-name root dir))
(file (file-relative-name this root))
(name (file-name-sans-extension file))
(asm-gcc (format "o/%s/%s-gcc.asm" mode name))
(asm-clang (format "o/%s/%s-clang.asm" mode name)))
(when (not (member ext '("c" "cc" "f")))
(error "don't know how to show assembly for non c/c++ source file"))
(let* ((default-directory root)
(compile-command
(format "make %s SILENT=0 -j8 -O MODE=%s %s %s"
(or extra-make-flags "") mode asm-gcc asm-clang)))
(save-buffer)
(set-visited-file-modtime (current-time))
(cosmo--purge-file asm-gcc)
(cosmo--purge-file asm-clang)
(save-buffer)
(setq cosmo--assembly-root root)
(setq cosmo--assembly-pending (cons asm-gcc asm-clang))
(let ((errormsg (shell-command-to-string (format "touch %s" file))))
(when (not (equal "" errormsg))
(error errormsg)))
(revert-buffer :ignore-auto :noconfirm)
(compile compile-command))))))
(defun cosmo-assembly (arg)
(interactive "P")
(setq arg (or arg 2))
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly (setq arg (logand (lognot 8)))
"SILENT=0 COPTS='-ffast-math -O3 -funsafe-math-optimizations -fsched2-use-superblocks'"))
(t (cosmo--assembly arg "SILENT=0 COPTS='-O3'"))))
(defun cosmo-assembly-native (arg)
(interactive "P")
(setq arg (or arg 3))
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly
(setq arg (logand (lognot 8)))
"SILENT=0 CCFLAGS=--verbose COPTS='$(IEEE_MATH)' TARGET_ARCH='-march=znver2'"))
(t
(cosmo--assembly
arg
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' TARGET_ARCH='-march=znver2'"))))
(defun cosmo-assembly-icelake (arg)
(interactive "P")
(setq arg (or arg 3))
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly
(setq arg (logand (lognot 8)))
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' TARGET_ARCH='-march=icelake-client'"))
(t
(cosmo--assembly
arg
"SILENT=0 CCFLAGS=--verbose COPTS='$(MATHEMATICAL) -O3' TARGET_ARCH='-march=icelake-client'"))))
(defun cosmo-assembly-balanced (arg)
(interactive "P")
(cosmo--assembly (or arg 5) "CFLAGS='-O2 -ftrapv' SILENT=0"))
(defun cosmo-mca (arg)
(interactive "P")
(let (code
delete
cleanup
(inhibit-message t)
(inhibit-read-only t)
(term (getenv "TERM"))
(prog (executable-find "llvm-mca-10"))
(buf1 (get-buffer-create "*mca*"))
(buf2 (generate-new-buffer "*mca*")))
(setenv "TERM" "xterm-256color")
(setq cleanup
(lambda ()
(setenv term)
(kill-buffer buf2)
(when delete (delete-file delete))))
(condition-case exc
(progn
(if (not (buffer-modified-p))
(setq code (buffer-file-name))
(setq code (make-temp-file "mca.s"))
(write-region nil nil code)
(setq delete code))
(with-current-buffer buf2
(insert "\n")
(setq rc (call-process prog nil t nil
"-mcpu=skylake"
"-mtriple=x86_64-pc-linux-gnu"
"--bottleneck-analysis"
"-instruction-info"
"-iterations=8"
"-all-stats"
"-all-views"
"-timeline"
code)))
(with-current-buffer buf1
(replace-buffer-contents buf2)
(if (eq rc 0)
(fundamental-mode)
(compilation-mode))
(xterm-color-colorize-buffer)
(display-buffer (current-buffer))))
('error
(funcall cleanup)
(error exc)))
(funcall cleanup)))
(defun cosmo-assemble-hook ()
(add-to-list 'compilation-finish-functions
'cosmo--assembly-compilation-finished)
(local-set-key (kbd "C-c C-a") 'cosmo-assembly)
(local-set-key (kbd "C-c C-b") 'cosmo-assembly-balanced)
(local-set-key (kbd "C-c C-n") 'cosmo-assembly-native)
(local-set-key (kbd "C-c C-i") 'cosmo-assembly-icelake))
(defun cosmo-mca-hook ()
;; (local-set-key (kbd "C-c C-h") 'cosmo-mca)
)
(progn
(add-hook 'asm-mode-hook 'cosmo-mca-hook)
(add-hook 'makefile-mode-hook 'cosmo-assemble-hook)
(add-hook 'asm-mode-hook 'cosmo-assemble-hook)
(add-hook 'ld-script-mode-hook 'cosmo-assemble-hook)
(add-hook 'dired-mode-hook 'cosmo-assemble-hook)
(add-hook 'c-mode-common-hook 'cosmo-assemble-hook)
(add-hook 'fortran-mode-hook 'cosmo-assemble-hook)
(add-hook 'protobuf-mode-hook 'cosmo-assemble-hook))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Run buffer.
;; C-c C-r Run
;; M-1 C-c C-r Run w/ MODE=tiny
;; M-2 C-c C-r Run w/ MODE=opt
;; M-3 C-c C-r Run w/ MODE=rel
;; M-4 C-c C-r Run w/ MODE=dbg
;; M-5 C-c C-r Run w/ MODE=""
(defun cosmo-run (arg)
(interactive "P")
(let* ((this (or (buffer-file-name) dired-directory))
(root (or (locate-dominating-file this "Makefile") default-directory))
(file (file-relative-name this root)))
(when root
(let ((default-directory root))
(save-buffer)
(cond ((memq major-mode '(c-mode c++-mode asm-mode fortran-mode))
(let* ((mode (cosmo--make-mode arg))
(compile-command (cosmo--compile-command this root 'run mode)))
(compile compile-command)))
((eq major-mode 'sh-mode)
(compile (format "sh %s" file)))
((eq major-mode 'python-mode)
(compile (format "python %s" file)))
('t
(error "cosmo-run: unknown major mode")))))))
(progn
(define-key asm-mode-map (kbd "C-c C-r") 'cosmo-run)
(define-key c-mode-base-map (kbd "C-c C-r") 'cosmo-run)
(define-key fortran-mode-map (kbd "C-c C-r") 'cosmo-run)
(define-key sh-mode-map (kbd "C-c C-r") 'cosmo-run)
(define-key python-mode-map (kbd "C-c C-r") 'cosmo-run))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Debug buffer.
;; C-c C-d Run in GDB/GUD
;; M-1 C-c C-d Run in GDB/GUD w/ MODE=tiny
;; M-2 C-c C-d Run in GDB/GUD w/ MODE=opt
;; M-3 C-c C-d Run in GDB/GUD w/ MODE=rel
;; M-4 C-c C-d Run in GDB/GUD w/ MODE=dbg
;; M-5 C-c C-d Run in GDB/GUD w/ MODE=""
(defun cosmo-debug (arg)
(interactive "P")
(let* ((this (or (buffer-file-name) dired-directory))
(root (locate-dominating-file this "Makefile")))
(when root
(let* ((mode (cosmo--make-mode arg "dbg"))
(name (file-relative-name this root))
(next (file-name-sans-extension name))
(exec (format "o/%s/%s.com.dbg" mode next))
(default-directory root)
(compile-command (cosmo--compile-command this root nil mode)))
(compile compile-command)
(gdb (format "gdb -q -nh -i=mi %s -ex run" exec))))))
(progn
(define-key asm-mode-map (kbd "C-c C-d") 'cosmo-debug)
(define-key c-mode-base-map (kbd "C-c C-d") 'cosmo-debug))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C-c C-t Toggle buffer between source file and unit test file.
(defun cosmo-toggle-buddy ()
(interactive)
(let* ((this (or (buffer-file-name) dired-directory))
(root (locate-dominating-file this "Makefile")))
(when root
(let* ((name (file-relative-name this root))
(dir (file-name-directory this))
(pkgname (file-name-nondirectory (substring dir 0 -1)))
(dots (file-relative-name root dir))
(notest (cosmo-file-name-sans-extensions
(cosmo-replace "_test" "" (cosmo-lchop "test/" name))))
(buddy
(cond ((and (cosmo-startswith "test/" dir)
(cosmo-endswith "/test.mk" name))
(message (format "%s/%s.mk" (substring dir 5) pkgname))
(format "%s/%s.mk" (substring dir 5) pkgname))
((cosmo-startswith "test/" name)
(cosmo-first-that
'file-exists-p
(list (concat dots notest ".s")
(concat dots notest ".S")
(concat dots notest ".f")
(concat dots notest ".F")
(concat dots notest ".c")
(concat dots notest ".cc")
(concat dots notest ".rl")
(concat dots notest ".ncabi.c")
(concat dots notest ".hookabi.c")
(concat dots notest ".h"))))
(t
(format "%stest/%s_test.c"
dots (cosmo-file-name-sans-extensions name))))))
(when buddy
(find-file buddy))))))
(progn
(global-set-key (kbd "C-c C-t") 'cosmo-toggle-buddy))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C-c C-h Add Include Line
(defun cosmo-add-include ()
(interactive)
(let* ((no-whine t)
(tag-file "HTAGS")
(this (buffer-name))
(case-fold-search nil)
(search (thing-at-point 'symbol))
(buffer (find-file-noselect (format "%s/%s"
(locate-dominating-file
this tag-file)
tag-file)
no-whine))
(header (with-current-buffer buffer
(revert-buffer :ignore-auto :noconfirm)
(save-excursion
(goto-char 0)
(when (re-search-forward
(concat "\177" search "\001") nil t)
(when (re-search-backward "\f\n\\([^,]*\\)," nil t)
(match-string 1))))))
(root (locate-dominating-file this "Makefile"))
(name (file-relative-name this root)))
(when header
(when (not (equal header name))
(save-excursion
(goto-char 0)
(re-search-forward "#include" nil t)
(re-search-forward "^$")
(re-search-backward "#include" nil t)
(beginning-of-line)
(insert (concat "#include \"" header "\"\n"))))
(message header))))
(progn
(define-key asm-mode-map (kbd "C-c C-h") 'cosmo-add-include)
(define-key c-mode-base-map (kbd "C-c C-h") 'cosmo-add-include))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C-c C-o Show Optimization Report
(defun cosmo-show-optinfo (arg)
(interactive "P")
(let* ((mode (cosmo--make-mode arg "opt"))
(this (or (buffer-file-name) dired-directory))
(root (locate-dominating-file this "Makefile")))
(when root
(let* ((name (file-relative-name this root))
(buddy
(format "%s/o/%s/%s.optinfo.gz"
root mode (cosmo-file-name-sans-extensions name))))
(when buddy
(find-file buddy))))))
(defun cosmo-lisp-is-the-worst-this-is-so-tiring ()
(define-key c-mode-base-map (kbd "C-c C-o") 'cosmo-show-optinfo))
(add-hook 'c-mode-common-hook 'cosmo-lisp-is-the-worst-this-is-so-tiring)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Cosmopolitan Extended Language Keyword Definitions
(defun cosmo-keywords-hook ()
(font-lock-add-keywords
nil `((,cosmo-c-keywords-regex . font-lock-keyword-face)
(,cosmo-c-builtins-regex . font-lock-builtin-face)
(,cosmo-c-constants-regex . font-lock-constant-face)
(,cosmo-c-types-regex . font-lock-type-face))))
(add-hook 'c-mode-common-hook 'cosmo-keywords-hook)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Symbol naming convention conversion
;; e.g. STARTF_USESHOWWINDOW -> kNtStartfUseshowwindow
(defun cosmo--ms-to-google-const (s)
(declare (pure t) (side-effect-free t))
(let ((d (downcase (substring s 1))))
(cosmo-replace
"kNtNt" "kNt"
(concat
"kNt"
(substring s 0 1)
(replace-regexp-in-string
"_\\([A-Za-z]\\)"
(lambda (m)
(upcase (match-string 1 m)))
d)))))
;; e.g. kNtStartfUseshowwindow -> STARTF_USESHOWWINDOW
(defun cosmo--google-to-ms-const (s)
(declare (pure t) (side-effect-free t))
(upcase (replace-regexp-in-string
"\\(.\\)\\([A-Z]\\)"
(lambda (m)
(upcase (concat
(match-string 1 m) "_"
(match-string 2 m))))
(substring s 3)
t)))
(defun cosmo-toggle-ms-const ()
(interactive)
(let* ((case-fold-search nil)
(bounds (if (use-region-p)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'symbol)))
(text (buffer-substring-no-properties (car bounds) (cdr bounds))))
(when bounds
(let ((fn (if (or (cosmo-contains "_" text)
(equal text (upcase text)))
'cosmo--ms-to-google-const
'cosmo--google-to-ms-const)))
(delete-region (car bounds) (cdr bounds))
(insert (funcall fn text))))))
;; (define-key c-mode-base-map (kbd "C-c C-l") 'cosmo-toggle-ms-const)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CP/M EOF for the lulz
(defun cosmo-before-save ()
(cond ((memq major-mode '(c-mode asm-mode))
(set (make-local-variable 'require-final-newline)
(not (equal (buffer-substring-no-properties
(- (point-max) 1) (point-max))
"\x1a"))))))
(add-hook 'before-save-hook 'cosmo-before-save)
(provide 'cosmo-stuff)
;;; cosmo-stuff.el ends here

53
tool/emacs/javadown.el Normal file
View file

@ -0,0 +1,53 @@
;;; javadown.el --- C/C++ Markdown Javadocs
;; Copyright 2019 Justine Tunney
;; Author: Justine Tunney
;; Version: 0.1
;; Permission to use, copy, modify, and/or distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;; Commentary:
;;
;; Unintentionally empty.
;;; Code:
(require 'cc-fonts)
(defconst javadown-font-lock-doc-comments
`(;; e.g. ### Implementation Details
("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(#+[^\n\r]+\\)"
3 ,c-doc-markup-face-name prepend nil)
;; e.g. {@code param}
("{@[a-z]+[^}\n\r]*}"
0 ,c-doc-markup-face-name prepend nil)
;; e.g. @param name does stuff
("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)"
3 ,c-doc-markup-face-name prepend nil)
;; e.g. <video src=doge.mp4>
(,(concat "</?\\sw"
"\\("
(concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
"\"[^\"]*\"\\|'[^']*'")
"\\)*>")
0 ,c-doc-markup-face-name prepend nil)))
(defconst javadown-font-lock-keywords
`((,(lambda (limit)
(c-font-lock-doc-comments "/\\*\\*" limit
javadown-font-lock-doc-comments)))))
(provide 'javadown)
;;; javadown.el ends here

486
tool/emacs/key.py Normal file
View file

@ -0,0 +1,486 @@
import os,sys,re
kar_types = frozenset([
"char",
"signed char",
"unsigned char",
"int",
"signed",
"signed int",
"unsigned",
"unsigned int",
"short",
"short int",
"short signed",
"short signed int",
"short unsigned",
"short unsigned int",
"signed short",
"signed short int",
"unsigned short",
"unsigned short int",
"long",
"long int",
"long signed",
"long signed int",
"long unsigned",
"long unsigned int",
"signed long",
"signed long int",
"unsigned long",
"unsigned long int",
"float",
"double",
])
ansi_types = frozenset([
"void",
"wchar_t",
"wint_t",
"size_t",
"long double",
])
c99_types = frozenset([
"long long",
"long long int",
"long long signed",
"long long signed int",
"long long unsigned",
"long long unsigned int",
"signed long long",
"signed long long int",
"unsigned long long",
"unsigned long long int",
"char16_t",
"char32_t",
"errno_t",
"int8_t",
"int16_t",
"int32_t",
"int64_t",
"int_fast8_t",
"int_fast16_t",
"int_fast32_t",
"int_fast64_t",
"int_least16_t",
"int_least32_t",
"int_least64_t",
"int_least8_t",
"uint_fast16_t",
"uint_fast32_t",
"uint_fast64_t",
"uint_fast8_t",
"uint_least16_t",
"uint_least32_t",
"uint_least64_t",
"uint_least8_t",
"intmax_t",
"intptr_t",
"ptrdiff_t",
"ssize_t",
"uint16_t",
"uint32_t",
"uint64_t",
"uint64_t",
"uint8_t",
"uintmax_t",
"uintptr_t",
])
c11_types = frozenset([
"atomic_bool",
"atomic_char",
"atomic_schar",
"atomic_uchar",
"atomic_short",
"atomic_ushort",
"atomic_int",
"atomic_uint",
"atomic_long",
"atomic_ulong",
"atomic_llong",
"atomic_ullong",
"atomic_char16_t",
"atomic_char32_t",
"atomic_wchar_t",
"atomic_int_least8_t",
"atomic_uint_least8_t",
"atomic_int_least16_t",
"atomic_uint_least16_t",
"atomic_int_least32_t",
"atomic_uint_least32_t",
"atomic_int_least64_t",
"atomic_uint_least64_t",
"atomic_int_fast8_t",
"atomic_uint_fast8_t",
"atomic_int_fast16_t",
"atomic_uint_fast16_t",
"atomic_int_fast32_t",
"atomic_uint_fast32_t",
"atomic_int_fast64_t",
"atomic_uint_fast64_t",
"atomic_intptr_t",
"atomic_uintptr_t",
"atomic_size_t",
"atomic_ptrdiff_t",
"atomic_intmax_t",
"atomic_uintmax_t",
])
gnu_types = frozenset([
"__int128",
"signed __int128",
"unsigned __int128",
])
cxx17_types = frozenset([
"bool",
])
cosmo_types = frozenset([
"bool32",
"int128_t",
"int_fast128_t",
"int_least128_t",
"uint128_t",
])
x86intrin_types = frozenset([
"__m1",
"__m2",
"__m64",
"__m128",
"__m128_u",
"__m128d",
"__m128d_u",
"__m128i",
"__m128i_u",
"__v16qi",
"__v16qs",
"__v16qu",
"__v1di",
"__v2df",
"__v2di",
"__v2du",
"__v2sf",
"__v2si",
"__v4hi",
"__v4sf",
"__v4si",
"__v4su",
"__v8hi",
"__v8hu",
"__v8qi",
])
################################################################################
kar_kws = frozenset([
"auto",
"if",
"break",
"case",
"while",
"continue",
"default",
"return",
"do",
"signed",
"else",
"sizeof",
"extern",
"struct",
"switch",
"for",
"goto",
"union",
])
ansi_kws = frozenset([
"auto",
"if",
"break",
"case",
"volatile",
"while",
"const",
"register",
"continue",
"default",
"return",
"do",
"double",
"signed",
"else",
"sizeof",
"static",
"extern",
"struct",
"float",
"switch",
"for",
"typedef",
"goto",
"union",
"typedef",
"enum",
])
c99_kws= frozenset([
"_Bool",
"_Complex",
"_Imaginary",
"inline",
"restrict",
])
c11_kws = frozenset([
"_Alignas",
"_Alignof",
"_Atomic",
"_Generic",
"_Noreturn",
"_Static_assert",
"_Thread_local",
])
cxx17_kws = frozenset([
"alignas",
"alignof",
"asm",
"auto",
"bool",
"break",
"case",
"catch",
"class",
"const",
"const_cast",
"constexpr",
"continue",
"decltype",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"mutable",
"namespace",
"new",
"noexcept",
"nullptr",
"operator",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"using",
"virtual",
"virtual",
"volatile",
"volatile",
"while",
])
cosmo_kws = frozenset([
"pass",
"alignas",
"aligned",
"alignof",
"artificial",
"attributeallocalign",
"attributeallocsize",
"autotype",
"byanymeansnecessary",
"compatfn",
"decltype",
"externinline",
"firstclass",
"flattenout",
"forcealignargpointer",
"forceinline",
"frownedupon",
"hasatleast",
"hidden",
"initarray",
"interruptfn",
"mallocesque",
"mayalias",
"memcpyesque",
"nocallback",
"nodebuginfo",
"nodiscard",
"noinline",
"noinstrument",
"nointerpose",
"nooptimize",
"noprune",
"noreturn",
"nosideeffect",
"nothrow",
"nothrow",
"null",
"nullterminated",
"paramsnonnull",
"preinitarray",
"printfesque",
"privileged",
"pureconst",
"reallocesque",
"relegated",
"returnsnonnull",
"returnspointerwithnoaliases",
"returnstwice",
"scanfesque",
"strftimeesque",
"strlenesque",
"testonly",
"textexit",
"textreal",
"textstartup",
"textwindows",
"thatispacked",
"threadlocal",
"typeof",
"unreachable",
"warnifused",
"winstruct",
"nocallersavedregisters",
"pass",
"alignas",
"aligned",
"alignof",
"artificial",
"attributeallocalign",
"attributeallocsize",
"autotype",
"byanymeansnecessary",
"compatfn",
"decltype",
"externinline",
"firstclass",
"flattenout",
"forcealignargpointer",
"forceinline",
"frownedupon",
"hasatleast",
"hidden",
"initarray",
"interruptfn",
"mallocesque",
"mayalias",
"memcpyesque",
"nocallback",
"nodebuginfo",
"nodiscard",
"noinline",
"noinstrument",
"nointerpose",
"nooptimize",
"noprune",
"noreturn",
"nosideeffect",
"nothrow",
"nothrow",
"null",
"nullterminated",
"paramsnonnull",
"preinitarray",
"printfesque",
"privileged",
"pureconst",
"reallocesque",
"relegated",
"returnsnonnull",
"returnspointerwithnoaliases",
"returnstwice",
"scanfesque",
"strftimeesque",
"strlenesque",
"testonly",
"textexit",
"textreal",
"textstartup",
"textwindows",
"thatispacked",
"threadlocal",
"typeof",
"unreachable",
"warnifused",
"winstruct",
"nocallersavedregisters",
])
################################################################################
typegroups = (("kar", kar_types),
("ansi", ansi_types),
("c99", c99_types),
("c11", c11_types),
("gnu", gnu_types),
("cxx17", cxx17_types),
("cosmo", cosmo_types),
("x86intrin", x86intrin_types))
kwgroups = (("kar", kar_kws),
("ansi", ansi_kws),
("c99", c99_kws),
("c11", c11_kws),
("cxx17", cxx17_kws),
("cosmo", cosmo_kws))
types = reduce(lambda a,b: a|b[1], typegroups, set())
kws = reduce(lambda a,b: a|b[1], kwgroups, set())
################################################################################
for name, gg, nonono in (("cosmo-c-types", typegroups, kws),
("cosmo-c-keywords", kwgroups, types)):
first = True
sys.stdout.write("""\
(defconst %s-regex
(let (""" % name)
for k, vs in gg:
sys.stdout.write(("""%s(%s (list %s))
""" % ("" if first else "\n ", k, """
""".join('"%s"' % repr(s)[1:][:-1]
for s in vs - nonono))).rstrip())
first = False
sys.stdout.write(""")
(concat "\\<" (regexp-opt (append """)
sys.stdout.write("""
""".join(k for k,_ in gg))
sys.stdout.write(""")) "\\>")))\n\n""")

185
tool/emacs/ld-script.el Normal file
View file

@ -0,0 +1,185 @@
;;; ld-script.el --- GNU linker script editing mode for Emacs
;; Copyright (C) 2001-2018 Free Software Foundation, Inc.
;; Author: Masatake YAMATO<jet@gyve.org>
;; Keywords: languages, faces
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Major mode for editing GNU linker (ld) scripts.
;;; Code:
;; Custom
(defgroup ld-script nil
"GNU linker script code editing commands for Emacs."
:prefix "ld-script-"
:group 'languages)
(defvar ld-script-location-counter-face 'ld-script-location-counter)
(defface ld-script-location-counter
'((t :weight bold :inherit font-lock-builtin-face))
"Face for location counter in GNU ld script."
:group 'ld-script)
;; Syntax rules
(defvar ld-script-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\ "-" st)
(modify-syntax-entry ?{ "(}" st)
(modify-syntax-entry ?} "){" st)
(modify-syntax-entry ?\( "()" st)
(modify-syntax-entry ?\) ")(" st)
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
(modify-syntax-entry ?_ "_" st)
(modify-syntax-entry ?. "_" st)
(modify-syntax-entry ?\\ "\\" st)
(modify-syntax-entry ?: "." st)
(modify-syntax-entry ?, "." st)
(modify-syntax-entry ?? "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?* ". 23" st)
(modify-syntax-entry ?/ ". 14" st)
(modify-syntax-entry ?+ "." st)
(modify-syntax-entry ?- "." st)
(modify-syntax-entry ?! "." st)
(modify-syntax-entry ?~ "." st)
(modify-syntax-entry ?% "." st)
(modify-syntax-entry ?< "." st)
(modify-syntax-entry ?> "." st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?| "." st)
(modify-syntax-entry ?\" "\"" st)
st)
"Syntax table used while in `ld-script-mode'.")
;; Font lock keywords
;; (The section number comes from ld's info.)
(defvar ld-script-keywords
'(
;; 3.4.1 Setting the Entry Point
"ENTRY"
;; 3.4.2 Commands Dealing with Files
"INCLUDE" "INPUT" "GROUP" "AS_NEEDED" "OUTPUT" "SEARCH_DIR" "STARTUP"
;; 3.4.3 Commands Dealing with Object File Formats
"OUTPUT_FORMAT" "TARGET"
;; 3.4.4 Assign alias names to memory regions
"REGION_ALIAS"
;; 3.4.5 Other Linker Script Commands
"ASSERT" "EXTERN" "FORCE_COMMON_ALLOCATION"
"INHIBIT_COMMON_ALLOCATION" "INSERT" "AFTER" "BEFORE"
"NOCROSSREFS" "NOCROSSREFS_TO" "OUTPUT_ARCH" "LD_FEATURE"
;; 3.5.2 HIDDEN
"HIDDEN"
;; 3.5.3 PROVIDE
"PROVIDE"
;; 3.5.4 PROVIDE_HIDDEN
"PROVIDE_HIDDEN"
;; 3.6 SECTIONS Command
"SECTIONS"
;; 3.6.4.2 Input Section Wildcard Patterns
"SORT" "SORT_NONE" "SORT_BY_NAME" "SORT_BY_ALIGNMENT"
"SORT_BY_INIT_PRIORITY"
;; 3.6.4.3 Input Section for Common Symbols
"COMMON"
;; 3.6.4.4 Input Section and Garbage Collection
"KEEP"
;; 3.6.5 Output Section Data
"BYTE" "SHORT" "LONG" "QUAD" "SQUAD" "FILL"
;; 3.6.6 Output Section Keywords
"CREATE_OBJECT_SYMBOLS" "CONSTRUCTORS"
"__CTOR_LIST__" "__CTOR_END__" "__DTOR_LIST__" "__DTOR_END__"
;; 3.6.7 Output Section Discarding
;; See `ld-script-font-lock-keywords'
;; 3.6.8.1 Output Section Type
"NOLOAD" "DSECT" "COPY" "INFO" "OVERLAY"
;; 3.6.8.2 Output Section LMA
"AT"
;; 3.6.8.4 Forced Input Alignment
"SUBALIGN"
;; 3.6.8.5 Output Section Constraint
"ONLY_IF_RO" "ONLY_IF_RW"
;; 3.6.8.7 Output Section Phdr
":PHDR"
;; 3.7 MEMORY Command
"MEMORY"
;; 3.8 PHDRS Command
"PHDRS" "FILEHDR" "FLAGS"
"PT_NULL" "PT_LOAD" "PT_DYNAMIC" "PT_INTERP" "PT_NOTE" "PT_SHLIB" "PT_PHDR"
"PT_GNU_STACK"
;; 3.9 VERSION Command
"VERSION")
"Keywords used of GNU ld script.")
;; 3.10.2 Symbolic Constants
;; 3.10.9 Builtin Functions
(defvar ld-script-builtins
'("CONSTANT"
"MAXPAGESIZE"
"COMMONPAGESIZE"
"ABSOLUTE"
"ADDR"
"ALIGN"
"ALIGNOF"
"BLOCK"
"DATA_SEGMENT_ALIGN"
"DATA_SEGMENT_END"
"DATA_SEGMENT_RELRO_END"
"DEFINED"
"LENGTH" "len" "l"
"LOADADDR"
"LOG2CEIL"
"MAX"
"MIN"
"NEXT"
"ORIGIN" "org" "o"
"SEGMENT_START"
"SIZEOF"
"SIZEOF_HEADERS"
"sizeof_headers")
"Builtin functions of GNU ld script.")
(defvar ld-script-font-lock-keywords
(append
`((,(concat "\\_<" (regexp-opt ld-script-keywords) "\\_>")
0 font-lock-keyword-face)
(,(concat "\\_<" (regexp-opt ld-script-builtins) "\\_>")
0 font-lock-builtin-face)
;; 3.6.7 Output Section Discarding
;; 3.6.4.1 Input Section Basics
;; 3.6.8.7 Output Section Phdr
("/DISCARD/\\|EXCLUDE_FILE\\|:NONE" . font-lock-warning-face)
("\\W\\(\\.\\)\\W" 1 ld-script-location-counter-face)
)
cpp-font-lock-keywords)
"Default font-lock-keywords for `ld-script-mode'.")
;;;###autoload
(define-derived-mode ld-script-mode prog-mode "LD-Script"
"A major mode to edit GNU ld script files"
(set (make-local-variable 'comment-start) "/* ")
(set (make-local-variable 'comment-end) " */")
(set (make-local-variable 'font-lock-defaults)
'(ld-script-font-lock-keywords nil)))
(provide 'ld-script)
;;; ld-script.el ends here

View file

@ -0,0 +1,11 @@
(require 'compile)
(define-derived-mode optinfo-mode compilation-mode "Optimization Info"
(let ((root (locate-dominating-file (buffer-file-name) "Makefile")))
(when root
(setq-local default-directory root))))
(auto-compression-mode t)
(add-to-list 'auto-mode-alist '("\\.optinfo\\(\\|\\.gz\\)$" . optinfo-mode))
(provide 'optinfo-mode)

224
tool/emacs/protobuf-mode.el Normal file
View file

@ -0,0 +1,224 @@
;;; protobuf-mode.el --- major mode for editing protocol buffers.
;; Author: Alexandre Vassalotti <alexandre@peadrop.com>
;; Created: 23-Apr-2009
;; Version: 0.3
;; Package-Version: 3.10.0
;; Keywords: google protobuf languages
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are
;; met:
;;
;; * Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; * Redistributions in binary form must reproduce the above
;; copyright notice, this list of conditions and the following disclaimer
;; in the documentation and/or other materials provided with the
;; distribution.
;; * Neither the name of Google Inc. nor the names of its
;; contributors may be used to endorse or promote products derived from
;; this software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;; Commentary:
;; Installation:
;; - Put `protobuf-mode.el' in your Emacs load-path.
;; - Add this line to your .emacs file:
;; (require 'protobuf-mode)
;;
;; You can customize this mode just like any mode derived from CC Mode. If
;; you want to add customizations specific to protobuf-mode, you can use the
;; `protobuf-mode-hook'. For example, the following would make protocol-mode
;; use 2-space indentation:
;;
;; (defconst my-protobuf-style
;; '((c-basic-offset . 2)
;; (indent-tabs-mode . nil)))
;;
;; (add-hook 'protobuf-mode-hook
;; (lambda () (c-add-style "my-style" my-protobuf-style t)))
;;
;; Refer to the documentation of CC Mode for more information about
;; customization details and how to use this mode.
;;
;; TODO:
;; - Make highlighting for enum values work properly.
;; - Fix the parser to recognize extensions as identifiers and not
;; as casts.
;; - Improve the parsing of option assignment lists. For example:
;; optional int32 foo = 1 [(my_field_option) = 4.5];
;; - Add support for fully-qualified identifiers (e.g., with a leading ".").
;;; Code:
(require 'cc-mode)
(eval-when-compile
(and (= emacs-major-version 24)
(>= emacs-minor-version 4)
(require 'cl))
(require 'cc-langs)
(require 'cc-fonts))
;; This mode does not inherit properties from other modes. So, we do not use
;; the usual `c-add-language' function.
(eval-and-compile
(put 'protobuf-mode 'c-mode-prefix "protobuf-"))
;; The following code uses of the `c-lang-defconst' macro define syntactic
;; features of protocol buffer language. Refer to the documentation in the
;; cc-langs.el file for information about the meaning of the -kwds variables.
(c-lang-defconst c-primitive-type-kwds
protobuf '("double" "float" "int32" "int64" "uint32" "uint64" "sint32"
"sint64" "fixed32" "fixed64" "sfixed32" "sfixed64" "bool"
"string" "bytes" "group"))
(c-lang-defconst c-modifier-kwds
protobuf '("required" "optional" "repeated"))
(c-lang-defconst c-class-decl-kwds
protobuf '("message" "enum" "service"))
(c-lang-defconst c-constant-kwds
protobuf '("true" "false"))
(c-lang-defconst c-other-decl-kwds
protobuf '("package" "import"))
(c-lang-defconst c-other-kwds
protobuf '("default" "max"))
(c-lang-defconst c-identifier-ops
;; Handle extended identifiers like google.protobuf.MessageOptions
protobuf '((left-assoc ".")))
;; The following keywords do not fit well in keyword classes defined by
;; cc-mode. So, we approximate as best we can.
(c-lang-defconst c-type-list-kwds
protobuf '("extensions" "to" "reserved"))
(c-lang-defconst c-typeless-decl-kwds
protobuf '("extend" "rpc" "option" "returns"))
;; Here we remove default syntax for loops, if-statements and other C
;; syntactic features that are not supported by the protocol buffer language.
(c-lang-defconst c-brace-list-decl-kwds
;; Remove syntax for C-style enumerations.
protobuf nil)
(c-lang-defconst c-block-stmt-1-kwds
;; Remove syntax for "do" and "else" keywords.
protobuf nil)
(c-lang-defconst c-block-stmt-2-kwds
;; Remove syntax for "for", "if", "switch" and "while" keywords.
protobuf nil)
(c-lang-defconst c-simple-stmt-kwds
;; Remove syntax for "break", "continue", "goto" and "return" keywords.
protobuf nil)
(c-lang-defconst c-paren-stmt-kwds
;; Remove special case for the "(;;)" in for-loops.
protobuf nil)
(c-lang-defconst c-label-kwds
;; Remove case label syntax for the "case" and "default" keywords.
protobuf nil)
(c-lang-defconst c-before-label-kwds
;; Remove special case for the label in a goto statement.
protobuf nil)
(c-lang-defconst c-cpp-matchers
;; Disable all the C preprocessor syntax.
protobuf nil)
(c-lang-defconst c-decl-prefix-re
;; Same as for C, except it does not match "(". This is needed for disabling
;; the syntax for casts.
protobuf "\\([\{\};,]+\\)")
;; Add support for variable levels of syntax highlighting.
(defconst protobuf-font-lock-keywords-1 (c-lang-const c-matchers-1 protobuf)
"Minimal highlighting for protobuf-mode.")
(defconst protobuf-font-lock-keywords-2 (c-lang-const c-matchers-2 protobuf)
"Fast normal highlighting for protobuf-mode.")
(defconst protobuf-font-lock-keywords-3 (c-lang-const c-matchers-3 protobuf)
"Accurate normal highlighting for protobuf-mode.")
(defvar protobuf-font-lock-keywords protobuf-font-lock-keywords-3
"Default expressions to highlight in protobuf-mode.")
;; Our syntax table is auto-generated from the keyword classes we defined
;; previously with the `c-lang-const' macro.
(defvar protobuf-mode-syntax-table nil
"Syntax table used in protobuf-mode buffers.")
(or protobuf-mode-syntax-table
(setq protobuf-mode-syntax-table
(funcall (c-lang-const c-make-mode-syntax-table protobuf))))
(defvar protobuf-mode-abbrev-table nil
"Abbreviation table used in protobuf-mode buffers.")
(defvar protobuf-mode-map nil
"Keymap used in protobuf-mode buffers.")
(or protobuf-mode-map
(setq protobuf-mode-map (c-make-inherited-keymap)))
(easy-menu-define protobuf-menu protobuf-mode-map
"Protocol Buffers Mode Commands"
(cons "Protocol Buffers" (c-lang-const c-mode-menu protobuf)))
;;;###autoload (add-to-list 'auto-mode-alist '("\\.proto\\'" . protobuf-mode))
;;;###autoload
(defun protobuf-mode ()
"Major mode for editing Protocol Buffers description language.
The hook `c-mode-common-hook' is run with no argument at mode
initialization, then `protobuf-mode-hook'.
Key bindings:
\\{protobuf-mode-map}"
(interactive)
(kill-all-local-variables)
(set-syntax-table protobuf-mode-syntax-table)
(setq major-mode 'protobuf-mode
mode-name "Protocol-Buffers"
local-abbrev-table protobuf-mode-abbrev-table
abbrev-mode t)
(use-local-map protobuf-mode-map)
(c-initialize-cc-mode t)
(if (fboundp 'c-make-emacs-variables-local)
(c-make-emacs-variables-local))
(c-init-language-vars protobuf-mode)
(c-common-init 'protobuf-mode)
(easy-menu-add protobuf-menu)
(c-run-mode-hooks 'c-mode-common-hook 'protobuf-mode-hook)
(c-update-modeline))
(provide 'protobuf-mode)
;;; protobuf-mode.el ends here