gas.js 8.6 KB

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