Skip to content

Commit 95d423d

Browse files
committed
asprintf progress, new style
1 parent 2c74196 commit 95d423d

File tree

7 files changed

+762
-296
lines changed

7 files changed

+762
-296
lines changed

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrFunction.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ public SavedFunctionState (LlvmIrFunction owner, ILlvmIrSavedFunctionSignatureSt
414414
public LlvmIrFunctionBody Body { get; }
415415
public string? Comment { get; set; }
416416
public bool ReturnsValue => Signature.ReturnType != typeof(void);
417+
public bool UsesVarArgs { get; }
417418

418419
public LlvmIrFunction (LlvmIrFunctionSignature signature, LlvmIrFunctionAttributeSet? attributeSet = null)
419420
{
@@ -422,6 +423,15 @@ public LlvmIrFunction (LlvmIrFunctionSignature signature, LlvmIrFunctionAttribut
422423

423424
functionState = new FunctionState ();
424425
foreach (LlvmIrFunctionParameter parameter in signature.Parameters) {
426+
if (UsesVarArgs) {
427+
throw new InvalidOperationException ($"Internal error: function '{signature.Name}' uses variable arguments and it has at least one argument following the varargs (...) one. This is not allowed.");
428+
}
429+
430+
if (parameter.IsVarArgs) {
431+
UsesVarArgs = true;
432+
continue;
433+
}
434+
425435
if (!String.IsNullOrEmpty (parameter.Name)) {
426436
continue;
427437
}

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrFunctionBody.cs

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,43 @@ public LlvmIrFunctionBody (LlvmIrFunction func, LlvmIrFunction.FunctionState fun
171171
previousLabel = implicitStartBlock = new LlvmIrFunctionImplicitStartLabel (functionState.StartingBlockNumber);
172172
}
173173

174+
public void Add (LlvmIrFunctionLabelItem label)
175+
{
176+
label.WillAddToBody (this, functionState);
177+
if (definedLabels.Contains (label.Name)) {
178+
throw new InvalidOperationException ($"Internal error: label with name '{label.Name}' already added to function '{ownerFunction.Signature.Name}' body");
179+
}
180+
items.Add (label);
181+
definedLabels.Add (label.Name);
182+
183+
// Rotate preceding blocks
184+
if (precedingBlock2 != null) {
185+
precedingBlock2 = null;
186+
}
187+
188+
precedingBlock2 = precedingBlock1;
189+
precedingBlock1 = previousLabel;
190+
previousLabel = label;
191+
192+
var comment = new StringBuilder (" preds = %");
193+
comment.Append (precedingBlock1.Name);
194+
if (precedingBlock2 != null) {
195+
comment.Append (", %");
196+
comment.Append (precedingBlock2.Name);
197+
}
198+
label.Comment = comment.ToString ();
199+
}
200+
201+
public void Add (LlvmIrFunctionBodyItem item)
202+
{
203+
items.Add (item);
204+
}
205+
206+
public void AddComment (string text)
207+
{
208+
Add (new LlvmIrFunctionBodyComment (text));
209+
}
210+
174211
public LlvmIrInstructions.Alloca Alloca (LlvmIrVariable result)
175212
{
176213
var ret = new LlvmIrInstructions.Alloca (result);
@@ -199,38 +236,25 @@ public LlvmIrInstructions.Call Call (LlvmIrFunction function, LlvmIrVariable? re
199236
return ret;
200237
}
201238

202-
public LlvmIrInstructions.Icmp Icmp (LlvmIrIcmpCond cond, LlvmIrVariable op1, object? op2, LlvmIrVariable result)
203-
{
204-
var ret = new LlvmIrInstructions.Icmp (cond, op1, op2, result);
205-
Add (ret);
206-
return ret;
207-
}
208-
209-
public LlvmIrInstructions.Load Load (LlvmIrVariable source, LlvmIrVariable result, LlvmIrMetadataItem? tbaa = null)
239+
public LlvmIrInstructions.Ext Ext (LlvmIrVariable source, Type targetType, LlvmIrVariable result)
210240
{
211-
var ret = new LlvmIrInstructions.Load (source, result) {
212-
TBAA = tbaa,
213-
};
241+
var ret = new LlvmIrInstructions.Ext (source, targetType, result);
214242
Add (ret);
215243
return ret;
216244
}
217245

218-
public LlvmIrInstructions.Store Store (LlvmIrVariable from, LlvmIrVariable to, LlvmIrMetadataItem? tbaa = null)
246+
public LlvmIrInstructions.Icmp Icmp (LlvmIrIcmpCond cond, LlvmIrVariable op1, object? op2, LlvmIrVariable result)
219247
{
220-
var ret = new LlvmIrInstructions.Store (from, to) {
221-
TBAA = tbaa,
222-
};
223-
248+
var ret = new LlvmIrInstructions.Icmp (cond, op1, op2, result);
224249
Add (ret);
225250
return ret;
226251
}
227252

228-
public LlvmIrInstructions.Store Store (LlvmIrVariable to, LlvmIrMetadataItem? tbaa = null)
253+
public LlvmIrInstructions.Load Load (LlvmIrVariable source, LlvmIrVariable result, LlvmIrMetadataItem? tbaa = null)
229254
{
230-
var ret = new LlvmIrInstructions.Store (to) {
255+
var ret = new LlvmIrInstructions.Load (source, result) {
231256
TBAA = tbaa,
232257
};
233-
234258
Add (ret);
235259
return ret;
236260
}
@@ -255,35 +279,26 @@ public LlvmIrInstructions.Ret Ret (Type retvalType, object? retval = null)
255279
return ret;
256280
}
257281

258-
public void Add (LlvmIrFunctionLabelItem label)
282+
public LlvmIrInstructions.Store Store (LlvmIrVariable from, LlvmIrVariable to, LlvmIrMetadataItem? tbaa = null)
259283
{
260-
label.WillAddToBody (this, functionState);
261-
if (definedLabels.Contains (label.Name)) {
262-
throw new InvalidOperationException ($"Internal error: label with name '{label.Name}' already added to function '{ownerFunction.Signature.Name}' body");
263-
}
264-
items.Add (label);
265-
definedLabels.Add (label.Name);
266-
267-
// Rotate preceding blocks
268-
if (precedingBlock2 != null) {
269-
precedingBlock2 = null;
270-
}
271-
272-
precedingBlock2 = precedingBlock1;
273-
precedingBlock1 = previousLabel;
274-
previousLabel = label;
284+
var ret = new LlvmIrInstructions.Store (from, to) {
285+
TBAA = tbaa,
286+
};
275287

276-
var comment = new StringBuilder (" preds = %");
277-
comment.Append (precedingBlock1.Name);
278-
if (precedingBlock2 != null) {
279-
comment.Append (", %");
280-
comment.Append (precedingBlock2.Name);
281-
}
282-
label.Comment = comment.ToString ();
288+
Add (ret);
289+
return ret;
283290
}
284291

285-
public void Add (LlvmIrFunctionBodyItem item)
292+
/// <summary>
293+
/// Stores `null` in the indicated variable
294+
/// </summary>
295+
public LlvmIrInstructions.Store Store (LlvmIrVariable to, LlvmIrMetadataItem? tbaa = null)
286296
{
287-
items.Add (item);
297+
var ret = new LlvmIrInstructions.Store (to) {
298+
TBAA = tbaa,
299+
};
300+
301+
Add (ret);
302+
return ret;
288303
}
289304
}

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrInstructions.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ public Call (LlvmIrFunction function, LlvmIrVariable? result = null, ICollection
230230
throw new ArgumentNullException ($"Internal error: function '{function.Signature.Name}' requires {argCount} arguments", nameof (arguments));
231231
}
232232

233-
if (arguments.Count != argCount) {
233+
if (function.UsesVarArgs) {
234+
if (arguments.Count < argCount) {
235+
throw new ArgumentException ($"Internal error: varargs function '{function.Signature.Name}' needs at least {argCount} fixed arguments, got {arguments.Count} instead");
236+
}
237+
} else if (arguments.Count != argCount) {
234238
throw new ArgumentException ($"Internal error: function '{function.Signature.Name}' requires {argCount} arguments, but {arguments.Count} were provided", nameof (arguments));
235239
}
236240

@@ -334,6 +338,53 @@ void WriteArgument (GeneratorWriteContext context, LlvmIrFunctionParameter param
334338
}
335339
}
336340

341+
public class Ext : LlvmIrInstruction
342+
{
343+
const string FpextOpCode = "fpext";
344+
const string SextOpCode = "sext";
345+
const string ZextOpCode = "zext";
346+
347+
LlvmIrVariable result;
348+
LlvmIrVariable source;
349+
Type targetType;
350+
351+
public Ext (LlvmIrVariable source, Type targetType, LlvmIrVariable result)
352+
: base (GetOpCode (targetType))
353+
{
354+
this.source = source;
355+
this.targetType = targetType;
356+
this.result = result;
357+
}
358+
359+
protected override void WriteValueAssignment (GeneratorWriteContext context)
360+
{
361+
context.Output.Write (result.Reference);
362+
context.Output.Write (" = ");
363+
}
364+
365+
protected override void WriteBody (GeneratorWriteContext context)
366+
{
367+
context.Output.Write (LlvmIrGenerator.MapToIRType (source.Type));
368+
context.Output.Write (' ');
369+
context.Output.Write (source.Reference);
370+
context.Output.Write (" to ");
371+
context.Output.Write ( LlvmIrGenerator.MapToIRType (targetType));
372+
}
373+
374+
static string GetOpCode (Type targetType)
375+
{
376+
if (targetType == typeof(double)) {
377+
return FpextOpCode;
378+
} else if (targetType == typeof(int)) {
379+
return SextOpCode;
380+
} else if (targetType == typeof(uint)) {
381+
return ZextOpCode;
382+
} else {
383+
throw new InvalidOperationException ($"Unsupported target type for upcasting: {targetType}");
384+
}
385+
}
386+
}
387+
337388
public class Icmp : LlvmIrInstruction
338389
{
339390
LlvmIrIcmpCond cond;

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void RegisterString (LlvmIrGlobalVariable variable, string? stringGroupName = nu
279279
RegisterString ((string)variable.Value, stringGroupName, stringGroupComment, symbolSuffix);
280280
}
281281

282-
void RegisterString (string value, string? stringGroupName = null, string? stringGroupComment = null, string? symbolSuffix = null)
282+
public void RegisterString (string value, string? stringGroupName = null, string? stringGroupComment = null, string? symbolSuffix = null)
283283
{
284284
if (stringManager == null) {
285285
stringManager = new LlvmIrStringManager ();

0 commit comments

Comments
 (0)