Created
October 8, 2012 10:37
-
-
Save jkrempus/3851893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio, std.regex, std.range, std.algorithm; | |
| auto translator() | |
| { | |
| immutable scalar = [ | |
| "f32" : "float", | |
| "f64" : "double", | |
| "i8" : "byte", | |
| "i16" : "short", | |
| "i32" : "int", | |
| "i64" : "long"]; | |
| auto rvec = regex(`llvm_v(\d+)([fi]\d+)_ty`); | |
| auto rscal = regex(`llvm_([fi]\d+)_ty`); | |
| return (string type) | |
| { | |
| if(type == "") | |
| return "void"; | |
| if(type == "llvm_ptr_ty") | |
| return "void*"; | |
| auto m = match(type, rvec); | |
| if(!m.empty) | |
| return scalar[m.front[2]] ~ m.front[1]; | |
| m = match(type, rscal); | |
| if(!m.empty) | |
| return scalar[m.front[1]]; | |
| return null; | |
| }; | |
| } | |
| void printDecl(R)(string intrinsic, string builtin, string ret, R params) | |
| { | |
| auto format = | |
| `pragma (intrinsic, "%s") | |
| %s %s(%s); | |
| `; | |
| writefln(format, intrinsic, ret, builtin, params.joiner(", ")); | |
| } | |
| void main() | |
| { | |
| auto str = stdin.byLine.join; | |
| auto translate = translator(); | |
| auto braces = regex(`\[([^\]]*)\]`, "g"); | |
| auto words = regex(`(\w+)`, "g"); | |
| auto def = regex( | |
| `def int_(x86_[^\s:]*)\s*:\s*GCCBuiltin<"([^"]*)">,\s*` | |
| `Intrinsic<([^>]*)>`, "g"); | |
| foreach(c; match(str, def)) | |
| (){ | |
| auto r = match(c[3].idup, braces); | |
| auto ret = translate(match(r.front[1], words).front[1]); | |
| if(ret == null) | |
| return; | |
| r.popFront(); | |
| string[] params = []; | |
| foreach(w; match(r.front[1], words)) | |
| { | |
| auto t = translate(w[1]); | |
| if(t == null) | |
| return; | |
| params ~= t; | |
| } | |
| printDecl("llvm." ~ replace(c[1].idup, "_", "."), | |
| c[2].idup, ret, params); | |
| }(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment