Theme Inspinia
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.

gas.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/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.ax = "variable";
  140. registers.eax = "variable-2";
  141. registers.rax = "variable-3";
  142. registers.bx = "variable";
  143. registers.ebx = "variable-2";
  144. registers.rbx = "variable-3";
  145. registers.cx = "variable";
  146. registers.ecx = "variable-2";
  147. registers.rcx = "variable-3";
  148. registers.dx = "variable";
  149. registers.edx = "variable-2";
  150. registers.rdx = "variable-3";
  151. registers.si = "variable";
  152. registers.esi = "variable-2";
  153. registers.rsi = "variable-3";
  154. registers.di = "variable";
  155. registers.edi = "variable-2";
  156. registers.rdi = "variable-3";
  157. registers.sp = "variable";
  158. registers.esp = "variable-2";
  159. registers.rsp = "variable-3";
  160. registers.bp = "variable";
  161. registers.ebp = "variable-2";
  162. registers.rbp = "variable-3";
  163. registers.ip = "variable";
  164. registers.eip = "variable-2";
  165. registers.rip = "variable-3";
  166. registers.cs = "keyword";
  167. registers.ds = "keyword";
  168. registers.ss = "keyword";
  169. registers.es = "keyword";
  170. registers.fs = "keyword";
  171. registers.gs = "keyword";
  172. }
  173. function armv6(_parserConfig) {
  174. // Reference:
  175. // http://infocenter.arm.com/help/topic/com.arm.doc.qrc0001l/QRC0001_UAL.pdf
  176. // http://infocenter.arm.com/help/topic/com.arm.doc.ddi0301h/DDI0301H_arm1176jzfs_r0p7_trm.pdf
  177. lineCommentStartSymbol = "@";
  178. directives.syntax = "builtin";
  179. registers.r0 = "variable";
  180. registers.r1 = "variable";
  181. registers.r2 = "variable";
  182. registers.r3 = "variable";
  183. registers.r4 = "variable";
  184. registers.r5 = "variable";
  185. registers.r6 = "variable";
  186. registers.r7 = "variable";
  187. registers.r8 = "variable";
  188. registers.r9 = "variable";
  189. registers.r10 = "variable";
  190. registers.r11 = "variable";
  191. registers.r12 = "variable";
  192. registers.sp = "variable-2";
  193. registers.lr = "variable-2";
  194. registers.pc = "variable-2";
  195. registers.r13 = registers.sp;
  196. registers.r14 = registers.lr;
  197. registers.r15 = registers.pc;
  198. custom.push(function(ch, stream) {
  199. if (ch === '#') {
  200. stream.eatWhile(/\w/);
  201. return "number";
  202. }
  203. });
  204. }
  205. var arch = (parserConfig.architecture || "x86").toLowerCase();
  206. if (arch === "x86") {
  207. x86(parserConfig);
  208. } else if (arch === "arm" || arch === "armv6") {
  209. armv6(parserConfig);
  210. }
  211. function nextUntilUnescaped(stream, end) {
  212. var escaped = false, next;
  213. while ((next = stream.next()) != null) {
  214. if (next === end && !escaped) {
  215. return false;
  216. }
  217. escaped = !escaped && next === "\\";
  218. }
  219. return escaped;
  220. }
  221. function clikeComment(stream, state) {
  222. var maybeEnd = false, ch;
  223. while ((ch = stream.next()) != null) {
  224. if (ch === "/" && maybeEnd) {
  225. state.tokenize = null;
  226. break;
  227. }
  228. maybeEnd = (ch === "*");
  229. }
  230. return "comment";
  231. }
  232. return {
  233. startState: function() {
  234. return {
  235. tokenize: null
  236. };
  237. },
  238. token: function(stream, state) {
  239. if (state.tokenize) {
  240. return state.tokenize(stream, state);
  241. }
  242. if (stream.eatSpace()) {
  243. return null;
  244. }
  245. var style, cur, ch = stream.next();
  246. if (ch === "/") {
  247. if (stream.eat("*")) {
  248. state.tokenize = clikeComment;
  249. return clikeComment(stream, state);
  250. }
  251. }
  252. if (ch === lineCommentStartSymbol) {
  253. stream.skipToEnd();
  254. return "comment";
  255. }
  256. if (ch === '"') {
  257. nextUntilUnescaped(stream, '"');
  258. return "string";
  259. }
  260. if (ch === '.') {
  261. stream.eatWhile(/\w/);
  262. cur = stream.current().toLowerCase();
  263. style = directives[cur];
  264. return style || null;
  265. }
  266. if (ch === '=') {
  267. stream.eatWhile(/\w/);
  268. return "tag";
  269. }
  270. if (ch === '{') {
  271. return "braket";
  272. }
  273. if (ch === '}') {
  274. return "braket";
  275. }
  276. if (/\d/.test(ch)) {
  277. if (ch === "0" && stream.eat("x")) {
  278. stream.eatWhile(/[0-9a-fA-F]/);
  279. return "number";
  280. }
  281. stream.eatWhile(/\d/);
  282. return "number";
  283. }
  284. if (/\w/.test(ch)) {
  285. stream.eatWhile(/\w/);
  286. if (stream.eat(":")) {
  287. return 'tag';
  288. }
  289. cur = stream.current().toLowerCase();
  290. style = registers[cur];
  291. return style || null;
  292. }
  293. for (var i = 0; i < custom.length; i++) {
  294. style = custom[i](ch, stream, state);
  295. if (style) {
  296. return style;
  297. }
  298. }
  299. },
  300. lineComment: lineCommentStartSymbol,
  301. blockCommentStart: "/*",
  302. blockCommentEnd: "*/"
  303. };
  304. });
  305. });