From 4b9aac47b72dda4952cd443523681867e55400e3 Mon Sep 17 00:00:00 2001 From: Fanael Linithien Date: Fri, 19 Aug 2022 17:38:37 +0200 Subject: [PATCH] Improve NASM lexer Adds support for SSE/AVX/AVX-512 registers and 'rel' and 'abs' address operators. --- pygments/lexers/asm.py | 7 +- tests/examplefiles/nasm/nasm_simd.asm | 13 +++ tests/examplefiles/nasm/nasm_simd.asm.output | 102 +++++++++++++++++++ 3 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 tests/examplefiles/nasm/nasm_simd.asm create mode 100644 tests/examplefiles/nasm/nasm_simd.asm.output diff --git a/pygments/lexers/asm.py b/pygments/lexers/asm.py index 8e803c9116..6087d6ed27 100644 --- a/pygments/lexers/asm.py +++ b/pygments/lexers/asm.py @@ -730,8 +730,9 @@ class NasmLexer(RegexLexer): declkw = r'(?:res|d)[bwdqt]|times' register = (r'(r[0-9][0-5]?[bwd]?|' r'[a-d][lh]|[er]?[a-d]x|[er]?[sb]p|[er]?[sd]i|[c-gs]s|st[0-7]|' - r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7])\b') - wordop = r'seg|wrt|strict' + r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]|k[0-7]|' + r'[xyz]mm(?:[12][0-9]?|3[01]?|[04-9]))\b') + wordop = r'seg|wrt|strict|rel|abs' type = r'byte|[dq]?word' # Directives must be followed by whitespace, otherwise CPU will match # cpuid for instance. @@ -778,7 +779,7 @@ class NasmLexer(RegexLexer): (r'#.*', Comment.Single) ], 'punctuation': [ - (r'[,():\[\]]+', Punctuation), + (r'[,{}():\[\]]+', Punctuation), (r'[&|^<>+*/%~-]+', Operator), (r'[$]+', Keyword.Constant), (wordop, Operator.Word), diff --git a/tests/examplefiles/nasm/nasm_simd.asm b/tests/examplefiles/nasm/nasm_simd.asm new file mode 100644 index 0000000000..587e30e823 --- /dev/null +++ b/tests/examplefiles/nasm/nasm_simd.asm @@ -0,0 +1,13 @@ +; test source for SIMD operations +; not intended to be executable +bits 64 + +kmovq k1, [rel mask] +vpxor xmm0, xmm0, xmm0 +vmovdqa32 zmm30, [abs data] +vpaddd zmm0{k1}, zmm0, zmm30 +vpxor xmm1, xmm1, [rel xmm01] + +mask dq 0xAAAAAAAA55555555 +data: times 16 dd 0x12345678 +xmm01: times 4 dd 0 diff --git a/tests/examplefiles/nasm/nasm_simd.asm.output b/tests/examplefiles/nasm/nasm_simd.asm.output new file mode 100644 index 0000000000..289d8accfa --- /dev/null +++ b/tests/examplefiles/nasm/nasm_simd.asm.output @@ -0,0 +1,102 @@ +'; test source for SIMD operations' Comment.Single +'\n' Text.Whitespace + +'; not intended to be executable' Comment.Single +'\n' Text.Whitespace + +'bits' Keyword +' ' Text.Whitespace +'64' Literal.Number.Integer +'\n\n' Text.Whitespace + +'kmovq' Name.Function +' ' Text.Whitespace +'k1' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'[' Punctuation +'rel' Operator.Word +' ' Text.Whitespace +'mask' Name.Variable +']' Punctuation +'\n' Text.Whitespace + +'vpxor' Name.Function +' ' Text.Whitespace +'xmm0' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'xmm0' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'xmm0' Name.Builtin +'\n' Text.Whitespace + +'vmovdqa32' Name.Function +' ' Text.Whitespace +'zmm30' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'[' Punctuation +'abs' Operator.Word +' ' Text.Whitespace +'data' Name.Variable +']' Punctuation +'\n' Text.Whitespace + +'vpaddd' Name.Function +' ' Text.Whitespace +'zmm0' Name.Builtin +'{' Punctuation +'k1' Name.Builtin +'},' Punctuation +' ' Text.Whitespace +'zmm0' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'zmm30' Name.Builtin +'\n' Text.Whitespace + +'vpxor' Name.Function +' ' Text.Whitespace +'xmm1' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'xmm1' Name.Builtin +',' Punctuation +' ' Text.Whitespace +'[' Punctuation +'rel' Operator.Word +' ' Text.Whitespace +'xmm01' Name.Variable +']' Punctuation +'\n\n' Text.Whitespace + +'mask' Name.Function +' ' Text.Whitespace +'dq' Name.Variable +' ' Text.Whitespace +'0xAAAAAAAA55555555' Literal.Number.Hex +'\n' Text.Whitespace + +'data:' Name.Label +' ' Text.Whitespace +'times' Keyword.Declaration +' ' Text.Whitespace +'16' Literal.Number.Integer +' ' Text.Whitespace +'dd' Name.Variable +' ' Text.Whitespace +'0x12345678' Literal.Number.Hex +'\n' Text.Whitespace + +'xmm01:' Name.Label +' ' Text.Whitespace +'times' Keyword.Declaration +' ' Text.Whitespace +'4' Literal.Number.Integer +' ' Text.Whitespace +'dd' Name.Variable +' ' Text.Whitespace +'0' Literal.Number.Integer +'\n' Text.Whitespace