use std::{
fmt,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
};
use ruau_syntax::{
Location, Stat,
parse::{ParsedModule, parse_module_bytes_with_config, parse_module_with_config},
visit::{Visitor, WalkControl, walk_stat},
};
use crate::BytecodeChunk;
mod analysis;
mod builtin_folding;
mod context;
mod function_compiler;
mod helpers;
mod inline_cost;
mod options;
use analysis::ConstantValue;
use context::CompileContext;
#[cfg(test)]
use function_compiler::constant_ad_operand;
use function_compiler::{
BreakBranchKind, FunctionCompiler, LoopControlBranchKind, LoopUnrollPlan, TypeAliasInfo,
};
use helpers::*;
pub use options::{
CompileOptions, FastFlag, FastInt, KnownMember, KnownMemberValue, UpstreamCompilerOptions,
UpstreamParseOptions, effective_compile_options, source_compile_options,
};
pub const CONSTANT_STRING_FOLD_LIMIT: usize = 4096;
const EXPORT_TOP_LEVEL_MESSAGE: &str = "'export' may only be applied to top-level statements";
const EXPORT_RETURN_CONFLICT_MESSAGE: &str =
"Exporting values is not compatible with top-level return (export/return conflict)";
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CompileError {
kind: CompileErrorKind,
location: Option<Location>,
message: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CompileErrorKind {
Parse,
Cancelled,
Internal,
}
impl CompileError {
pub fn new(message: impl Into<String>) -> Self {
Self {
kind: CompileErrorKind::Internal,
location: None,
message: message.into(),
}
}
pub fn cancelled() -> Self {
Self {
kind: CompileErrorKind::Cancelled,
location: None,
message: "compilation cancelled".to_owned(),
}
}
fn parse(message: impl Into<String>, location: Location) -> Self {
Self {
kind: CompileErrorKind::Parse,
location: Some(location),
message: message.into(),
}
}
#[must_use]
pub fn kind(&self) -> CompileErrorKind {
self.kind
}
#[must_use]
pub fn location(&self) -> Option<Location> {
self.location
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for CompileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(location) = self.location {
write!(
f,
"{}:{}: {}",
location.begin.line + 1,
location.begin.column + 1,
self.message
)
} else {
f.write_str(&self.message)
}
}
}
impl std::error::Error for CompileError {}
pub fn compile_source(
source: &str,
options: &CompileOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
chunkify_parse_error(compile_source_strict(source, options, cancel))
}
pub fn compile_source_bytes(
source: &[u8],
options: &CompileOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
chunkify_parse_error(compile_source_bytes_strict(source, options, cancel))
}
pub fn compile_source_strict(
source: &str,
options: &CompileOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
let options = options.to_upstream_options();
compile_source_strict_with_upstream_options(source, &options, cancel)
}
#[doc(hidden)]
pub fn compile_source_strict_with_upstream_options(
source: &str,
options: &UpstreamCompilerOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
let effective = source_compile_options(source, options);
check_compile_cancelled(cancel.as_ref())?;
let parsed = parse_module_with_config(source, &effective.parse_config());
compile_parsed_module_with_effective(&parsed, &effective, cancel)
}
pub fn compile_source_bytes_strict(
source: &[u8],
options: &CompileOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
let options = options.to_upstream_options();
compile_source_bytes_strict_with_upstream_options(source, &options, cancel)
}
#[doc(hidden)]
pub fn compile_source_bytes_strict_with_upstream_options(
source: &[u8],
options: &UpstreamCompilerOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
let lossy = String::from_utf8_lossy(source);
let effective = source_compile_options(&lossy, options);
check_compile_cancelled(cancel.as_ref())?;
let parsed = parse_module_bytes_with_config(source, &effective.parse_config());
compile_parsed_module_with_effective(&parsed, &effective, cancel)
}
#[doc(hidden)]
pub fn compile_parsed_module_strict_with_upstream_options(
parsed: &ParsedModule,
options: &UpstreamCompilerOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
let lossy = String::from_utf8_lossy(parsed.source());
let effective = source_compile_options(&lossy, options);
check_compile_cancelled(cancel.as_ref())?;
compile_parsed_module_with_effective(parsed, &effective, cancel)
}
fn compile_parsed_module_with_effective(
parsed: &ParsedModule,
effective: &UpstreamCompilerOptions,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
if !parsed
.config()
.ast_compatible_with(effective.parse_config())
{
return Err(CompileError::new(
"parsed module options do not match effective compiler syntax options",
));
}
if let Some(error) = parsed.errors().first() {
return Err(CompileError::parse(error.message.clone(), error.location));
}
let mut effective = effective.clone();
apply_native_attribute_options(parsed.root(), &mut effective);
compile_ast_with_implicit_return_delta(
parsed.root(),
&effective,
u8::from(parsed.source().ends_with(b"\n")),
cancel,
)
}
fn apply_native_attribute_options(root: &Stat, options: &mut UpstreamCompilerOptions) {
struct NativeAttributeVisitor {
found: bool,
}
impl<'ast> Visitor<'ast> for NativeAttributeVisitor {
fn visit_expr(&mut self, expr: &'ast ruau_syntax::Expr) -> WalkControl {
if let ruau_syntax::Expr::Function { attributes, .. } = expr
&& attributes
.iter()
.any(|attribute| attribute.name.as_str() == "native")
{
self.found = true;
return WalkControl::SkipChildren;
}
WalkControl::Continue
}
}
let mut visitor = NativeAttributeVisitor { found: false };
walk_stat(root, &mut visitor);
if visitor.found {
options.optimization_level = 2;
options.type_info_level = 1;
}
}
#[doc(hidden)]
pub fn chunkify_parse_error(
result: Result<BytecodeChunk, CompileError>,
) -> Result<BytecodeChunk, CompileError> {
match result {
Err(error) if error.kind() == CompileErrorKind::Parse => {
let message = match error.location() {
Some(location) => {
format!(":{}: {}", location.begin.line + 1, error.message())
}
None => error.message().to_owned(),
};
Ok(BytecodeChunk::Error {
message: message.into_bytes(),
})
}
other => other,
}
}
fn line_location(line: u32) -> Location {
let position = ruau_syntax::Position {
line: line.saturating_sub(1),
column: 0,
};
Location {
begin: position,
end: position,
}
}
fn compile_ast_with_implicit_return_delta(
root: &Arc<Stat>,
options: &UpstreamCompilerOptions,
implicit_return_line_delta: u8,
cancel: Option<Arc<AtomicBool>>,
) -> Result<BytecodeChunk, CompileError> {
check_compile_cancelled(cancel.as_ref())?;
if let Some((line, message)) = repeat_continue_condition_error(root) {
return Err(CompileError::parse(message, line_location(line)));
}
if let Some((location, message)) = export_value_ast_error(root) {
return Err(CompileError::parse(message, location));
}
let context = CompileContext::with_cancel(Arc::clone(root), options, cancel);
context.check_cancelled()?;
let mut compiler = FunctionCompiler::new(context, implicit_return_line_delta);
compiler.compile_registered_functions_for_root(root)?;
compiler.compile_root(root)?;
Ok(compiler.finish())
}
fn check_compile_cancelled(cancel: Option<&Arc<AtomicBool>>) -> Result<(), CompileError> {
if cancel.is_some_and(|cancel| cancel.load(Ordering::Relaxed)) {
return Err(CompileError::cancelled());
}
Ok(())
}
fn export_value_ast_error(root: &Stat) -> Option<(Location, &'static str)> {
let Stat::Block { body, .. } = root else {
return exported_value_location(root).map(|location| (location, EXPORT_TOP_LEVEL_MESSAGE));
};
let mut has_export = false;
let mut has_return = false;
for stat in body {
if let Some(location) = nested_exported_value_location(stat) {
return Some((location, EXPORT_TOP_LEVEL_MESSAGE));
}
if let Some(location) = exported_value_location(stat) {
if has_return {
return Some((location, EXPORT_RETURN_CONFLICT_MESSAGE));
}
has_export = true;
}
if let Stat::Return {
location: Some(location),
..
} = stat
{
if has_export {
return Some((*location, EXPORT_RETURN_CONFLICT_MESSAGE));
}
has_return = true;
}
}
None
}
fn nested_exported_value_location(stat: &Stat) -> Option<Location> {
struct NestedExportFinder {
skip_root: bool,
location: Option<Location>,
}
impl<'ast> Visitor<'ast> for NestedExportFinder {
fn visit_stat(&mut self, stat: &'ast Stat) -> WalkControl {
if self.skip_root {
self.skip_root = false;
return WalkControl::Continue;
}
if self.location.is_none() {
self.location = exported_value_location(stat);
}
if self.location.is_some() {
WalkControl::SkipChildren
} else {
WalkControl::Continue
}
}
}
let mut finder = NestedExportFinder {
skip_root: true,
location: None,
};
walk_stat(stat, &mut finder);
finder.location
}
fn exported_value_location(stat: &Stat) -> Option<Location> {
match stat {
Stat::Local {
exported: true,
location,
..
}
| Stat::LocalFunction {
exported: true,
location,
..
}
| Stat::Class {
exported: true,
location,
..
} => *location,
_ => None,
}
}
#[cfg(test)]
mod tests;