add: builtins functions

This commit is contained in:
Vallahor 2022-06-01 01:24:29 -03:00 committed by Andrew Kelley
parent a0ec87be71
commit b9fa91002e
2 changed files with 473 additions and 85 deletions

View File

@ -1085,6 +1085,9 @@ var zigAnalysis;
let literal = expr.enumLiteral;
return literal;
}
case "void": {
return "VOID WIP";
}
case "switchOp":{
let payloadHtml = "switch() {</br>";
for (let i = 0; i < expr.switchOp.cases.length; i++) {
@ -1115,13 +1118,12 @@ var zigAnalysis;
// const fieldRef = zigAnalysis.decls[expr.fieldRef.index];
// const struct_name = zigAnalysis.decls[expr.struct[0].val.typeRef.refPath[0].declRef].name;
console.log(expr)
console.log(fieldRef)
// console.log(fieldRef)
// return "@enumToInt(" + exprName(enumToInt, opts) + ")";
// return exprName(fieldRef,opts);
return "WIP"
}
case "enumToInt" : {
console.log(expr);
const enumToInt = zigAnalysis.exprs[expr.enumToInt];
return "@enumToInt(" + exprName(enumToInt, opts) + ")";
}
@ -1133,6 +1135,187 @@ var zigAnalysis;
const sizeOf = zigAnalysis.exprs[expr.sizeOf];
return "@sizeOf(" + exprName(sizeOf, opts) + ")";
}
case "builtinIndex" : {
const builtinIndex = zigAnalysis.exprs[expr.builtinIndex];
return exprName(builtinIndex, opts);
}
case "builtin": {
const param_expr = zigAnalysis.exprs[expr.builtin.param];
let param = exprName(param_expr, opts);
let payloadHtml = "@";
switch (expr.builtin.name) {
case "align_of": {
payloadHtml += "alignOf";
break;
}
case "bool_to_int": {
payloadHtml += "boolToInt";
break;
}
case "embed_file": {
payloadHtml += "embedFile";
break;
}
case "error_name": {
payloadHtml += "errorName";
break;
}
case "panic": {
payloadHtml += "panic";
break;
}
case "set_cold": {
payloadHtml += "setCold";
break;
}
case "set_runtime_safety": {
payloadHtml += "setRuntimeSafety";
break;
}
case "sqrt": {
payloadHtml += "sqrt";
break;
}
case "sin": {
payloadHtml += "sin";
break;
}
case "cos": {
payloadHtml += "cos";
break;
}
case "tan": {
payloadHtml += "tan";
break;
}
case "exp": {
payloadHtml += "exp";
break;
}
case "exp2": {
payloadHtml += "exp2";
break;
}
case "log": {
payloadHtml += "log";
break;
}
case "log2": {
payloadHtml += "log2";
break;
}
case "log10": {
payloadHtml += "log10";
break;
}
case "fabs": {
payloadHtml += "fabs";
break;
}
case "floor": {
payloadHtml += "floor";
break;
}
case "ceil": {
payloadHtml += "ceil";
break;
}
case "trunc": {
payloadHtml += "trunc";
break;
}
case "round": {
payloadHtml += "round";
break;
}
case "tag_name": {
payloadHtml += "tagName";
break;
}
case "reify": {
payloadHtml += "Type";
break;
}
case "type_name": {
payloadHtml += "typeName";
break;
}
case "frame_type": {
payloadHtml += "Frame";
break;
}
case "frame_size": {
payloadHtml += "frameSize";
break;
}
default: console.log("builtin function not handled yet or doesn't exist!");
};
return payloadHtml + "(" + param + ")";
}
case "builtinBinIndex" : {
const builtinBinIndex = zigAnalysis.exprs[expr.builtinBinIndex];
return exprName(builtinBinIndex, opts);
}
case "builtinBin": {
const lhsOp = zigAnalysis.exprs[expr.builtinBin.lhs];
const rhsOp = zigAnalysis.exprs[expr.builtinBin.rhs];
let lhs = exprName(lhsOp, opts);
let rhs = exprName(rhsOp, opts);
let payloadHtml = "@";
switch (expr.builtinBin.name) {
case "float_to_int": {
payloadHtml += "floatToInt";
break;
}
case "int_to_float": {
payloadHtml += "intToFloat";
break;
}
case "int_to_ptr": {
payloadHtml += "intToPtr";
break;
}
case "int_to_enum": {
payloadHtml += "intToEnum";
break;
}
case "float_cast": {
payloadHtml += "floatCast";
break;
}
case "int_cast": {
payloadHtml += "intCast";
break;
}
case "ptr_cast": {
payloadHtml += "ptrCast";
break;
}
case "truncate": {
payloadHtml += "truncate";
break;
}
case "align_cast": {
payloadHtml += "alignCast";
break;
}
case "has_decl": {
payloadHtml += "hasDecl";
break;
}
case "has_field": {
payloadHtml += "hasField";
break;
}
default: console.log("builtin function not handled yet or doesn't exist!");
};
return payloadHtml + "(" + lhs + ", " + rhs + ")";
}
case "binOpIndex" : {
const binOpIndex = zigAnalysis.exprs[expr.binOpIndex];
return exprName(binOpIndex, opts);
@ -1146,9 +1329,6 @@ var zigAnalysis;
let print_lhs = "";
let print_rhs = "";
console.log(lhsOp)
console.log(rhsOp)
if (lhsOp['binOpIndex']) {
print_lhs = "(" + lhs + ")";
} else {
@ -1229,6 +1409,21 @@ var zigAnalysis;
case 11 : {
return "@alignCast(" + print_lhs + ", " + print_rhs + ")";
}
case 12 : {
operator += "&";
break;
}
case 13 : {
operator += "++";
break;
}
case 14 : {
operator += "**";
break;
}
case 15 : {
return "@Vector(" + print_lhs + ", " + print_rhs + ")";
}
default: console.log("operator not handled yet or doesn't exist!");
};
if (expr.binOp.wrap) {
@ -1723,7 +1918,11 @@ var zigAnalysis;
linkFnNameDecl: opts.linkFnNameDecl,
});
payloadHtml += '<span class="tok-kw">' + escapeHtml(name) + '</span>';
} else if ("comptimeExpr" in value) {
} else if ("binOpIndex" in value) {
payloadHtml += exprName(value, opts);
console.log(value);
console.log(payloadHtml);
}else if ("comptimeExpr" in value) {
let comptimeExpr = zigAnalysis.comptimeExprs[value.comptimeExpr].code;
if (opts.wantHtml) {
payloadHtml += '<span class="tok-kw">' + comptimeExpr + '</span>';

View File

@ -668,6 +668,10 @@ const DocData = struct {
compileError: []const u8,
errorSets: usize,
string: []const u8, // direct value
builtin: Builtin,
builtinIndex: usize,
builtinBin: BuiltinBin,
builtinBinIndex: usize,
switchIndex: usize, // index in `exprs`
switchOp: SwitchOp,
// Index a `type` like struct with expressions
@ -682,7 +686,7 @@ const DocData = struct {
rhs: usize, // index in `exprs`
// opKind
// Identify the operator in js
// 0: add, 1: sub, 2: mul, 3: div, 4: mod, 5: rem, 6: rem_mod, 7: shl, 8: shr, 9: bitcast, 10: bit_or, 11: align_cast
// 0: add, 1: sub, 2: mul, 3: div, 4: mod, 5: rem, 6: rem_mod, 7: shl, 8: shr, 9: bitcast, 10: bit_or, 11: align_cast, 12: bit_and, 13: array_cat, 14: array_mul, 15: vector
// Others binOp are not handled yet
opKind: usize = 0,
// flags to operations
@ -696,6 +700,15 @@ const DocData = struct {
cases: []usize,
else_index: ?usize,
};
const BuiltinBin = struct {
name: []const u8 = "", // fn name
lhs: usize, // index in `exprs`
rhs: usize, // index in `exprs`
};
const Builtin = struct {
name: []const u8 = "", // fn name
param: usize, // index in `exprs`
};
const As = struct {
typeRefArg: ?usize, // index in `exprs`
exprArg: usize, // index in `exprs`
@ -800,6 +813,26 @@ const DocData = struct {
options,
w,
),
.builtin => |v| try std.json.stringify(
struct { builtin: Builtin }{ .builtin = v },
options,
w,
),
.builtinIndex => |v| try std.json.stringify(
struct { builtinIndex: usize }{ .builtinIndex = v },
options,
w,
),
.builtinBin => |v| try std.json.stringify(
struct { builtinBin: BuiltinBin }{ .builtinBin = v },
options,
w,
),
.builtinBinIndex => |v| try std.json.stringify(
struct { builtinBinIndex: usize }{ .builtinBinIndex = v },
options,
w,
),
.typeOf_peer => |v| try std.json.stringify(
struct { typeOf_peer: []usize }{ .typeOf_peer = v },
options,
@ -1041,37 +1074,38 @@ fn walkInstruction(
.expr = .{ .binOpIndex = binop_index },
};
},
.align_cast => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
const binop_index = self.exprs.items.len;
try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
var lhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.lhs,
false,
);
var rhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.rhs,
false,
);
const lhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, lhs.expr);
const rhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, rhs.expr);
self.exprs.items[binop_index] = .{ .binOp = .{ .lhs = lhs_index, .rhs = rhs_index, .opKind = 11 } };
return DocData.WalkResult{
.typeRef = .{ .type = @enumToInt(Ref.type_type) },
.expr = .{ .binOpIndex = binop_index },
};
},
// implementing in builtinBin @check if it's ok to delete it
// .align_cast => {
// const pl_node = data[inst_index].pl_node;
// const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
//
// const binop_index = self.exprs.items.len;
// try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
//
// var lhs: DocData.WalkResult = try self.walkRef(
// file,
// parent_scope,
// extra.data.lhs,
// false,
// );
// var rhs: DocData.WalkResult = try self.walkRef(
// file,
// parent_scope,
// extra.data.rhs,
// false,
// );
//
// const lhs_index = self.exprs.items.len;
// try self.exprs.append(self.arena, lhs.expr);
// const rhs_index = self.exprs.items.len;
// try self.exprs.append(self.arena, rhs.expr);
// self.exprs.items[binop_index] = .{ .binOp = .{ .lhs = lhs_index, .rhs = rhs_index, .opKind = 11 } };
//
// return DocData.WalkResult{
// .typeRef = .{ .type = @enumToInt(Ref.type_type) },
// .expr = .{ .binOpIndex = binop_index },
// };
// },
.bit_or => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
@ -1103,6 +1137,37 @@ fn walkInstruction(
.expr = .{ .binOpIndex = binop_index },
};
},
.bit_and => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
const binop_index = self.exprs.items.len;
try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
var lhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.lhs,
false,
);
var rhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.rhs,
false,
);
const lhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, lhs.expr);
const rhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, rhs.expr);
self.exprs.items[binop_index] = .{ .binOp = .{ .lhs = lhs_index, .rhs = rhs_index, .opKind = 12 } };
return DocData.WalkResult{
.typeRef = .{ .type = @enumToInt(Ref.type_type) },
.expr = .{ .binOpIndex = binop_index },
};
},
.add => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
@ -1763,6 +1828,175 @@ fn walkInstruction(
};
},
// throwing this TODO
// TODO: handle `Array` in tryResolveDeclPath.type
// .array_cat => {
// const pl_node = data[inst_index].pl_node;
// const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
//
// const binop_index = self.exprs.items.len;
// try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
//
// var lhs: DocData.WalkResult = try self.walkRef(
// file,
// parent_scope,
// extra.data.lhs,
// false,
// );
// var rhs: DocData.WalkResult = try self.walkRef(
// file,
// parent_scope,
// extra.data.rhs,
// false,
// );
//
// const lhs_index = self.exprs.items.len;
// try self.exprs.append(self.arena, lhs.expr);
// const rhs_index = self.exprs.items.len;
// try self.exprs.append(self.arena, rhs.expr);
// self.exprs.items[binop_index] = .{ .binOp = .{ .lhs = lhs_index, .rhs = rhs_index, .exact = true, .opKind = 13 } };
//
// return DocData.WalkResult{
// .typeRef = .{ .type = @enumToInt(Ref.type_type) },
// .expr = .{ .binOpIndex = binop_index },
// };
// },
// .array_mul => {
// const pl_node = data[inst_index].pl_node;
// const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
//
// const binop_index = self.exprs.items.len;
// try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
//
// var lhs: DocData.WalkResult = try self.walkRef(
// file,
// parent_scope,
// extra.data.lhs,
// false,
// );
// var rhs: DocData.WalkResult = try self.walkRef(
// file,
// parent_scope,
// extra.data.rhs,
// false,
// );
//
// const lhs_index = self.exprs.items.len;
// try self.exprs.append(self.arena, lhs.expr);
// const rhs_index = self.exprs.items.len;
// try self.exprs.append(self.arena, rhs.expr);
// self.exprs.items[binop_index] = .{ .binOp = .{ .lhs = lhs_index, .rhs = rhs_index, .exact = true, .opKind = 14 } };
//
// return DocData.WalkResult{
// .typeRef = .{ .type = @enumToInt(Ref.type_type) },
// .expr = .{ .binOpIndex = binop_index },
// };
// },
.vector_type => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
const binop_index = self.exprs.items.len;
try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
var lhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.lhs,
false,
);
var rhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.rhs,
false,
);
const lhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, lhs.expr);
const rhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, rhs.expr);
self.exprs.items[binop_index] = .{ .binOp = .{ .lhs = lhs_index, .rhs = rhs_index, .exact = true, .opKind = 15 } };
return DocData.WalkResult{
.typeRef = .{ .type = @enumToInt(Ref.type_type) },
.expr = .{ .binOpIndex = binop_index },
};
},
.align_of,
.bool_to_int,
.embed_file,
.error_name,
.panic,
.set_cold, // @check
.set_runtime_safety, // @check
.sqrt,
.sin,
.cos,
.tan,
.exp,
.exp2,
.log,
.log2,
.log10,
.fabs,
.floor,
.ceil,
.trunc,
.round,
.tag_name,
.reify,
.type_name,
.frame_type,
.frame_size,
=> {
const un_node = data[inst_index].un_node;
const bin_index = self.exprs.items.len;
try self.exprs.append(self.arena, .{ .builtin = .{ .param = 0 } });
const param = try self.walkRef(file, parent_scope, un_node.operand, false);
const param_index = self.exprs.items.len;
try self.exprs.append(self.arena, param.expr);
self.exprs.items[bin_index] = .{ .builtin = .{ .name = @tagName(tags[inst_index]), .param = param_index } };
return DocData.WalkResult{
.typeRef = param.typeRef,
.expr = .{ .builtinIndex = bin_index },
};
},
.float_to_int, .int_to_float, .int_to_ptr, .int_to_enum, .float_cast, .int_cast, .ptr_cast, .truncate, .align_cast, .has_decl, .has_field => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
const binop_index = self.exprs.items.len;
try self.exprs.append(self.arena, .{ .builtinBin = .{ .lhs = 0, .rhs = 0 } });
var lhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.lhs,
false,
);
var rhs: DocData.WalkResult = try self.walkRef(
file,
parent_scope,
extra.data.rhs,
false,
);
const lhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, lhs.expr);
const rhs_index = self.exprs.items.len;
try self.exprs.append(self.arena, rhs.expr);
self.exprs.items[binop_index] = .{ .builtinBin = .{ .name = @tagName(tags[inst_index]), .lhs = lhs_index, .rhs = rhs_index } };
return DocData.WalkResult{
.typeRef = .{ .type = @enumToInt(Ref.type_type) },
.expr = .{ .builtinBinIndex = binop_index },
};
},
.error_union_type => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Bin, pl_node.payload_index);
@ -2398,26 +2632,6 @@ fn walkInstruction(
.expr = .{ .typeOf = operand_index },
};
},
.align_of => {
// WIP
// not fully working
// @alignOf() with some pointer type it's not even called in js
// @alignOf([*]u8) are being rendered as [*]u8
const un_node = data[inst_index].un_node;
const operand = try self.walkRef(
file,
parent_scope,
un_node.operand,
need_type,
);
const operand_index = self.exprs.items.len;
try self.exprs.append(self.arena, operand.expr);
return DocData.WalkResult{
.typeRef = operand.typeRef,
.expr = .{ .alignOf = operand_index },
};
},
.typeof_builtin => {
const pl_node = data[inst_index].pl_node;
const extra = file.zir.extraData(Zir.Inst.Block, pl_node.payload_index);
@ -2438,31 +2652,6 @@ fn walkInstruction(
.expr = .{ .typeOf = operand_index },
};
},
.typeof_log2_int_type => {
// @check
const un_node = data[inst_index].un_node;
const operand = try self.walkRef(
file,
parent_scope,
un_node.operand,
need_type,
);
// WIP
// const sep = "=" ** 200;
// std.debug.print("{s}\n", .{sep});
// std.debug.print("un_node = {any}\n", .{un_node});
// std.debug.print("operand = {any}\n", .{operand});
// std.debug.print("{s}\n", .{sep});
const operand_index = self.exprs.items.len;
try self.exprs.append(self.arena, operand.expr);
return DocData.WalkResult{
.typeRef = operand.typeRef,
.expr = .{ .typeOf = operand_index },
};
},
.type_info => {
// @check
const un_node = data[inst_index].un_node;
@ -3681,8 +3870,8 @@ fn tryResolveRefPath(
panicWithContext(
file,
inst_index,
"TODO: handle `{s}` in tryResolveDeclPath.type\n",
.{@tagName(self.types.items[t_index])},
"TODO: handle `{s}` in tryResolveDeclPath.type\nInfo: {}",
.{ @tagName(self.types.items[t_index]), resolved_parent },
);
},
.Unanalyzed => {