This commit is contained in:
edg-l 2024-02-17 20:32:44 +00:00
parent d45574efff
commit 430305e38c
32 changed files with 155 additions and 121 deletions

View file

@ -1,2 +1,2 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `codegen` mod in crate `edlang_codegen_llvm`."><title>edlang_codegen_llvm::codegen - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="edlang_codegen_llvm" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../edlang_codegen_llvm/index.html">edlang_codegen_llvm</a><span class="version">0.0.1-alpha.7</span></h2></div><h2 class="location"><a href="#">Module codegen</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#functions">Functions</a></li></ul></section><h2><a href="../index.html">In crate edlang_codegen_llvm</a></h2></div></nav><div class="sidebar-resizer"></div>
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../../edlang_codegen_llvm/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../index.html">edlang_codegen_llvm</a>::<wbr><a class="mod" href="#">codegen</a><button id="copy-path" title="Copy item path to clipboard"><img src="../../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../../src/edlang_codegen_llvm/codegen.rs.html#1-1331">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><h2 id="functions" class="section-header"><a href="#functions">Functions</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.compile.html" title="fn edlang_codegen_llvm::codegen::compile">compile</a></div></li></ul></section></div></main></body></html>
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../../edlang_codegen_llvm/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../index.html">edlang_codegen_llvm</a>::<wbr><a class="mod" href="#">codegen</a><button id="copy-path" title="Copy item path to clipboard"><img src="../../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../../src/edlang_codegen_llvm/codegen.rs.html#1-1347">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><h2 id="functions" class="section-header"><a href="#functions">Functions</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.compile.html" title="fn edlang_codegen_llvm::codegen::compile">compile</a></div></li></ul></section></div></main></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1330,6 +1330,22 @@
<a href="#1329" id="1329">1329</a>
<a href="#1330" id="1330">1330</a>
<a href="#1331" id="1331">1331</a>
<a href="#1332" id="1332">1332</a>
<a href="#1333" id="1333">1333</a>
<a href="#1334" id="1334">1334</a>
<a href="#1335" id="1335">1335</a>
<a href="#1336" id="1336">1336</a>
<a href="#1337" id="1337">1337</a>
<a href="#1338" id="1338">1338</a>
<a href="#1339" id="1339">1339</a>
<a href="#1340" id="1340">1340</a>
<a href="#1341" id="1341">1341</a>
<a href="#1342" id="1342">1342</a>
<a href="#1343" id="1343">1343</a>
<a href="#1344" id="1344">1344</a>
<a href="#1345" id="1345">1345</a>
<a href="#1346" id="1346">1346</a>
<a href="#1347" id="1347">1347</a>
</pre></div><pre class="rust"><code><span class="kw">use </span>std::{collections::HashMap, error::Error, path::PathBuf};
<span class="kw">use </span>edlang_ir <span class="kw">as </span>ir;
@ -2303,46 +2319,9 @@
locals: <span class="kw-2">&amp;</span>HashMap&lt;usize, PointerValue&lt;<span class="lifetime">'ctx</span>&gt;&gt;,
op: <span class="kw-2">&amp;</span>ir::Operand,
) -&gt; <span class="prelude-ty">Result</span>&lt;(BasicValueEnum&lt;<span class="lifetime">'ctx</span>&gt;, TypeInfo), BuilderError&gt; {
<span class="comment">// todo: implement projection
</span><span class="kw">let </span>body = ctx.ctx.program.functions.get(<span class="kw-2">&amp;</span>fn_id).unwrap();
<span class="prelude-val">Ok</span>(<span class="kw">match </span>op {
ir::Operand::Copy(place) =&gt; {
<span class="kw">let </span>pointee_ty = compile_basic_type(ctx, <span class="kw-2">&amp;</span>body.locals[place.local].ty);
<span class="kw">let </span>ptr = <span class="kw-2">*</span>locals.get(<span class="kw-2">&amp;</span>place.local).unwrap();
(
ctx.builder.build_load(pointee_ty, ptr, <span class="string">""</span>)<span class="question-mark">?</span>,
body.locals[place.local].ty.clone(),
)
}
ir::Operand::Move(place) =&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>ptr = <span class="kw-2">*</span>locals.get(<span class="kw-2">&amp;</span>place.local).unwrap();
<span class="kw">let </span><span class="kw-2">mut </span>local_ty = body.locals[place.local].ty.clone();
<span class="kw">for </span>proj <span class="kw">in </span><span class="kw-2">&amp;</span>place.projection {
<span class="kw">match </span>proj {
ir::PlaceElem::Deref =&gt; {
ptr = ctx
.builder
.build_load(compile_basic_type(ctx, <span class="kw-2">&amp;</span>local_ty), ptr, <span class="string">"deref"</span>)<span class="question-mark">?
</span>.into_pointer_value();
local_ty = <span class="kw">match </span>local_ty.kind {
ir::TypeKind::Ptr(inner) =&gt; <span class="kw-2">*</span>inner,
ir::TypeKind::Ref(<span class="kw">_</span>, inner) =&gt; <span class="kw-2">*</span>inner,
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
}
}
ir::PlaceElem::Field { .. } =&gt; <span class="macro">todo!</span>(),
ir::PlaceElem::Index { .. } =&gt; <span class="macro">todo!</span>(),
}
}
<span class="kw">let </span>pointee_ty = compile_basic_type(ctx, <span class="kw-2">&amp;</span>local_ty);
(
ctx.builder.build_load(pointee_ty, ptr, <span class="string">""</span>)<span class="question-mark">?</span>,
body.locals[place.local].ty.clone(),
)
}
ir::Operand::Copy(place) =&gt; compile_load_place(ctx, fn_id, locals, place, <span class="bool-val">true</span>)<span class="question-mark">?</span>,
ir::Operand::Move(place) =&gt; compile_load_place(ctx, fn_id, locals, place, <span class="bool-val">false</span>)<span class="question-mark">?</span>,
ir::Operand::Constant(data) =&gt; <span class="kw">match </span><span class="kw-2">&amp;</span>data.kind {
ir::ConstKind::Value(value) =&gt; (
compile_value(ctx, value, <span class="kw-2">&amp;</span>data.type_info)<span class="question-mark">?</span>,
@ -2353,6 +2332,59 @@
})
}
<span class="kw">fn </span>compile_load_place&lt;<span class="lifetime">'ctx</span>&gt;(
ctx: <span class="kw-2">&amp;</span>ModuleCompileCtx&lt;<span class="lifetime">'ctx</span>, <span class="lifetime">'_</span>&gt;,
fn_id: DefId,
locals: <span class="kw-2">&amp;</span>HashMap&lt;usize, PointerValue&lt;<span class="lifetime">'ctx</span>&gt;&gt;,
place: <span class="kw-2">&amp;</span>ir::Place,
_is_copy: bool,
) -&gt; <span class="prelude-ty">Result</span>&lt;(BasicValueEnum&lt;<span class="lifetime">'ctx</span>&gt;, TypeInfo), BuilderError&gt; {
<span class="kw">let </span>body = ctx.ctx.program.functions.get(<span class="kw-2">&amp;</span>fn_id).unwrap();
<span class="kw">let </span><span class="kw-2">mut </span>ptr = <span class="kw-2">*</span>locals.get(<span class="kw-2">&amp;</span>place.local).unwrap();
<span class="kw">let </span><span class="kw-2">mut </span>local_ty = body.locals[place.local].ty.clone();
<span class="kw">for </span>proj <span class="kw">in </span><span class="kw-2">&amp;</span>place.projection {
<span class="kw">match </span>proj {
ir::PlaceElem::Deref =&gt; {
ptr = ctx
.builder
.build_load(compile_basic_type(ctx, <span class="kw-2">&amp;</span>local_ty), ptr, <span class="string">"deref"</span>)<span class="question-mark">?
</span>.into_pointer_value();
local_ty = <span class="kw">match </span>local_ty.kind {
ir::TypeKind::Ptr(inner) =&gt; <span class="kw-2">*</span>inner,
ir::TypeKind::Ref(<span class="kw">_</span>, inner) =&gt; <span class="kw-2">*</span>inner,
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
}
}
ir::PlaceElem::Field { field_idx } =&gt; {
local_ty = <span class="kw">match </span>local_ty.kind {
ir::TypeKind::Struct(id) =&gt; {
<span class="kw">let </span>struct_body = ctx.ctx.program.structs.get(<span class="kw-2">&amp;</span>id).unwrap();
<span class="kw">let </span>ty = struct_body.variants[<span class="kw-2">*</span>field_idx].ty.clone();
<span class="kw">let </span>field_name = struct_body.variants[<span class="kw-2">*</span>field_idx].name.clone();
ptr = ctx.builder.build_struct_gep(
compile_basic_type(ctx, <span class="kw-2">&amp;</span>local_ty),
ptr,
(<span class="kw-2">*</span>field_idx).try_into().unwrap(),
<span class="kw-2">&amp;</span><span class="macro">format!</span>(<span class="string">"ptr_field_{field_name}"</span>),
)<span class="question-mark">?</span>;
ty
}
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
}
}
ir::PlaceElem::Index { .. } =&gt; <span class="macro">todo!</span>(),
}
}
<span class="kw">let </span>pointee_ty = compile_basic_type(ctx, <span class="kw-2">&amp;</span>local_ty);
<span class="prelude-val">Ok</span>((
ctx.builder.build_load(pointee_ty, ptr, <span class="string">""</span>)<span class="question-mark">?</span>,
body.locals[place.local].ty.clone(),
))
}
<span class="kw">fn </span>compile_value&lt;<span class="lifetime">'ctx</span>&gt;(
ctx: <span class="kw-2">&amp;</span>ModuleCompileCtx&lt;<span class="lifetime">'ctx</span>, <span class="lifetime">'_</span>&gt;,
val: <span class="kw-2">&amp;</span>ValueTree,

