02 Nov Regular Expression with UVM
간혹 instance hierarchy를 기반으로 filtering 하는 등 testbench 내에서 regular expression, 즉, 정규식을 사용하면 유용한 경우가 있다. 이를 위해 UVM은 regular expression matching을 할 수 있도록 아래와 같이 uvm_re_match
라는 DPI-C function을 제공한다. 해당 function은 POSIX function인 regexec
를 call 하도록 되어 있다.
import "DPI-C" function int uvm_re_match(string re, string str);
Function 사용 방법은 간단하다. String str
이 regular expression re
와 일치하는 경우 0을 return 하고, 그렇지 않은 경우 1과 같은 error code를 return 한다. 아래 예제를 참고하자. 한 가지 주의할 점은 escaped character를 표현하기 위해 사용되는 \
는 한 번 더 escape 하여 \\
로 표현해야 한다는 점이다.
string str, re; str = "uvm_test_top.env.agent[0]"; re = "uvm_test_top\\.env\\.agent\\[[0-9]\\]"; if (uvm_re_match(re, str) == 0) begin `uvm_info("REGEX", "match", UVM_MEDIUM) end else begin `uvm_error("REGEX", "no match") end
참고로 이러한 regular expression matching은 UVM에서 DPI를 사용하는 경우에만 유효하다. 즉, UVM_NO_DPI
또는 UVM_REGEX_NO_DPI
와 같은 text macro가 define 되는 경우 uvm_re_match
는 regular expression matching이 아닌 glob style matching을 수행한다. Glob style matching은 *
, ?
등의 wildcard character를 이용한 단순한 matching만을 지원한다.
`ifdef UVM_NO_DPI // ... `define UVM_REGEX_NO_DPI // ... `endif
`ifndef UVM_REGEX_NO_DPI import "DPI-C" function int uvm_re_match(string re, string str); import "DPI-C" function void uvm_dump_re_cache(); import "DPI-C" function string uvm_glob_to_re(string glob); `else // The Verilog only version does not match regular expressions, // it only does glob style matching. function int uvm_re_match(string re, string str); // ... endfunction function void uvm_dump_re_cache(); endfunction function string uvm_glob_to_re(string glob); // ... endfunction `endif
마지막으로 regular expression에 대해 참고하거나 실시간으로 테스트 할 수 있는 RegExr 웹사이트를 추천하며 마친다.
References
Jung Ik Moon
Verification Engineer
No Comments