Skip to content

Commit

Permalink
Add Definition kind for asm register operand
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 5, 2024
1 parent 44e3b7d commit 304f54e
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 35 deletions.
17 changes: 13 additions & 4 deletions src/tools/rust-analyzer/crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum Definition {
ToolModule(ToolModule),
ExternCrateDecl(ExternCrateDecl),
InlineAsmRegOrRegClass(()),
InlineAsmRegOperand(()),
}

impl Definition {
Expand Down Expand Up @@ -89,7 +90,8 @@ impl Definition {
| Definition::BuiltinLifetime(_)
| Definition::TupleField(_)
| Definition::ToolModule(_)
| Definition::InlineAsmRegOrRegClass(_) => return None,
| Definition::InlineAsmRegOrRegClass(_)
| Definition::InlineAsmRegOperand(_) => return None,
};
Some(module)
}
Expand Down Expand Up @@ -124,7 +126,8 @@ impl Definition {
| Definition::GenericParam(_)
| Definition::Label(_)
| Definition::DeriveHelper(_)
| Definition::InlineAsmRegOrRegClass(_) => return None,
| Definition::InlineAsmRegOrRegClass(_)
| Definition::InlineAsmRegOperand(_) => return None,
};
Some(vis)
}
Expand Down Expand Up @@ -153,7 +156,9 @@ impl Definition {
Definition::ToolModule(_) => return None, // FIXME
Definition::DeriveHelper(it) => it.name(db),
Definition::ExternCrateDecl(it) => return it.alias_or_name(db),
Definition::InlineAsmRegOrRegClass(_) => return None, // FIXME
Definition::InlineAsmRegOrRegClass(_) | Definition::InlineAsmRegOperand(_) => {
return None
} // FIXME
};
Some(name)
}
Expand Down Expand Up @@ -216,7 +221,7 @@ impl Definition {
Definition::ToolModule(_) => None,
Definition::DeriveHelper(_) => None,
Definition::TupleField(_) => None,
Definition::InlineAsmRegOrRegClass(_) => None,
Definition::InlineAsmRegOrRegClass(_) | Definition::InlineAsmRegOperand(_) => None,
};

docs.or_else(|| {
Expand Down Expand Up @@ -275,6 +280,7 @@ impl Definition {
}
// FIXME
Definition::InlineAsmRegOrRegClass(_) => "inline_asm_reg_or_reg_class".to_owned(),
Definition::InlineAsmRegOperand(_) => "inline_asm_reg_operand".to_owned(),
}
}
}
Expand Down Expand Up @@ -706,6 +712,9 @@ impl NameRefClass {
NameRefClass::ExternCrateShorthand { krate, decl: extern_crate }
})
},
ast::AsmRegSpec(_) => {
Some(NameRefClass::Definition(Definition::InlineAsmRegOrRegClass(())))
},
_ => None
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ impl Definition {
| Definition::ToolModule(_)
| Definition::TupleField(_)
| Definition::InlineAsmRegOrRegClass(_) => return None,
// FIXME:
Definition::InlineAsmRegOperand(_) => return None,
// FIXME: This should be doable in theory
Definition::DeriveHelper(_) => return None,
};
Expand Down
6 changes: 4 additions & 2 deletions src/tools/rust-analyzer/crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ pub(crate) fn resolve_doc_path_for_def(
| Definition::GenericParam(_)
| Definition::Label(_)
| Definition::DeriveHelper(_)
| Definition::InlineAsmRegOrRegClass(_) => None,
| Definition::InlineAsmRegOrRegClass(_)
| Definition::InlineAsmRegOperand(_) => None,
}
.map(Definition::from)
}
Expand Down Expand Up @@ -674,7 +675,8 @@ fn filename_and_frag_for_def(
| Definition::BuiltinLifetime(_)
| Definition::ToolModule(_)
| Definition::DeriveHelper(_)
| Definition::InlineAsmRegOrRegClass(_) => return None,
| Definition::InlineAsmRegOrRegClass(_)
| Definition::InlineAsmRegOperand(_) => return None,
};

