Skip to content

Instantly share code, notes, and snippets.

@jkrempus
Created October 8, 2012 10:37
Show Gist options
  • Select an option

  • Save jkrempus/3851893 to your computer and use it in GitHub Desktop.

Select an option

Save jkrempus/3851893 to your computer and use it in GitHub Desktop.
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