You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
8.9 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("gas", function(_config, parserConfig) {
  13. 'use strict';
  14. // If an architecture is specified, its initialization function may
  15. // populate this array with custom parsing functions which will be
  16. // tried in the event that the standard functions do not find a match.
  17. var custom = [];
  18. // The symbol used to start a line comment changes based on the target
  19. // architecture.
  20. // If no architecture is pased in "parserConfig" then only multiline
  21. // comments will have syntax support.
  22. var lineCommentStartSymbol = "";
  23. // These directives are architecture independent.
  24. // Machine specific directives should go in their respective
  25. // architecture initialization function.
  26. // Reference:
  27. // http://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops
  28. var directives = {
  29. ".abort" : "builtin",
  30. ".align" : "builtin",
  31. ".altmacro" : "builtin",
  32. ".ascii" : "builtin",
  33. ".asciz" : "builtin",
  34. ".balign" : "builtin",
  35. ".balignw" : "builtin",
  36. ".balignl" : "builtin",
  37. ".bundle_align_mode" : "builtin",
  38. ".bundle_lock" : "builtin",
  39. ".bundle_unlock" : "builtin",
  40. ".byte" : "builtin",
  41. ".cfi_startproc" : "builtin",
  42. ".comm" : "builtin",
  43. ".data" : "builtin",
  44. ".def" : "builtin",
  45. ".desc" : "builtin",
  46. ".dim" : "builtin",
  47. ".double" : "builtin",
  48. ".eject" : "builtin",
  49. ".else" : "builtin",
  50. ".elseif" : "builtin",
  51. ".end" : "builtin",
  52. ".endef" : "builtin",
  53. ".endfunc" : "builtin",
  54. ".endif" : "builtin",
  55. ".equ" : "builtin",
  56. ".equiv" : "builtin",
  57. ".eqv" : "builtin",
  58. ".err" : "builtin",
  59. ".error" : "builtin",
  60. ".exitm" : "builtin",
  61. ".extern" : "builtin",
  62. ".fail" : "builtin",
  63. ".file" : "builtin",
  64. ".fill" : "builtin",
  65. ".float" : "builtin",
  66. ".func" : "builtin",
  67. ".global" : "builtin",
  68. ".gnu_attribute" : "builtin",
  69. ".hidden" : "builtin",
  70. ".hword" : "builtin",
  71. ".ident" : "builtin",
  72. ".if" : "builtin",
  73. ".incbin" : "builtin",
  74. ".include" : "builtin",
  75. ".int" : "builtin",
  76. ".internal" : "builtin",
  77. ".irp" : "builtin",
  78. ".irpc" : "builtin",
  79. ".lcomm" : "builtin",
  80. ".lflags" : "builtin",
  81. ".line" : "builtin",
  82. ".linkonce" : "builtin",
  83. ".list" : "builtin",
  84. ".ln" : "builtin",
  85. ".loc" : "builtin",
  86. ".loc_mark_labels" : "builtin",
  87. ".local" : "builtin",
  88. ".long" : "builtin",
  89. ".macro" : "builtin",
  90. ".mri" : "builtin",
  91. ".noaltmacro" : "builtin",
  92. ".nolist" : "builtin",
  93. ".octa" : "builtin",
  94. ".offset" : "builtin",
  95. ".org" : "builtin",
  96. ".p2align" : "builtin",
  97. ".popsection" : "builtin",
  98. ".previous" : "builtin",
  99. ".print" : "builtin",
  100. ".protected" : "builtin",
  101. ".psize" : "builtin",
  102. ".purgem" : "builtin",
  103. ".pushsection" : "builtin",
  104. ".quad" : "builtin",
  105. ".reloc" : "builtin",
  106. ".rept" : "builtin",
  107. ".sbttl" : "builtin",
  108. ".scl" : "builtin",
  109. ".section" : "builtin",
  110. ".set" : "builtin",
  111. ".short" : "builtin",
  112. ".single" : "builtin",
  113. ".size" : "builtin",
  114. ".skip" : "builtin",
  115. ".sleb128" : "builtin",
  116. ".space" : "builtin",
  117. ".stab" : "builtin",
  118. ".string" : "builtin",
  119. ".struct" : "builtin",
  120. ".subsection" : "builtin",
  121. ".symver" : "builtin",
  122. ".tag" : "builtin",
  123. ".text" : "builtin",
  124. ".title" : "builtin",
  125. ".type" : "builtin",
  126. ".uleb128" : "builtin",
  127. ".val" : "builtin",
  128. ".version" : "builtin",
  129. ".vtable_entry" : "builtin",
  130. ".vtable_inherit" : "builtin",
  131. ".warning" : "builtin",
  132. ".weak" : "builtin",
  133. ".weakref" : "builtin",
  134. ".word" : "builtin"
  135. };
  136. var registers = {};
  137. function x86(_parserConfig) {
  138. lineCommentStartSymbol = "#";
  139. registers.al = "variable";
  140. registers.ah = "variable";
  141. registers.ax = "variable";
  142. registers.eax = "variable-2";
  143. registers.rax = "variable-3";
  144. registers.bl = "variable";
  145. registers.bh = "variable";
  146. registers.bx = "variable";
  147. registers.ebx = "variable-2";
  148. registers.rbx = "variable-3";
  149. registers.cl = "variable";
  150. registers.ch = "variable";
  151. registers.cx = "variable";
  152. registers.ecx = "variable-2";
  153. registers.rcx = "variable-3";
  154. registers.dl = "variable";
  155. registers.dh = "variable";
  156. registers.dx = "variable";
  157. registers.edx = "variable-2";
  158. registers.rdx = "variable-3";
  159. registers.si = "variable";
  160. registers.esi = "variable-2";
  161. registers.rsi = "variable-3";
  162. registers.di = "variable";
  163. registers.edi = "variable-2";
  164. registers.rdi = "variable-3";
  165. registers.sp = "variable";
  166. registers.esp = "variable-2";
  167. registers.rsp = "variable-3";
  168. registers.bp = "variable";
  169. registers.ebp = "variable-2";
  170. registers.rbp = "variable-3";
  171. registers.ip = "variable";
  172. registers.eip = "variable-2";
  173. registers.rip = "variable-3";
  174. registers.cs = "keyword";
  175. registers.ds = "keyword";
  176. registers.ss = "keyword";
  177. registers.es = "keyword";
  178. registers.fs = "keyword";
  179. registers.gs = "keyword";
  180. }
  181. function armv6(_parserConfig) {
  182. // Reference:
  183. // http://infocenter.arm.com/help/topic/com.arm.doc.qrc0001l/QRC0001_UAL.pdf
  184. // http://infocenter.arm.com/help/topic/com.arm.doc.ddi0301h/DDI0301H_arm1176jzfs_r0p7_trm.pdf
  185. lineCommentStartSymbol = "@";
  186. directives.syntax = "builtin";
  187. registers.r0 = "variable";
  188. registers.r1 = "variable";
  189. registers.r2 = "variable";
  190. registers.r3 = "variable";
  191. registers.r4 = "variable";
  192. registers.r5 = "variable";
  193. registers.r6 = "variable";
  194. registers.r7 = "variable";
  195. registers.r8 = "variable";
  196. registers.r9 = "variable";
  197. registers.r10 = "variable";
  198. registers.r11 = "variable";
  199. registers.r12 = "variable";
  200. registers.sp = "variable-2";
  201. registers.lr = "variable-2";
  202. registers.pc = "variable-2";
  203. registers.r13 = registers.sp;
  204. registers.r14 = registers.lr;
  205. registers.r15 = registers.pc;
  206. custom.push(function(ch, stream) {
  207. if (ch === '#') {
  208. stream.eatWhile(/\w/);
  209. return "number";
  210. }
  211. });
  212. }
  213. var arch = (parserConfig.architecture || "x86").toLowerCase();
  214. if (arch === "x86") {
  215. x86(parserConfig);
  216. } else if (arch === "arm" || arch === "armv6") {
  217. armv6(parserConfig);
  218. }
  219. function nextUntilUnescaped(stream, end) {
  220. var escaped = false, next;
  221. while ((next = stream.next()) != null) {
  222. if (next === end && !escaped) {
  223. return false;
  224. }
  225. escaped = !escaped && next === "\\";
  226. }
  227. return escaped;
  228. }
  229. function clikeComment(stream, state) {
  230. var maybeEnd = false, ch;
  231. while ((ch = stream.next()) != null) {
  232. if (ch === "/" && maybeEnd) {
  233. state.tokenize = null;
  234. break;
  235. }
  236. maybeEnd = (ch === "*");
  237. }
  238. return "comment";
  239. }
  240. return {
  241. startState: function() {
  242. return {
  243. tokenize: null
  244. };
  245. },
  246. token: function(stream, state) {
  247. if (state.tokenize) {
  248. return state.tokenize(stream, state);
  249. }
  250. if (stream.eatSpace()) {
  251. return null;
  252. }
  253. var style, cur, ch = stream.next();
  254. if (ch === "/") {
  255. if (stream.eat("*")) {
  256. state.tokenize = clikeComment;
  257. return clikeComment(stream, state);
  258. }
  259. }
  260. if (ch === lineCommentStartSymbol) {
  261. stream.skipToEnd();
  262. return "comment";
  263. }
  264. if (ch === '"') {
  265. nextUntilUnescaped(stream, '"');
  266. return "string";
  267. }
  268. if (ch === '.') {
  269. stream.eatWhile(/\w/);
  270. cur = stream.current().toLowerCase();
  271. style = directives[cur];
  272. return style || null;
  273. }
  274. if (ch === '=') {
  275. stream.eatWhile(/\w/);
  276. return "tag";
  277. }
  278. if (ch === '{') {
  279. return "bracket";
  280. }
  281. if (ch === '}') {
  282. return "bracket";
  283. }
  284. if (/\d/.test(ch)) {
  285. if (ch === "0" && stream.eat("x")) {
  286. stream.eatWhile(/[0-9a-fA-F]/);
  287. return "number";
  288. }
  289. stream.eatWhile(/\d/);
  290. return "number";
  291. }
  292. if (/\w/.test(ch)) {
  293. stream.eatWhile(/\w/);
  294. if (stream.eat(":")) {
  295. return 'tag';
  296. }
  297. cur = stream.current().toLowerCase();
  298. style = registers[cur];
  299. return style || null;
  300. }
  301. for (var i = 0; i < custom.length; i++) {
  302. style = custom[i](ch, stream, state);
  303. if (style) {
  304. return style;
  305. }
  306. }
  307. },
  308. lineComment: lineCommentStartSymbol,
  309. blockCommentStart: "/*",
  310. blockCommentEnd: "*/"
  311. };
  312. });
  313. });