Some((def, res, None))
Expand Down
5 changes: 3 additions & 2 deletions src/tools/rust-analyzer/crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub(crate) fn def_to_kind(db: &RootDatabase, def: Definition) -> SymbolInformati
Variable
}
}
Definition::Label(..) => Variable, // For lack of a better variant
Definition::Label(..) | Definition::InlineAsmRegOperand(_) => Variable, // For lack of a better variant
Definition::DeriveHelper(..) => Attribute,
Definition::BuiltinAttr(..) => Attribute,
Definition::ToolModule(..) => Module,
Expand Down Expand Up @@ -322,7 +322,8 @@ pub(crate) fn def_to_moniker(
| Definition::BuiltinLifetime(_)
| Definition::BuiltinAttr(_)
| Definition::ToolModule(_)
| Definition::InlineAsmRegOrRegClass(_) => return None,
| Definition::InlineAsmRegOrRegClass(_)
| Definition::InlineAsmRegOperand(_) => return None,

Definition::Local(local) => {
if !local.is_param(db) {
Expand Down
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/crates/ide/src/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ impl TryToNav for Definition {
| Definition::ToolModule(_)
| Definition::InlineAsmRegOrRegClass(_)
| Definition::BuiltinAttr(_) => None,
// FIXME
Definition::InlineAsmRegOperand(_) => None,
// FIXME: The focus range should be set to the helper declaration
Definition::DeriveHelper(it) => it.derive().try_to_nav(db),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ pub(super) fn highlight_def(
Definition::InlineAsmRegOrRegClass(_) => {
Highlight::new(HlTag::Symbol(SymbolKind::InlineAsmRegOrRegClass))
}
Definition::InlineAsmRegOperand(_) => Highlight::new(HlTag::Symbol(SymbolKind::Local)),
};

let def_crate = def.krate(db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn module_def_to_hl_tag(def: Definition) -> HlTag {
Definition::ToolModule(_) => SymbolKind::ToolModule,
Definition::DeriveHelper(_) => SymbolKind::DeriveHelper,
Definition::InlineAsmRegOrRegClass(_) => SymbolKind::InlineAsmRegOrRegClass,
Definition::InlineAsmRegOperand(_) => SymbolKind::Local,
};
HlTag::Symbol(symbol)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,17 @@
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">o</span> <span class="operator">=</span> <span class="numeric_literal">0</span><span class="semicolon">;</span>
<span class="module crate_root default_library library">core</span><span class="operator">::</span><span class="module default_library library">arch</span><span class="operator">::</span><span class="macro default_library library unsafe">asm</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span>
<span class="string_literal macro">"%input = OpLoad _ {0}"</span><span class="comma macro">,</span>
<span class="macro default_library library macro">concat</span><span class="macro_bang macro">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"%result = "</span><span class="comma macro">,</span> <span class="unresolved_reference macro">bar</span><span class="comma macro">,</span> <span class="string_literal macro">" _ %input"</span><span class="parenthesis macro">)</span><span class="comma macro">,</span>
<span class="macro default_library library macro">concat</span><span class="macro_bang macro">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"%result = "</span><span class="comma macro">,</span> <span class="string_literal macro">"bar"</span><span class="comma macro">,</span> <span class="string_literal macro">" _ %input"</span><span class="parenthesis macro">)</span><span class="comma macro">,</span>
<span class="string_literal macro">"OpStore {1} %result"</span><span class="comma macro">,</span>
<span class="keyword control macro">in</span><span class="parenthesis macro">(</span><span class="unresolved_reference macro">reg</span><span class="parenthesis macro">)</span> <span class="operator macro">&</span><span class="variable macro">foo</span><span class="comma macro">,</span>
<span class="keyword control macro">in</span><span class="parenthesis macro">(</span><span class="unresolved_reference macro">reg</span><span class="parenthesis macro">)</span> <span class="operator macro">&</span><span class="keyword macro">mut</span> <span class="variable macro mutable">o</span><span class="comma macro">,</span>
<span class="keyword control macro">in</span><span class="parenthesis macro">(</span><span class="reg library macro">reg</span><span class="parenthesis macro">)</span> <span class="operator macro">&</span><span class="variable macro">foo</span><span class="comma macro">,</span>
<span class="keyword control macro">in</span><span class="parenthesis macro">(</span><span class="reg library macro">reg</span><span class="parenthesis macro">)</span> <span class="operator macro">&</span><span class="keyword macro">mut</span> <span class="variable macro mutable">o</span><span class="comma macro">,</span>
<span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="variable mutable">o</span>

<span class="keyword">let</span> <span class="variable declaration">thread_id</span><span class="colon">:</span> <span class="builtin_type">usize</span><span class="semicolon">;</span>
<span class="module crate_root default_library library">core</span><span class="operator">::</span><span class="module default_library library">arch</span><span class="operator">::</span><span class="macro default_library library unsafe">asm</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"
mov {0}, gs:[0x30]
mov {0}, [{0}+0x48]
"</span><span class="comma macro">,</span> <span class="keyword macro">out</span><span class="parenthesis macro">(</span><span class="unresolved_reference macro">reg</span><span class="parenthesis macro">)</span> <span class="variable macro">thread_id</span><span class="comma macro">,</span> <span class="keyword macro">options</span><span class="parenthesis macro">(</span><span class="keyword macro">pure</span><span class="comma macro">,</span> <span class="keyword macro">readonly</span><span class="comma macro">,</span> <span class="keyword macro">nostack</span><span class="parenthesis macro">)</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
"</span><span class="comma macro">,</span> <span class="keyword macro">out</span><span class="parenthesis macro">(</span><span class="reg library macro">reg</span><span class="parenthesis macro">)</span> <span class="variable macro">thread_id</span><span class="comma macro">,</span> <span class="keyword macro">options</span><span class="parenthesis macro">(</span><span class="keyword macro">pure</span><span class="comma macro">,</span> <span class="keyword macro">readonly</span><span class="comma macro">,</span> <span class="keyword macro">nostack</span><span class="parenthesis macro">)</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>

<span class="keyword">static</span> <span class="static declaration">UNMAP_BASE</span><span class="colon">:</span> <span class="builtin_type">usize</span><span class="semicolon">;</span>
<span class="keyword const">const</span> <span class="constant const declaration">MEM_RELEASE</span><span class="colon">:</span> <span class="builtin_type">usize</span><span class="semicolon">;</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@
<span class="module crate_root default_library library">core</span><span class="operator">::</span><span class="module default_library library">arch</span><span class="operator">::</span><span class="macro default_library library unsafe">asm</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span>
<span class="string_literal macro">"mov {0}, {1}"</span><span class="comma macro">,</span>
<span class="string_literal macro">"add {0}, 5"</span><span class="comma macro">,</span>
<span class="keyword macro">out</span><span class="parenthesis macro">(</span><span class="unresolved_reference macro">reg</span><span class="parenthesis macro">)</span> <span class="variable macro">o</span><span class="comma macro">,</span>
<span class="keyword control macro">in</span><span class="parenthesis macro">(</span><span class="unresolved_reference macro">reg</span><span class="parenthesis macro">)</span> <span class="variable macro">i</span><span class="comma macro">,</span>
<span class="keyword macro">out</span><span class="parenthesis macro">(</span><span class="reg library macro">reg</span><span class="parenthesis macro">)</span> <span class="variable macro">o</span><span class="comma macro">,</span>
<span class="keyword control macro">in</span><span class="parenthesis macro">(</span><span class="reg library macro">reg</span><span class="parenthesis macro">)</span> <span class="variable macro">i</span><span class="comma macro">,</span>
<span class="parenthesis macro">)</span><span class="semicolon">;</span>

<span class="keyword const">const</span> <span class="constant const declaration">CONSTANT</span><span class="colon">:</span> <span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">=</span> <span class="parenthesis">(</span><span class="parenthesis">)</span><span class="colon">:</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1287,12 +1287,11 @@ fn main() {
let mut o = 0;
core::arch::asm!(
"%input = OpLoad _ {0}",
concat!("%result = ", bar, " _ %input"),
concat!("%result = ", "bar", " _ %input"),
"OpStore {1} %result",
in(reg) &foo,
in(reg) &mut o,
);
o
let thread_id: usize;
core::arch::asm!("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,13 @@ fn parse_clobber_abi(p: &mut Parser<'_>) {
fn parse_reg(p: &mut Parser<'_>) {
p.expect(T!['(']);
if p.at(T![ident]) {
name_ref(p)
let m = p.start();
name_ref(p);
m.complete(p, ASM_REG_SPEC);
} else if p.at(T![string]) {
p.bump_any()
let m = p.start();
p.bump_any();
m.complete(p, ASM_REG_SPEC);
} else {
p.err_and_bump("expected register name");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ SOURCE_FILE
STRING "\"add {x}, {tmp}\""
COMMA ","
WHITESPACE "\n "
NAME
IDENT "x"
WHITESPACE " "
EQ "="
WHITESPACE " "
ASM_REG_OPERAND
INOUT_KW "inout"
NAME
IDENT "x"
WHITESPACE " "
EQ "="
WHITESPACE " "
ASM_DIR_SPEC
INOUT_KW "inout"
L_PAREN "("
NAME_REF
IDENT "reg"
ASM_REG_SPEC
NAME_REF
IDENT "reg"
R_PAREN ")"
WHITESPACE " "
PATH_EXPR
Expand All @@ -54,16 +56,18 @@ SOURCE_FILE
IDENT "x"
COMMA ","
WHITESPACE "\n "
NAME
IDENT "tmp"
WHITESPACE " "
EQ "="
WHITESPACE " "
ASM_REG_OPERAND
OUT_KW "out"
NAME
IDENT "tmp"
WHITESPACE " "
EQ "="
WHITESPACE " "
ASM_DIR_SPEC
OUT_KW "out"
L_PAREN "("
NAME_REF
IDENT "reg"
ASM_REG_SPEC
NAME_REF
IDENT "reg"
R_PAREN ")"
WHITESPACE " "
UNDERSCORE_EXPR
Expand Down

0 comments on commit 304f54e

Please sign in to comment.