-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas-mode.el
2174 lines (1969 loc) · 85.3 KB
/
gas-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; gas-mode.el --- mode for editing assembler code
;; Copyright (C) 2007 Heike C. Zimmerer
;; Time-stamp: <2007-12-27 18:14:08 hcz>
;; Author: Heike C. Zimmerer <[email protected]>
;; Created: 20 Feb 2007
;; Version: 1.10 2009-2-25 hcz
;; Keywords: languages
;; This file is written for GNU Emacs, and uses the same license
;; terms; however, it is an add-on and not part of it.
;; 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; To use this mode, put gas-mode.el somewhere on your load-path.
;; Then add this to your .emacs:
;;
;; (require 'gas-mode)
;; (add-to-list 'auto-mode-alist '("\\.S\\'" . gas-mode))
;; gas-mode recognizes gas syntax (including embedded C preprocessor
;; directives). It does a limited amount of parsing, so it can do
;; some fancy things with syntactic elements (like labels). It,
;; however does not know about the peculiarities of the special
;; processor you're writing code for (there are just too many of
;; them), so, for example, it can't tell register names from labels.
;; Also, its scope is limited to the file you are editing.
;;
;; Symbol highlighting: For a symbol to be recognized as such, it must
;; be in a field where (as per gas syntax) symbols can be placed
;; (i.e. label field, argument field, some directives). If point
;; rests on such a symbol and there are more of it in the current
;; buffer, it is highlighted and you can move forward and backward
;; between all places where that symbol is referenced or defined with
;; forward-sexp and backward-sexp.
;;
;; This is different from a simple string search in that only those
;; places are considered where the symbol is actually used. For local
;; labels, gas-mode resolves which references are associated with
;; which location and only highlights those that fit. The
;; highlighting is different for different types of symbols; see the
;; customization buffer for the gas-symbol-* faces for short
;; explanations of their meanings.
;;
;; Special forms of local labels (like `55$') are not (yet?)
;; supported.
;; A special feature may need some explanation: C passthroughs.
;; Assembler code is often used to write functions that are later
;; called by C programs. For this to work, you usually have to
;; maintain two files, one containing the assembler source, the other
;; holding the C interface declaration. C passthroughs allow you to
;; move the C declaration part into the assembler file next to the
;; function it belongs to.
;;
;; From the assembler's point of view, C passthroughs are just C syntax
;; comments with some small syntactic sugar added, like:
;;
;; /*C
;; int a_declaration(void);
;; extern volatile int another_declaration;
;;
;; /# and this will be passed as comment #/
;; */
;;
;; Note the `/*C' at the beginning and the `/# ... #/' for the nested
;; comment.
;;
;; It is then up to the Make process to generate a .h file, which
;; carries the declarations and the comment, changing the "/#" and "#/"
;; into "/*" and "*/".
;;
;; This may be done by including a line similar to the following into
;; your Makefile (assuming $(ASFILES) is a list of your assembly
;; language files) (and don't forget to use a TAB for the white space
;; which introduces the action lines (<TAB> echo ..., <TAB>sed -n ...):
;;
;; asm-C-defs.h: $(ASFILES)
;; echo '/* Definitions of assembly language functions */' > $@
;; echo '/* (automatically created by make) */' >> $@
;; sed -n '/[/][*]C/,/[*][/]/{s|/[*]C||;s|[*]/||;s|/#|/*|;s|#/|*/|;p}' \
;; $^ >> $@
;;
;; (Note the above code requires the "/*C" and "*/" to be on a line of
;; their own.) gas-mode recognizes this kind of comment by proper
;; syntax highlighting. Symbol highlighting is also supported. For a
;; symbol to be highlighted within C passthrough code, it must be
;; defined to be global (because only then it is visible to an
;; external C program) in the same buffer.
;;
;; This mode runs `gas-mode-hook' when initialization is complete.
;;
;; Bugs:
;;
;; Most probably, yes. You'll tell me (<[email protected]>).
;;
;; This code is *not tested at all* for syntaxes where
;; `gas-commant-char' differs from `?;'.
;;; Change Log:
;;
;; 2007-05-26 1st public release (hcz).
;;
;; 2007-05-30 docstrings, commentary,
;; open string recognition with gas-next-token/limit-re.
;;
;; 2007-12-05 Bug with Intel syntax in arguemnt field fixed
;;
;; 2007-12-25 Indents fixed for C comments without closing '*/'
;;
;;; Code:
(defgroup gas nil
"Mode for editing gas syntax assembler code."
:link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
:group 'languages)
(defcustom gas-comment-char ?\;
"The comment start character assumed by gas mode."
:type 'character
; :set 'gas-set-gas-comment-char
:group 'gas)
(defcustom gas-opcode-column 14
"The opcode column."
:type 'integer
:group 'gas)
(defcustom gas-argument-column 20
"The column for the arguments (if any)."
:type 'integer
:group 'gas)
(defcustom gas-comment-column 36
"The column where end of line asm comments go to."
:type 'integer
:group 'gas)
(defcustom gas-comment-char-starts-comment nil
"Always jump to comment column when a variable `gas-comment-char' is typed.
It t, starts/expands a comment if appropriate. When
unset (nil), use `gas-comment-dwim' (usually bound to M-;) to get there."
:type 'boolean
:group 'gas)
(defcustom gas-indent-current-field-only nil
"If nil, 'indent' indents all fileds on the current line.
Else only the current field is affected."
:type 'boolean
:group 'gas)
;; (defcustom gas-preserve-trailing-whitespace nil
;; "If nil, (re-)indenting removes trailing white space."
;; :type 'boolean
;; :group 'gas)
(setq gas-preserve-trailing-whitespace nil) ; (currently?) non-functional.
(defcustom gas-enable-symbol-highlight t
"Enable symbol recognition and highlighing.
When t and if point is on a symbol, some limited parsing data is
collected and all occurences of this symbol in the buffer get
highlighted according to the results (defined, global, etc.). As
long as a symbol is highlighted, `forward-sexp' and `backward-sexp'
move to the next/previous occurence of the same symbol in the
same buffer."
:type 'boolean
:group 'gas)
(defcustom gas-use-C-passthrough t
"When true, C passthrough comments are recognized.
This kind of comment is introduced by the starting sequence
\"/*C\" and is meant to be processed later by an external
program \(see the introducing comment in gas-mode.el for an
example) into C source code. Within these passthrough-comments,
the combination /# ... #/ is available for nested comments which
will be later changed into real C comments (/* ... */) by the
same external program."
:type 'boolean
:group 'gas)
(defcustom gas-defun-regexp "\n\\([;#].*\\|.*[*]/[ \t]*\\|\\|[ \t]*\\|[ \t]+\\..*\\)\n\\([^ \t\n;]+:\\)"
"Regexp used to recognize the beginning of a defun.
The default value describes a line which is either empty, a
full-line left-justified comment or a directive, followed by a
line starting with a label. Note that the character \";\" in the
regexp will be replaced by the actual comment character described
by variable `gas-comment-char'."
:type 'regexp
; :set 'gas-set-comment-regexp
:group 'gas)
(defcustom gas-defun-regexp-subexp 2
"The subexp in `gas-defun-regexp to jump to."
:type 'integer
:group 'gas)
(defcustom gas-C-indent 3
"Indent to use with C style comments."
:group 'gas
:type 'integer)
(defcustom gas-C-comment-end-column 0
"Where to indent a C comment end (\"*/\") if it starts a line."
:group 'gas
:type 'integer)
(defcustom gas-symbol-highlight-delay 0.5
"After this many seconds symbols get highlighted.
Number of seconds of idle time (a float) to wait before a symbol
gets highlighted."
:group 'gas
:type 'float)
(defgroup gas-faces nil
"Faces used by gas-mode."
:group 'gas)
(defface gas-builtin
'((((class color) (background light)) (:foreground "maroon"))
(t (:foreground "yellow")))
"Face to use for Gas buitins."
:group 'gas-faces)
(defface gas-symbol-ok
'((((class color) (background light)) (:background "#e0ffe0"))
(((class color) (background dark)) (:background "#001f00"))
(t (:foreground "yellow" :background "blue")))
"Face to use for symbols where exactly 1 definition was found."
:group 'gas-faces)
(defface gas-symbol-error
'((((class color) (background light))
(:background "#ffffe8" :foreground "red" :weight bold))
(((class color) (background dark))
(:background "#181800" :foreground "red" :weight bold))
(t (:foreground "yellow" :background "red")))
"Face to use when highlighting symbols with more than 1 definition."
:group 'gas-faces)
(defface gas-symbol-global
'((((class color) (background light)) (:background "#d0f8ff"))
(((class color) (background dark)) (:background "#00383f"))
(t (:foreground "yellow" :background "blue")))
"Face to use when highlighting global symbols."
:group 'gas-faces)
(defface gas-symbol-undef
'((((class color) (background light))
(:background "#ffffe8" :foreground "maroon"))
(((class color) (background dark))
(:background "#181800"))
(t (:foreground "yellow" :background "red")))
"Face to use for symbols defined as global when no definition
was found."
:group 'gas-faces)
(defface gas-symbol-global-undef
'((((class color) (background light))
(:background "#d0f8ff" :foreground "red"))
(((class color) (background dark))
(:background "#002840" :foreground "red"))
(t (:foreground "yellow" :background "red")))
"Face to use for symbols when no definition is found."
:group 'gas-faces)
(defface gas-passthrough-code
'((((class color) (background light)) (:foreground "magenta4"))
(((class color) (background dark)) (:foreground "magenta2"))
(t (:foreground "magenta1" :background "cyan")))
"Marks passthrough code."
:group 'gas-faces)
(defface gas-passthrough-comment
'((((class color) (background light)) (:foreground "turquoise4"))
(((class color) (background dark)) (:foreground "turquoise2"))
(t (:foreground "turquoise1")))
"Marks passthrough comments."
:group 'gas-faces)
(defvar gas-builtin-face 'gas-builtin)
(defvar gas-symbol-ok-face 'gas-symbol-ok)
(defvar gas-symbol-error-face 'gas-symbol-error)
(defvar gas-symbol-global-face 'gas-symbol-global)
(defvar gas-symbol-undef-face 'gas-symbol-undef)
(defvar gas-symbol-global-undef-face 'gas-symbol-global-undef)
(defvar gas-passthrough-code-face 'gas-passthrough-code)
(defvar gas-passthrough-comment-face 'gas-passthrough-comment)
(defconst gas-max-lines-in-cache 500
"Maximum number of parsed lines in cache.
I don't expect much impact from this on performance (the line
cache is emptied on any buffer change anyway). Play around with
this value if you suspect memory may be your problem.")
(defconst gas-max-labels-in-cache 300
"Maximum number of symbols in highlight cache.
Reduce this if memory footprint grows too high (very unlikely).")
(defconst gas-re-sym "\\([_$A-Za-z][_0-9$A-Za-z]*\\)"
"Regexp defining a valid symbol as a subexpression.")
(defconst gas-skip-sym "_0-9$A-Za-z"
"The valid characters for a symbol as used in `skip-chars-*' functions.")
(defconst gas-re-nosym"[^_0-9$A-Za-z]"
"Regexp defining the character set not allowed in a symbol.")
(defvar gas-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\n "> b" st)
(modify-syntax-entry ?. "_" st)
(modify-syntax-entry ?/ ". 124b" st)
(modify-syntax-entry ?* ". 23" st)
st)
"Syntax table used while in gas mode.")
(defvar gas-mode-abbrev-table nil
"Abbrev table used while in Gas mode.")
(define-abbrev-table 'gas-mode-abbrev-table ())
(defvar gas-mode-map
(let ((map (make-sparse-keymap)))
;; Note that the comment character isn't set up until gas-mode is called.
(define-key map ":" 'gas-colon)
(define-key map "\M-;" 'gas-comment)
(define-key map ";" 'gas-comment-char)
(define-key map "#" 'gas-hash)
(define-key map (kbd "<S-iso-lefttab>") 'gas-indent-backward)
(define-key map "\C-c;" 'comment-region)
(define-key map "\C-j" 'newline-and-indent)
(define-key map "\C-m" 'newline-and-indent)
map)
"Keymap for Gas mode.")
(defconst gas-equ (regexp-opt '(".equ" ".set" ".eqv" ".equiv" ".set"))
"Regex matching all operators which define a symbol.")
(defconst gas-indents
'((C-comment-end . gas-C-comment-end-column)
(C-comment-start . 0)
(C-comment . gas-get-C-relative-indent)
(cpp-macro-def . 0)
(cpp-argument . 0)
(label . 0)
(opcode . gas-opcode-column)
(argument . gas-argument-column)
(asm-comment . gas-get-asm-comment-column))
"Fields and their indents.
The cdr (the indent) may either be a number, a symbol bound to a
number, or a symbol bound to a function yielding the value.")
(defconst gas-parse-sequences
'(
(starting-asm-line
. ((cpp-macro-def . cpp-macro-def)
(label . label)
(empty-label . label)
(asm-comment . asm-comment)
(opcode . opcode)))
(label
. ((opcode . opcode)
(asm-comment . asm-comment)
(garbage . garbage)))
(opcode
. ((asm-comment . asm-comment)
(argument . argument)
(eol-ws . eol-ws)
(garbage . garbage)))
(argument
. ((asm-comment . asm-comment)
(eol-ws . eol-ws)
(garbage . garbage)))
(asm-comment
. ((eol-ws . eol-ws)))
(cpp-argument
. ((eol-ws . eol-ws)
(garbage . garbage)))
(cpp-macro-def
. ((cpp-argument . cpp-argument)
(garbage . garbage)))
(cpp-argument
. ((eol-ws)
(garbage . garbage)))
(C-comment-start
. ((C-comment . C-comment)))
(starting-within-C-comment
. ((C-comment-end . C-comment-end)
(C-comment . C-comment)))
(C-comment
. ((C-comment-end . C-comment-end)
(eol-ws . eol-ws)
(garbage . garbage)))
(garbage
. ((eol-ws . eol-ws)))
(eol-ws
. fini))
"Mapping from the field type we're on to the field types to check next.
car - type of field we're on (IOW, the one just handled)
cdr - ordered list of (first (most special) check first):
car - token to match
cdr - next field type iff match." )
(defconst gas-patterns
'((cpp-macro-def
"[ \t]*\\(\\(#[^ \t\n]*\\)\\)"
(0 1 1 1)
"/[*]")
(cpp-argument
"[ \t]*\\(\\([ \t]*\\([^ \t\n]\\)+\\)+\\)"
(0 1 1 1)
"/[*]")
(label
"[ \t]*\\([^ :\t\n]+:\\)"
(0 1 1 1)
"/[*]\\|;")
(opcode
"[ \t]*\\([^ \t\n]+\\)"
(0 1 1 1)
"/[*]\\|;")
(argument
"[ \t]*\\(\\([ \t]*\\([^ \t\n]\\)+\\)+\\)"
(0 1 1 1)
"/[*]\\|;")
(garbage
"[ \t]*\\(\\([ \t]*\\([^ \t\n]\\)+\\)+\\)"
(0 1 1 1)
"/[*]\\|;")
(asm-comment
"[ \t]*\\(\\(;+\\)\\([ \t]*[^ \t\n]+\\)*\\)"
(0 1 1 1)
nil)
(C-inline-comment
"[ \t]*\\(/[*].*?[*]/\\)"
(0 1 1 1)
nil)
(C-comment-start
"[ \t]*\\(/[*]C?\\)"
(0 1 1 1)
nil)
(empty-label
;; a field of at least 1 white space: no fill at start, nil
;; text field at start, fill at end
" [ \t]*"
(0 0 0 nil)
"/[*]\\|;")
(C-comment-end
"[ \t]*\\([*]/\\)"
(0 1 1 1)
nil)
(C-comment
;; match the entire line (except for trailing whitespace)
"[ \t]*\\(\\([ \t]*[^ \t]+\\)*\\)"
(0 1 1 1)
"[*]/")
;; fill only: zero or more white space, nil text field at
;; end
(eol-ws
"[ \t]+\\( ?\\)"
(0 1 1 1) ; last element always empty
nil))
"An alist of parse patterns.
Each entry holds 4 elements (SYMBOL REGEXP SUBEXPS TERMINATE-RE):
SYMBOL - designator (a symbol) under which it will be
referenced.
REGEXP - the regexp to match against,
SUBEXPS - a list (BEG-COL TEXT-COL END-COL END-OF-FIELD) of at
which subexpression of REGEXP to find beg-col, text-col,
end-col (see `gas-parse-line-really') and the end of the
field,
TERMINATE-RE - a regexp, the start of which (if it matches and
if outside a \"..\" string) unconditionally terminates the
field.
Every occurence of the character \";\" in both regexps is
replaced by variable `gas-comment-char' before use.")
(defconst gas-elmt-types
'(type subtype beg-col text-col end-col text modified)
"The elements of a gas syntax field.")
(defconst gas-builtin-keywords (concat "^\\(\\(\\sw\\|\\s_\\)+:?\\)?[ \t]+\\("
(regexp-opt '(
".Abort" ".ABORT" ".Align" ".Altmacro" ".Ascii" ".Asciz"
".Balign" ".Byte" ".Comm"
".Data" ".Def" ".Desc" ".Dim" ".Double" ".Eject"
".Else" ".Elseif" ".End" ".Endef" ".Endfunc" ".Endif"
".Equ" ".Equiv" ".Eqv"
".Err" ".Error" ".Exitm" ".Extern" ".Fail"
".File" ".Fill" ".Float" ".Func"
".Global" ".Hidden" ".hword" ".Ident"
".If" ".ifb" ".ifc" ".ifeq" ".ifeqs"
".ifge" ".ifle" ".ifgt" ".iflt"
".ifnb" ".ifnc" ".ifndef" ".ifdef" ".ifnotdef" ".ifne" ".ifnes"
".Incbin" ".Include" ".Int"
".Internal" ".Irp" ".Irpc" ".Lcomm" ".Lflags" ".Line"
".Linkonce" ".List" ".Ln"
".Long" ".Macro" ".MRI" ".Noaltmacro"
".Nolist" ".Octa" ".Org" ".P2align" ".PopSection" ".Previous"
".Print" ".Protected" ".Psize" ".Purgem" ".PushSection"
".Quad" ".Rept" ".Sbttl" ".Scl" ".Section" ".Set" ".Short"
".Single" ".Size" ".Skip" ".Sleb128" ".Space" ".Stab" ".String"
".Struct" ".SubSection" ".Symver" ".Tag" ".Text" ".Title" ".Type"
".Uleb128" ".Val" ".Version" ".VTableEntry" ".VTableInherit"
".Warning" ".Weak" ".Weakref" ".Word" ".Deprecated"))
"\\)[ \t\n]"))
(defvar gas-font-lock-keywords
(append
(list
'(gas-return-passthrough-code-hi . (0 gas-passthrough-code-face t))
'(gas-return-passthrough-comment-hi . (0 gas-passthrough-comment-face t))
'(gas-return-gas-hi-ok . (0 gas-symbol-ok-face t))
'(gas-return-gas-hi-global . (0 gas-symbol-global-face t))
'(gas-return-gas-hi-error . (0 gas-symbol-error-face t))
'(gas-return-gas-hi-undef . (0 gas-symbol-undef-face t))
'(gas-return-gas-hi-global-undef . (0 gas-symbol-global-undef-face t))
(list gas-builtin-keywords 3 'gas-builtin-face)
'("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?" 1 font-lock-function-name-face))
cpp-font-lock-keywords)
"Additional expressions to highlight in gas mode.")
(put 'gas-mode 'font-lock-defaults '(gas-font-lock-keywords))
;;;###autoload
(defun gas-mode ()
"Major mode for editing assembler code.
Commands:
\(Some of these commands may exhibit slightly different behaviour if point
is on a C syntax line.)
\\[indent-for-tab-command] indent the field(s) point is on. If it already is in its position,
move on to the next field on the line.
\\[gas-indent-backward] Move to the previous field.
\\[gas-comment] When no region is active, starts a comment sequence:
- If a comment is present and point is not at its start, jump there.
- Else start a comment. If there already is one, increase its comment level.
What that is and what it does, is best explained when you try
it out: Move to en empty line, then type \\[gas-comment] and
then repeatedly \\[gas-comment] or \\[gas-comment-char].
Calls \\[gas-comment-dwim] (see below) if the region is active.
\\[forward-sexp] If you're on a highlighted symbol, jump to its next
occurence. Else do `forward-sexp' like in text mode.
\\[backward-sexp] If you're on a highlighted symbol, jump to its previous
occurence. Else do `backward-sexp' like in text mode.
\\[gas-comment-dwim] If the region starts at the leading white space
before a comment, all full-line comments in region will be
removed. If the region starts on a comment, comments will be
removed, but comments with leading white space will be left
untouched. Else insert triple variable `gas-comment-char's before all
lines in region.
\\[fill-paragraph] beautifies the paragraph around
point, i.e. it adjusts all assembly syntax fields to their
standard positions.
\\[indent-region] beautifies the region, i.e., adjusts all fields in region.
The following characters have a special meaning in special cases:
\\[gas-colon] if it terminates a label: outdent the label and
move to opcode column. Else, just insert \\[gas-colon] as usual.
\\[gas-comment-char] The value which introduces an asm style comment.
If typed in in a row after \\[gas-comment], behaves as an alias to \\[gas-comment]. else
just insert \\[gas-comment-char].
Can be customized to always act as alias (`gas-comment-char-starts-comment').
Alternatively, you may use a File Variable to make it buffer local
(which allows you to use different syntaxes in the same session).
Note: Setting it to a value other than ?\; has not yet been tested.
\\[gas-hash] If it starts a preprocessor directive: Outdent it to first
column.
Customization: Entry on this mode runs `gas-mode-hook'.
The customization group is called 'gas'.
Special commands:
\\{gas-mode-map}"
(interactive)
(kill-all-local-variables)
(setq mode-name "gas")
(setq major-mode 'gas-mode)
(setq local-abbrev-table gas-mode-abbrev-table)
(setq gas-comment-string (string gas-comment-char))
(setq gas-comment-re (regexp-quote gas-comment-string))
(set (make-local-variable 'indent-line-function) 'gas-indent)
(set (make-local-variable 'indent-region-function) 'gas-indent-region)
(set (make-local-variable 'forward-sexp-function) 'gas-forward-sexp)
(set (make-local-variable 'fill-paragraph-function) 'gas-fill-paragraph)
(set (make-local-variable 'font-lock-defaults) '(gas-font-lock-keywords))
(set (make-local-variable 'gas-local-comment-char) gas-comment-char)
(set (make-local-variable 'beginning-of-defun-function)
'gas-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function)
'gas-end-of-defun)
(set (make-local-variable 'font-lock-keywords-case-fold-search) t)
(use-local-map (nconc (make-sparse-keymap) gas-mode-map))
(local-set-key (vector gas-comment-char) 'gas-comment-char)
(set-syntax-table (make-syntax-table gas-mode-syntax-table))
(modify-syntax-entry gas-comment-char "< b")
(dolist (var '(gas-line-cache gas-globals-cache gas-locals-cache
gas-hi-valid gas-hi-sym-list gas-hi-global gas-hi-undef
gas-hi-error gas-hi-ok gas-changed gas-highlights gas-hi
gas-pass-code-hi gas-pass-comment-hi gas-symbol-timer
gas-highlights-error gas-symbol-highlight-beg
gas-symbol-highlight-end gas-doing-comment
after-change-functions))
(set (make-local-variable var) nil))
(add-to-list 'after-change-functions 'gas-after-change)
(add-hook 'pre-command-hook 'gas-symbol-pre-command)
(gas-start-symbol-timer)
(run-mode-hooks 'gas-mode-hook)
;; scan buffer for extra regions to highlight:
(gas-symbol-highlight))
(defun gas-dbg ()
"You won't need this unless you're debugging `gas-mode'."
(interactive)
(setq debug-on-error t)
;(setq debug-on-quit t)
(setq debug-items '(indent))
(switch-to-buffer-other-frame "*Messages*")
(switch-to-buffer-other-frame "*scratch*")
(info "Elisp")
(switch-to-buffer-other-frame "*scratch*")
(find-file-other-frame "x.S")
(switch-to-buffer-other-frame "x.S")
(switch-to-buffer-other-frame (get-buffer-create "*gas-dbg*"))
(find-file-other-frame "gas-mode.el")
(column-number-mode t)
(switch-to-buffer-other-frame "gas-mode.el"))
(defun dmsg (condition &rest args)
"Helper function, outputs debug messages into a buffer of their own.
If DEBUG-ITEMS (a symbol or a list) has a non-empty
intersection with CONDITION (a symbol or a list)', apply `format'
to ARGS and insert the result at the end of the buffer
`gas-dbg' (which is created if non-existing).
Currently defined symbols are: 'wip (work in progress),
'hi (highlighting), 'parser, 'cursor, 'sym (symbol highlighting),
'cursor."
(when (and (boundp 'debug-items) debug-items)
(when (not (listp condition))
(setq condition (list condition)))
(when (not (listp debug-items))
(setq debug-items (list debug-items)))
(when (or (eq condition '(all))
(catch 'found
(dolist (c condition)
(when (member c debug-items)
(throw 'found t)))))
(let ((contents (apply 'format args)))
(save-current-buffer
(set-buffer (get-buffer-create "*gas-dbg*"))
(goto-char (point-max))
(newline)
(insert contents)
(goto-char (point-max))
;(recenter -1)
)))))
(defun gas-change-comment-regexp (str)
"Return STR with all \";\"s replaced by (regexp-quote variable `gas-comment-char')."
(when str
(setq str (replace-regexp-in-string
".*\\[[^]]*\\(;\\)" gas-comment-string str t t 1))
(replace-regexp-in-string ";" (regexp-quote gas-comment-string) str t t)))
(defun gas-change-comment-string (str)
"Return STR with all \";\"s replaced by variable `gas-comment-char'."
(when str
(replace-regexp-in-string ";" gas-comment-string str t t)))
(defun gas-set-patterns (comment-char)
"Replace `gas-patterns' by a copy, replacing ?\; by COMMENT-CHAR.
Also sets variable `gas-comment-char' to COMMENT-CHAR."
(set (make-local-variable 'gas-comment-char) comment-char)
(set (make-local-variable 'gas-comment-string) (string gas-comment-char))
(set (make-local-variable 'gas-comment-re) (regexp-quote gas-comment-string))
(kill-local-variable 'gas-patterns)
(let (result)
(dolist (pattern gas-patterns)
(add-to-list 'result
(list
(car pattern)
(gas-change-comment-regexp (nth 1 pattern))
(nth 2 pattern)
(gas-change-comment-regexp (nth 3 pattern)))))
(set (make-local-variable 'gas-patterns) result)
(dolist (sym '(gas-defun-regexp))
(kill-local-variable sym)
(let ((result (gas-change-comment-regexp (eval sym))))
(set (make-local-variable sym) result)))))
(defun gas-after-change (beg end len)
"Invalidate saved parser state.
Argument BEG BEG, END, and LEN, athough saved in `gas-changed' for debugging purposes, are not used."
(setq gas-globals-cache nil)
(setq gas-hi-valid nil)
(setq gas-locals-cache nil)
(setq gas-changed (append gas-changed (list (current-buffer) beg end len))))
(defun gas-symbol-pre-command()
(setq gas-hi nil))
(defun gas-start-symbol-timer (&optional stop)
"Schedule a timer for symbol highlighting (if not already scheduled).
Optional STOP, if non-nil, means remove from schedule."
(if (and stop gas-symbol-timer)
(progn
(cancel-timer gas-symbol-timer)
(setq gas-symbol-timer nil))
(unless (and gas-symbol-timer
(memq gas-symbol-timer timer-idle-list))
(setq gas-symbol-timer
(run-with-idle-timer gas-symbol-highlight-delay
t
'gas-symbol-highlight-maybe)))))
(defun gas-return-passthrough-hi (pos what)
"Return next passthrough match (if any).
See highlight.el for POS and WHAT."
(let ((curpoint (point)))
(catch 'found
(dolist (match what)
(when (and (< curpoint (cdr match))
(> pos (car match)))
(when (< curpoint (car match))
(goto-char (car match)))
(when (re-search-forward ".+" (min pos (cdr match)) t)
(dmsg 'hi "match: %s, data: '%s'" match (match-string 0))
(throw 'found t)))
(when (>= (car match) pos)
(throw 'found nil))))))
(defun gas-return-passthrough-code-hi (pos)
"Return next passthrough code match (if any).
See highlight.el for documentation on POS."
(gas-return-passthrough-hi pos gas-pass-code-hi))
(defun gas-return-passthrough-comment-hi (pos)
"Return next passthrough comment match (if any).
See highlight.el for documentaion on POS ."
(gas-return-passthrough-hi pos gas-pass-comment-hi))
(defun gas-passthrough-highlight ()
"Compute blocks of C passthroughs to be highlighted."
(goto-char (point-min))
(setq gas-pass-code-hi nil)
(setq gas-pass-comment-hi nil)
(let (beg end limit)
(while (and (re-search-forward "/[*]C" nil t)
(setq beg (point))
(setq limit
(save-excursion
(and (re-search-forward "[*]/" nil t)
(- (point) 2)))))
(while (and beg
(setq end (and (re-search-forward "/#" limit t)
(point))))
(add-to-list 'gas-pass-code-hi (cons beg (- end 2)) t)
(forward-char 2)
(when
(setq beg (and (re-search-forward "#/" limit t) (point)))
(add-to-list 'gas-pass-comment-hi (cons (- end 2) beg) t)))
(when beg
(add-to-list 'gas-pass-code-hi (cons beg limit) t)))))
(defun gas-return-highlight (pos hi-list)
"Called through the gas-return-gas-hi-* functions by highlight.el.
If there's a match of POS against one of the entries in HI-LIST,
return match data. Else, return nil."
(when gas-hi-valid
(dmsg 'hi "gas-return-highlight, pos: %s, point: %s, list: %s" pos (point) hi-list)
(catch 'found
(dolist (match hi-list)
(if (< (car match) pos)
(when (>= (car match) (point))
(dmsg 'hi "gas-return-highlight, match: %s" match)
(goto-char (car match))
(throw 'found (re-search-forward ".+" (min pos (cadr match)) t)))
(dmsg 'hi "gas-return-highlight: nope.")
(throw 'found nil)
nil)))))
(defun gas-return-gas-hi-global (pos)
"Check POS against the entries in the list `gas-hi-global'.
Called by highlight.el."
(gas-return-highlight pos gas-hi-global))
(defun gas-return-gas-hi-undef (pos)
"Check POS against the entries in the list `gas-hi-undef'.
Called by highlight.el."
(gas-return-highlight pos gas-hi-undef))
(defun gas-return-gas-hi-error (pos)
"Check POS against the entries in the list `gas-hi-error'.
Called by highlight.el."
(gas-return-highlight pos gas-hi-error))
(defun gas-return-gas-hi-ok (pos)
"Check POS against the entries in the list `gas-hi-ok'.
Called by highlight.el."
(gas-return-highlight pos gas-hi-ok))
(defun gas-return-gas-hi-global-undef (pos)
"Check POS against the entries in the list `gas-hi-undef'.
Called by highlight.el."
(gas-return-highlight pos gas-hi-global-undef))
(defun gas-qualify-symbol (sym-re slist lflags)
"The common part of `gas-scan-global-symbol and `gas-scan-local-symbol'.
Gets called with point on a line where a label match may be
found. It expects SYM-RE to be a regexp describing the
label. Adds on match the match it finds to SLIST (a symbol bound
to a list of matches) and the kind of match (as a symbol, like
'def for a definition or 'duplicate for a duplicate definition)
to LFLAGS."
(save-excursion
(let ((eol (line-end-position))
(bol (line-beginning-position)))
(setq case-fold-search nil)
(if (gas-C-comment-p)
(progn
(backward-char)
(while (re-search-forward
(format "%s\\(%s\\)\\(%s\\|$\\)"
gas-re-nosym sym-re gas-re-nosym)
eol t)
(let ((beg (match-beginning 1))
(end (match-end 1)))
(when (gas-C-passthrough-code-p)
(add-to-list lflags 'C-ref t)
(add-to-list slist (list 'C-ref beg end) t)))))
;; not C style:
(let* ((fields (gas-parsed))
(lbl (gas-nth 'text 'label fields))
(arg (gas-nth 'text 'argument fields))
(type 'ref)
(argno 0))
(when (and lbl
(string-match (concat "^" sym-re ":?$") lbl))
(setq nlabels (1+ nlabels))
(if (member 'def (eval lflags))
(add-to-list lflags 'duplicate t)
(add-to-list lflags 'def t))
(move-to-column 0)
(looking-at "[^:]+:")
(add-to-list slist
(list 'def (match-beginning 0) (match-end 0)) t))
(when arg
(move-to-column (- (gas-nth 'text-col 'argument fields) 1))
(let ((type 'ref)
(eo-arg (+ bol (gas-nth 'end-col 'argument fields) 2))
(opcode (gas-nth 'text 'opcode fields)))
(while (re-search-forward
(format "%s\\(%s\\)\\(%s\\|$\\)"
gas-re-nosym sym-re gas-re-nosym)
eo-arg t)
(let ((beg (match-beginning 1))
(end (match-end 1)))
;; first argument?
(if (and (= argno 0) ; yes
(string-match gas-equ opcode)) ; assignment?
(progn
(setq type 'def) ; yes
(if (member 'def (eval lflags))
(add-to-list lflags 'duplicate t)
(add-to-list lflags 'def t)))
(add-to-list lflags 'ref t)) ; no
(when (string-match ".global" opcode)
(add-to-list lflags 'global t))
(add-to-list slist (list type beg end) t)
(setq argno (1+ argno))))))))))
;; skip past parsed part:
(end-of-line))
(defun gas-scan-global-symbol (sym)
"Scan the buffer vor valid occurences of the global symbol SYM.
Called by `gas-symbol-highlight'."
(goto-char (point-min))
(let* (sym-list
flags
qualifiers
(nlabels 0)
(sym-re (regexp-quote sym))
(re (format "\\(^\\|%s\\)\\(%s\\)\\(%s\\|$\\)"
gas-re-nosym sym-re gas-re-nosym)))
(while (re-search-forward re nil t)
(when (input-pending-p)
(throw 'event-abort nil))
(goto-char (match-beginning 2))
(dmsg 'sym "global: qualified1: %s" sym-list)
(gas-qualify-symbol sym-re 'sym-list 'flags))
(dmsg 'sym "global: sym-list: %s" sym-list)
(unless (equal sym-list '(nil))
(list sym flags sym-list))))
(defun gas-scan-local-symbol (orig-sym)
"Scan the buffer vor valid occurences of the local symbol SYM.
Called by `gas-symbol-highlight'.
Argument ORIG-SYM is the complete symbol (as written)."
(when (string-match "^\\(.*\\)\\([:bf]\\)" orig-sym)
(let* ((search-lo (point-min)) ; location of previous duplicate
search-mid ; label pos
(search-hi (point-max)) ; location of next duplicate
(sym (match-string 1 orig-sym))
(sym-kind (match-string 2 orig-sym))
(sym-re (regexp-quote sym))
(lbl-re (concat "^\\(" sym-re ":" "\\)" ))
(nlabels 0)
sym-list
flags
qualifiers
searches)
;; determine region where the label is valid
(save-excursion
(when (equal sym-kind "b")
(re-search-backward lbl-re nil t)) ; skip label
(when (re-search-backward lbl-re nil t)
(setq search-lo (match-end 1)))
(goto-char search-lo)
(when (re-search-forward lbl-re nil t)
(setq search-mid (match-beginning 1)))
(when (re-search-forward lbl-re nil t)
(setq search-hi (match-beginning 1))))
(let ((lo search-hi) ; first match
(hi search-lo) ; end of last match
(search-params
(if search-mid
(list
(list (concat sym-re "f") search-lo search-mid)
(list (concat sym-re ":")
search-mid (+ search-mid (length sym) 2))
(list (concat sym-re "b") search-mid search-hi))
(list
(list (concat sym-re "f") search-lo search-hi)
(list (concat sym-re "b") search-lo search-hi)))))
(dolist (param search-params)
(when (input-pending-p)
(throw 'event-abort nil))
(let* ((sym-re (car param))
(search-re (concat gas-re-nosym
"\\(" sym-re "\\)" gas-re-nosym))
(limit (caddr param)))
(goto-char (- (cadr param) 1))
(while (and (< (point) limit)
(re-search-forward search-re limit t))
(setq lo (min lo (match-beginning 1)))
(setq hi (max hi (match-end 1)))
(goto-char (match-beginning 1))
(gas-qualify-symbol sym-re 'sym-list 'flags))))
(dmsg 'sym "local: sym-list: %s" sym-list)