View file

@ -473,9 +473,10 @@
<a href="#472" id="472">472</a>
<a href="#473" id="473">473</a>
<a href="#474" id="474">474</a>
<a href="#475" id="475">475</a>
</pre></div><pre class="rust"><code><span class="comment">// Based on a cfg
</span><span class="kw">use </span>std::collections::{BTreeMap, HashSet};
</span><span class="kw">use </span>std::collections::{BTreeMap, HashMap, HashSet};
<span class="kw">use </span>edlang_span::Span;
<span class="kw">use </span>smallvec::SmallVec;
@ -569,6 +570,7 @@
<span class="kw">pub </span>is_pub: bool,
<span class="kw">pub </span>name: String,
<span class="kw">pub </span>variants: Vec&lt;AdtVariant&gt;,
<span class="kw">pub </span>name_to_idx: HashMap&lt;String, usize&gt;,
<span class="kw">pub </span>span: Span,
}
@ -834,7 +836,7 @@
<span class="attr">#[derive(Debug, Clone, Copy)]
</span><span class="kw">pub enum </span>PlaceElem {
Deref,
Field { field_idx: usize, type_info: usize },
Field { field_idx: usize },
Index { local: usize },
}

View file

@ -1139,6 +1139,7 @@
is_pub: <span class="bool-val">true</span>, <span class="comment">// todo struct pub
</span>name: info.name.name.clone(),
variants: Vec::new(),
name_to_idx: Default::default(),
span: info.span,
};
@ -1149,6 +1150,8 @@
ty: lower_type(<span class="kw-2">&amp;</span>ctx, <span class="kw-2">&amp;</span>field.r#type, module_id),
};
body.variants.push(variant);
body.name_to_idx
.insert(field.name.name.clone(), body.variants.len() - <span class="number">1</span>);
}
ctx.body.structs.insert(body.def_id, body);
@ -1454,21 +1457,6 @@
<span class="kw">fn </span>lower_assign(builder: <span class="kw-2">&amp;mut </span>BodyBuilder, info: <span class="kw-2">&amp;</span>ast::AssignStmt) {
<span class="kw">let </span>(<span class="kw-2">mut </span>place, <span class="kw-2">mut </span>ty) = lower_path(builder, <span class="kw-2">&amp;</span>info.name);
<span class="kw">if let </span><span class="prelude-val">Some</span>(PlaceElem::Deref) = place.projection.last() {
<span class="kw">match </span><span class="kw-2">&amp;</span>ty {
TypeKind::Ptr(inner) =&gt; {
ty = inner.kind.clone();
}
TypeKind::Ref(is_mut, inner) =&gt; {
<span class="kw">if </span>!is_mut {
<span class="macro">panic!</span>(<span class="string">"trying to mutate non mut ref"</span>);
}
ty = inner.kind.clone();
}
<span class="kw">_ </span>=&gt; <span class="macro">unreachable!</span>(),
}
}
<span class="kw">for _ in </span><span class="number">0</span>..info.deref_times {
<span class="kw">match </span><span class="kw-2">&amp;</span>ty {
TypeKind::Ptr(inner) =&gt; {
@ -1490,7 +1478,7 @@
builder.statements.push(Statement {
span: <span class="prelude-val">Some</span>(info.name.first.span),
kind: StatementKind::Assign(place, rvalue),
})
});
}
<span class="kw">fn </span>find_expr_type(builder: <span class="kw-2">&amp;mut </span>BodyBuilder, info: <span class="kw-2">&amp;</span>ast::Expression) -&gt; <span class="prelude-ty">Option</span>&lt;TypeKind&gt; {
@ -1923,8 +1911,7 @@
},
ast::ValueExpr::Str { value: <span class="kw">_</span>, span: <span class="kw">_ </span>} =&gt; <span class="macro">todo!</span>(),
ast::ValueExpr::Path(info) =&gt; {
<span class="comment">// add deref info to path
</span><span class="kw">let </span>(place, ty) = lower_path(builder, info);
<span class="kw">let </span>(place, ty) = lower_path(builder, info);
(Operand::Move(place), ty)
}
}
@ -1958,13 +1945,26 @@
.name_to_local
.get(<span class="kw-2">&amp;</span>info.first.name)
.expect(<span class="string">"local not found"</span>);
<span class="kw">let </span>ty = builder.body.locals[local].ty.kind.clone();
<span class="kw">let </span>projection = Vec::new();
<span class="kw">let </span><span class="kw-2">mut </span>ty = builder.body.locals[local].ty.kind.clone();
<span class="kw">let </span><span class="kw-2">mut </span>projection = Vec::new();
<span class="kw">for </span>extra <span class="kw">in </span><span class="kw-2">&amp;</span>info.extra {
<span class="kw">match </span>extra {
ast::PathSegment::Field(<span class="kw">_</span>) =&gt; <span class="macro">todo!</span>(),
ast::PathSegment::Field(name) =&gt; {
<span class="comment">// is while fine? auto deref
</span><span class="kw">while let </span>TypeKind::Ref(<span class="kw">_</span>, inner) = ty {
projection.push(PlaceElem::Deref);
ty = inner.kind;
}
<span class="kw">if let </span>TypeKind::Struct(id) = ty {
<span class="kw">let </span>struct_body = builder.ctx.body.structs.get(<span class="kw-2">&amp;</span>id).unwrap();
<span class="kw">let </span>idx = <span class="kw-2">*</span>struct_body.name_to_idx.get(<span class="kw-2">&amp;</span>name.name).unwrap();
projection.push(PlaceElem::Field { field_idx: idx });
ty = struct_body.variants[idx].ty.kind.clone();
}
}
ast::PathSegment::Index { .. } =&gt; <span class="macro">todo!</span>(),
}
}
@ -1972,7 +1972,7 @@
(
Place {
local,
projection: projection.into(), <span class="comment">// todo, field array deref
projection: projection.into(), <span class="comment">// todo, array
</span>},
ty,
)